本文翻译自:How to send an email using PHP?

I am using PHP on a website and I want to add emailing functionality. 我在网站上使用PHP,并且想添加电子邮件功能。

I have WAMPSERVER installed. 我已经安装了WAMPSERVER。

How do I send an email using PHP? 如何使用PHP发送电子邮件?


#1楼

参考:https://stackoom.com/question/MNwn/如何使用PHP发送电子邮件


#2楼

If you are interested in html formatted email, make sure to pass Content-type: text/html; 如果您对html格式的电子邮件感兴趣,请确保传递Content-type: text/html; in the header. 在标题中。 Example: 例:

// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';// subject
$subject = 'Birthday Reminders for August';// message
$message = '
<html>
<head><title>Birthday Reminders for August</title>
</head>
<body><p>Here are the birthdays upcoming in August!</p><table><tr><th>Person</th><th>Day</th><th>Month</th><th>Year</th></tr><tr><td>Joe</td><td>3rd</td><td>August</td><td>1970</td></tr><tr><td>Sally</td><td>17th</td><td>August</td><td>1973</td></tr></table>
</body>
</html>
';// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";// Mail it
mail($to, $subject, $message, $headers);

For more details, check php mail function. 有关更多详细信息,请检查php 邮件功能。


#3楼

You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer . 您也可以在https://github.com/PHPMailer/PHPMailer使用PHPMailer类。

It allows you to use the mail function or use an smtp server transparently. 它允许您透明地使用邮件功能或使用smtp服务器。 It also handles HTML based emails and attachments so you don't have to write your own implementation. 它还处理基于HTML的电子邮件和附件,因此您不必编写自己的实现。

The class is stable and it is used by many other projects like Drupal, SugarCRM, Yii, and Joomla! 该类是稳定的,它被许多其他项目(例如Drupal,SugarCRM,Yii和Joomla)使用。

Here is an example from the page above: 这是上一页的示例:

<?php
require 'PHPMailerAutoload.php';$mail = new PHPMailer;$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';if(!$mail->send()) {echo 'Message could not be sent.';echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {echo 'Message has been sent';
}

#4楼

this is very basic method to send plain text email using mail function. 这是使用邮件功能发送纯文本电子邮件的非常基本的方法。

<?php
$to = 'SomeOtherEmailAddress@Domain.com';
$subject = 'This is subject';
$message = 'This is body of email';
$from = "From: FirstName LastName <SomeEmailAddress@Domain.com>";
mail($to,$subject,$message,$from);

#5楼

You can use a mail web service such as Postmark, Sendgrid etc. 您可以使用邮件网络服务,例如邮戳,Sendgrid等。

Sendgrid vs Postmark vs Amazon SES and other email/SMTP API providers? Sendgrid,Postmark,Amazon SES和其他电子邮件/ SMTP API提供程序?

Edit: I just use the Google Gmail API now. 编辑:我现在只使用Google Gmail API 。 I had trouble sending reminder email to my employer's organization due to strict filters. 由于严格的过滤器,我无法向我的雇主组织发送提醒电子邮件。 But Gmail works as long as you don't spam people. 但是,只要您不向他人发送垃圾邮件,Gmail就可以正常工作。


#6楼

Try this: 尝试这个:

<?php
$to = "somebody@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";mail($to,$subject,$txt,$headers);
?>

如何使用PHP发送电子邮件?相关推荐

  1. 亚马逊ses如何发qq_使用Amazon SES发送电子邮件

    亚马逊ses如何发qq by Kangze Huang 黄康泽 使用Amazon SES发送电子邮件 (Sending emails with Amazon SES) 完整的AWS Web样板-教程3 ...

  2. php mail函数_PHP发送电子邮件函数mail详解

    重要:本文最后更新于2019-10-09 08:49:07,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗. PHP作为一门使用广泛的脚本语言,经过这么多年发展已经非常完善了,发送电子 ...

  3. 如何从我的Android应用程序发送电子邮件?

    我正在为Android编写应用程序. 如何从中发送电子邮件? #1楼 简单尝试这个 public void onCreate(Bundle savedInstanceState) {super.onC ...

  4. 在Python中使用SMTP发送电子邮件

    Python中有内置的smtplib模块,完成一封邮件的发送,需要做很多准备工作.第三方的py_smtp这个包也是基于smtplib的,在Python中使用py_smtp发送电子邮件非常方便,只要填写 ...

  5. C# 发送电子邮件(含附件)用到的类 system.web.mail

    主要是用到了System.Web.Mail命名空间,用到了此空间的三个类,分别是: ●MailMessage类,用于构造电子邮件 ●MailAttachment类,用于构造电子邮件附件 ●SmtpMa ...

  6. net类库中发送电子邮件的方法总结

    发送电子邮件是许多需要用户注册的网站的通用功能,通过正则表达式我们可以过滤掉不符合电子邮件格式的输入,但是仍没有办法确保用户填写的电子邮件地址一定是他本人真实有效的电子邮件地址,通常验证电子邮件真实有 ...

  7. iphone发送邮件html,iPhone使用smtp服务器发送电子邮件?

    在我的应用程序中,我能够使用smtp服务器发送电子邮件,因为我输入了正确的电子邮件ID和密码. 但当我输入我的Gmail或雅虎帐户详细信息时,我无法发送邮件. 因为我设置了relayHost = @& ...

  8. linux发邮件安装什么意思,linux – 如何找出安装/发送电子邮件的邮件程序?

    我正在尝试找出在我的服务器上发送电子邮件的电子邮件程序(如果有的话).我的雇主有几台服务器,其中大部分使用sendmail,但在我们的两台服务器上,我找不到电子邮件程序,但电子邮件已经发送了吗?我不想 ...

  9. php发送gmail,使用GMail SMTP服务器从PHP页面发送电子邮件

    我试图通过Gmail的SMTP服务器从一个PHP页面发送电子邮件,但是我得到了这个错误: 身份验证失败[SMTP:SMTP服务器不支持身份验证(代码:250,响应:mx.google.com at y ...

最新文章

  1. 用自定义的RoundImageView来实现圆形图片(可加边框)
  2. python爬虫天气预报难不难_python爬虫可以用来看天气预报吗?
  3. web service notes
  4. 三次握手和四次挥手之间的关系
  5. 丑数 Ugly Number
  6. 防火墙未来的技术发展趋势
  7. idea常用快捷方式
  8. 视觉SLAM——D435i运行ORB-SLAM2-RGB-D(依赖ros版)
  9. Truncated Signed Distance Function: Experiments on Voxel Size
  10. java web 课程设计_关于Javaweb课程设计的心得体会.doc
  11. oracle查询语句中case when的使用
  12. NanoHTTPD接收post数据
  13. 人脸数据清洗方法+代码汇总
  14. windows电脑版便签工具哪款好用?
  15. 新闻APP受到各界关注 趣头条 大众看点 等新闻
  16. 2022极米投影和米家投影哪个好 家用智能投影仪对比
  17. 极客学院 Android 系统体系教程
  18. AAAI 2023 Fello名单出炉,李学龙教授完成AI 领域Fellow大满贯!
  19. Oracle AWR报告生成步骤
  20. APP 转让问题记录-跳转微信小程序正在连接

热门文章

  1. Access 数据库连接字符串 (有密码)
  2. 面试Android实习生
  3. 华丽丽的GridLayout-使用案例
  4. Android之四大组件(Activity)
  5. Widget上实时刷新图片,造成anr问题
  6. CocoaPods私有库创建
  7. 数字建模matlab,Matlab基础及数学建模.ppt
  8. python transformers_transformers 安装
  9. [CSU1911]Card Game
  10. 802.1p 优先级与内部优先级的映射关系