在php网站开发中,发送电子邮件是一个非常普片的需求。比如网站注册功能,当用户注册完成后需要发送电子邮件给用户,提示用户注册成功或者发送验证链接,另外,用户修改账号密码也需要发送电子邮件。

本文章向大家介绍php发送邮件的两种方法:

  1. 使用php mail()发送邮件
  2. 使用第三方类库PHPMailer发送邮件

使用php mail()发送邮件

mail()是php的内置函数,它允许使用本地sendmail 程序发送电子邮件。无论何时调用mail()函数,它都会调用本地sendmail程序,该程序通常由系统管理员配置。如果你的虚拟主机位于Hostinger,你可以在电子邮件 – >邮件服务控制 部分启用/禁用此功能 。

默认情况下sendmail服务是自启(自行启动)。

语法:

mail(to,subject,message,headers,parameters)

参数:

参数名 描述
to 必需。规定 email 接收者。
subject 必需。规定 email 的主题。注释:该参数不能包含任何新行字符。
message 必需。定义要发送的消息。应使用 LF (\n) 来分隔各行。
headers 可选。规定附加的标题,比如 From、Cc 以及 Bcc。

应当使用 CRLF (\r\n) 分隔附加的标题。

parameters 可选。对邮件发送程序规定额外的参数。

使用php mail()发送html邮件

<?php
$to = “somebody@example.com, somebodyelse@example.com”;
$subject = “HTML email”;
$message = “
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
“;
// 当发送 HTML 电子邮件时,请始终设置 content-type
$headers = “MIME-Version: 1.0″ . “\r\n”;
$headers .= “Content-type:text/html;charset=iso-8859-1” . “\r\n”;
// 更多报头
$headers .= ‘From: <webmaster@example.com>’ . “\r\n”;
$headers .= ‘Cc: myboss@example.com’ . “\r\n”;
mail($to,$subject,$message,$headers);
?>

重要的是要记住,要发送HTML邮件,您需要设置Content-type标头:


  1. $headers = ‘MIME-Version: 1.0’ . “\r\n”;
  2. $headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;

使用php mail()发送纯文本邮件


<?php
$to = ‘maryjane@email.com’;
$subject = ‘Marriage Proposal’;
$message = ‘Hi Jane, will you marry me?’;
$from = ‘peterparker@email.com’;
// Sending email
if(mail($to, $subject, $message)){
echo ‘Your mail has been sent successfully.’;
} else{
echo ‘Unable to send email. Please try again.’;
}
?>

使用第三方类库PHPMailer发送邮件

PHPMailer是一个非常优秀的php第三方邮箱发送类函数, 专门用于php语言的邮件发送类,功能十分地强大,丰富了 PHP 本身单一的 mail() 函数。支持 SMTP 等、附件等。 PHPMailer 遵守 LGPL 授权,可以免费下载。

PHPMailer主要功能特点:

  1. 支持邮件 s/mime加密的数字签名
  2. 支持邮件多个 TOs, CCs, BCCs and REPLY-TOs
  3. 可以工作在任何服务器平台,所以不用担心WIN平台无法发送邮件的问题的
  4. 支持文本/HTML格式邮件
  5. 可以嵌入image图像
  6. 对于邮件客户端不支持HTML阅读的进行支持
  7. 功能强大的发送邮件调试功能debug
  8. 自定义邮件header
  9. 冗余SMTP服务器支持
  10. 支持8bit, base64, binary, and quoted-printable 编码
  11. 文字自动换行
  12. 支持多附件发送功能
  13. 支持SMTP服务器验证功能
  14. 在Sendmail, qmail, Postfix, Gmail, Imail, Exchange 等平台测试成功
  15. 提供的下载文件中,包括内容详细的说明文档及示例说明,所以不用担心难于上手的问题!
  16. PHPMailer 非常小巧、简单、方便、快捷

下载地址:https://github.com/PHPMailer/PHPMailer

下面我们一起来看个php中利用PHPMailer插件实现gmail发送邮件实例,希望此教程对大家有帮助。

下面代码是使用PHPMailer从本地Web服务器发送电子邮件的最简单示例:


<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once “vendor/autoload.php”;
//PHPMailer Object
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions
//From email address and name
$mail->From = “from@yourdomain.com”;
$mail->FromName = “Full Name”;
//To address and name
$mail->addAddress(“recepient1@example.com”, “Recepient Name”);
$mail->addAddress(“recepient1@example.com”); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo(“reply@yourdomain.com”, “Reply”);
//CC and BCC
$mail->addCC(“cc@example.com”);
$mail->addBCC(“bcc@example.com”);
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = “Subject Text”;
$mail->Body = “<i>Mail body in HTML</i>”;
$mail->AltBody = “This is the plain text version of the email content”;
try {
$mail->send();
echo “Message has been sent successfully”;
} catch (Exception $e) {
echo “Mailer Error: ” . $mail->ErrorInfo;
}

使用PHPMailer发送带有附件的电子邮件的示例:

<?php
require_once ‘class.phpmailer.php’;
$mail = new PHPMailer();
// Now you only need to add the necessary stuff
// HTML body
$body = “</pre>
<div>”;
$body .= ” Hello Dimitrios
“;
$body .= “<i>Your</i> personal photograph to this message.
“;
$body .= “Sincerely,
“;
$body .= “phpmailer test message “;
$body .= “</div>” ;
// And the absolute required configurations for sending HTML with attachement
$mail->AddAddress(“sendemailto@mail.zz”, “My-webpage Website”);
$mail->Subject = “test for phpmailer-3”;
$mail->MsgHTML($body);
$mail->AddAttachment(“phpmailer.gif”);
if(!$mail->Send()) {
echo “There was an error sending the message”;
exit;
}
echo “Message was sent successfully”;
?>

要将附件添加到电子邮件中,我们只需要使用addAttachment函数,该函数接受文件路径作为参数。要附加多个文件,你只需要多次调用它就可以了。