【问题标题】:How should I upgrade from PHPMailer 5.2 to 6.0?我应该如何从 PHPMailer 5.2 升级到 6.0?
【发布时间】:2018-02-06 23:50:04
【问题描述】:

我有一个当前使用最新版本的 PHPMailer 5.2.x 的脚本。 PHPMailer 6.0 已经发布,但是说它会破坏向后兼容性——我需要做什么来升级?

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$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 TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$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');

$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';
}

【问题讨论】:

    标签: php email phpmailer


    【解决方案1】:

    PHPMailer 6.0 中发生的主要变化:

    • 需要 PHP 5.5 或更高版本(从 5.0 开始)
    • 使用命名空间
    • 类文件名和位置已更改
    • 已删除其他不相关的“附加”类

    您可以在the changelogthe official upgrade guide 中了解许多其他较小的变化,但这些最有可能影响到您。

    通过作曲家升级

    要通过 composer 升级,请更改 composer.json 文件的 require 部分中的条目,然后运行 ​​composer update phpmailer/phpmailer

    "phpmailer/phpmailer": "~6.0"
    

    这将只更新 PHPMailer,不会触及任何其他依赖。

    PHPMailer 使用semver 版本编号策略,该模式将匹配 6.x 系列中的所有未来版本。这与之前推荐的 ~5.2 模式有所不同。

    加载类文件

    对于给出的示例脚本,我们主要需要更改类的加载方式。自动加载器不再存在,因此您要么需要使用 composer(在这种情况下,您不需要更改任何内容 - 标准的 composer 自动加载器会自动完成),或者您需要自己加载类。

    与作曲家:

    require 'vendor/autoload.php';
    

    没有作曲家:

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

    命名空间

    PHPMailer 类位于 PHPMailer\PHPMailer 命名空间中,因此您要么需要在该命名空间中工作,要么将它们导入您自己的命名空间或全局命名空间,例如:

    //Import PHPMailer classes into the global namespace
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    

    请注意,这些必须放在require 行之前。之后,您可以使用您习惯的原始类名:

    $mail = new PHPMailer;
    

    或者,您可以直接引用它们的完全限定名称,而不使用 use 语句,例如:

    $mail = new PHPMailer\PHPMailer\PHPMailer;
    

    这个类以这个“三重名称”结束的原因是因为它是 PHPMailer 项目中的 PHPMailer 类,属于 PHPMailer 组织。这使它能够与 PHPMailer 的其他分支、PHPMailer 组织的其他项目以及项目中的其他类区分开来。

    例外情况

    除了名称从phpmailerException 更改之外,异常的工作方式与以前的版本相同,但在捕获时需要注意命名空间:

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    
    $mail = new PHPMailer(true);
    try {
        ...
    } catch Exception($e) {
        //$e is an instance of PHPMailer\PHPMailer\Exception
    } catch \Exception ($e) {
        //$e is an instance of the PHP built-in Exception class
    }
    

    文档

    所有文档和示例代码也已针对 6.0 进行了更新。最好的起点是the readme filethe project wiki,您可以在其中找到广受欢迎的troubleshooting guide、大量教程和generated API documentation 的链接。如果您刚刚开始,请将您的代码基于the examples folder 中提供的示例。

    获得帮助

    如果您在使用 PHPMailer 时遇到问题,首先在 Stack Overflow 上搜索您的特定错误消息并在 the PHPMailer tag 下搜索。如果您认为在 PHPMailer 中发现了错误,请在 the github project 上报告(提示 - 无法从您的 GoDaddy 服务器连接到邮件服务器不是 PHPMailer 错误!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-03
      • 2011-12-08
      • 2015-07-14
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多