我应该如何从PHPMailer 5.2升级到6.0? [英] How should I upgrade from PHPMailer 5.2 to 6.0?

查看:72
本文介绍了我应该如何从PHPMailer 5.2升级到6.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本当前使用最新版本的PHPMailer5.2.x. PHPMailer 6.0已发布,但表示将破坏向后兼容性–升级需要做些什么?

I have a script that currently uses a recent version of PHPMailer 5.2.x. PHPMailer 6.0 has been released, but says it will break backward compatibility – what do I need to do to upgrade?

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

推荐答案

PHPMailer 6.0:

  • 需要PHP 5.5或更高版本(从5.0开始)
  • 使用名称空间
  • 班级文件名和位置已更改
  • 其他不相关的额外"类已被删除

还有许多其他较小的更改,您可以在更改日志中阅读,以及官方升级指南,但这些都是最有可能影响您的人.

There are many other smaller changes which you can read about in the changelog, and also the official upgrade guide, but these are the ones most likely to affect you.

要通过作曲家升级,请更改composer.json文件的require部分中的条目,然后运行composer update:

To upgrade via composer, change the entry in the require section of your composer.json file, and then run composer update:

"phpmailer/phpmailer": "~6.0"

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

PHPMailer uses a semver release numbering policy, and that pattern will match all future releases in the 6.x series. This is a change from the previously recommended ~5.2 pattern.

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

For the example script given, we mainly need to change how the class is loaded. The autoloader is no longer there, so you either need to be using composer (in which case you won't need to change anything - the standard composer autoloader will do it automatically), or you need to load the classes yourself.

使用作曲家:

require 'vendor/autoload.php';

没有作曲家:

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

名称间隔

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

Namespacing

The PHPMailer classes are in the PHPMailer\PHPMailer namespace, so you either need to work in that namespace, or import them into your own or the global namespace, for example:

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

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

Note that these must be placed before the require lines. After that you can use the original class names you're used to:

$mail = new PHPMailer;

或者,您可以直接引用其标准名称,而无需use语句,例如:

Alternatively, you can refer to their fully-qualified names directly, without the use statements, for example:

$mail = new PHPMailer\PHPMailer\PHPMailer;

此类之所以以三重名称"结尾是因为它是PHPMailer组织拥有的PHPMailer项目中的PHPMailer类.这样就可以将它与PHPMailer的其他分支,PHPMailer组织的其他项目以及项目中的其他类区分开来.

The reason this class ends up with this "triple name" is because it's the PHPMailer class, in the PHPMailer project, owned by the PHPMailer organisation. This allows it to be differentiated from other forks of PHPMailer, other projects by the PHPMailer organisation, and other classes within the project.

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

Other than the name change from phpmailerException, exceptions work the same way as in previous versions, but you need to look out for the namespace when catching:

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.最好的开始位置是自述文件疑难解答指南,大量教程和生成的API文档.如果您只是入门,请基于 examples文件夹.

Documentation

All documentation and example code has been updated for 6.0 too. The best place to start is the readme file or the project wiki, where you will find links to the ever-popular troubleshooting guide, numerous tutorials, and generated API documentation. If you're just starting out, base your code on the examples provided in the examples folder.

如果您在使用 PHPMailer时遇到问题,请首先在Stack Overflow上搜索您的特定错误消息,并在 github项目中进行报告(提示-正在无法从GoDaddy服务器连接到邮件服务器不是PHPMailer错误!)

If you have a problem using PHPMailer, first of all search on Stack Overflow for your specific error message and under the PHPMailer tag. If you think you have found a bug in PHPMailer, report it on the github project (hint - being unable to connect to mail servers from your GoDaddy server is not a PHPMailer bug!)

这篇关于我应该如何从PHPMailer 5.2升级到6.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆