PHPMailer - 功能齐全的 PHP 电子邮件库

电子邮件,现在仍然是互联网中最常用、最通用的通信方式之一。其能够承载包括文字、图像和视频等多种形式的信息,且普及性非常广,几乎所有的互联网用户都拥有邮箱地址。

在软件开发的过程中,发送邮件是十分常见的需求,尤其是在进行自动通知功能中,发送邮件往往是主要的通知方式之一。

然而,在邮件发送机制中,包括身份验证、邮件格式、附件文件、编码和国际化等,都比较容易产生问题。一个功能强大且完备的电子邮件库是很有必要的。

PHPMailer


简介

PHPMailer,是 PHPMailer 组织在 Github 上开源的 PHP 电子邮件库,目前版本为 6.3.0。

PHPMailer 是最为著名的电子邮件库之一,被广泛使用在开源项目中,包括 WordPress、Drupal、Yii 等。其集成了 SMTP 支持,无需另外搭建邮件服务;支持附件和内联附件;支持各种编码和各国语言;自动验证电子邮件地址,拥有邮件头注入保护机制;兼容 PHP 5.5 +,包括 PHP 8.0。


PHPMailer


使用

要在项目中使用 PHPMailer,可以用 Composer 安装,在 composer.json 文件中添加

"phpmailer/phpmailer": "^6.2"

或直接运行 Composer 安装:

composer require phpmailer/phpmailer

也可以手动下载,复制到 include_path 中,并添加类依赖:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

安装完成后,既可以在项目中使用了。


PHPMailer


使用 PHPMailer 发送邮件十分简单,我们来看一个简单的例子:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.example.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'user@example.com';                     //SMTP username
    $mail->Password   = 'secret';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('from@example.com', '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');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $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';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}


首先,使用 new PHPMailer 创建一个邮件服务。然后对其进行配置:

  • 服务配置,包括主机、端口、用户名、密码,和 SMTP 协议相关等
  • 邮件接收设置,包括发送地址、接收地址、抄送地址等
  • 附件配置,添加文件附件
  • 内容配置,包括内容格式、主题、内容等

最后,调用 $mail->send() 完成发送。整个流程包裹在 try ... catch 中进行错误处理。


PHPMailer

PHPMailer 还支持更多的高级功能,比如邮件加签:

$mail->sign(
    '/path/to/cert.crt', //The location of your certificate file
    '/path/to/cert.key', //The location of your private key file
    'yourSecretPrivateKeyPassword',
    '/path/to/certchain.pem' //The location of your chain file
);

SMTP 协议的 SSL 配置:

$mail->SMTPOptions = array(
    'ssl' => [
        'verify_peer' => true,
        'verify_depth' => 3,
        'allow_self_signed' => true,
        'peer_name' => 'smtp.example.com',
        'cafile' => '/etc/ssl/ca_cert.pem',
    ],
);


总结

PHPMailer 设计简洁,使用简单,能够很容易地就实现电子邮件发送的需求,不需要过多的额外配置和依赖。

PHPMailer 功能齐全,对 SMTP 协议和各主流邮箱服务拥有完善的支持,尤其在安全性,和邮件内容格式方面,能够支持十分复杂的专业配置,使得其能够适应各种各样的应用场景。


PHPMailer

举报
评论 0