Syn*_*hro 5 php email phpmailer
我有一个脚本,目前使用最新版本的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';
}
Run Code Online (Sandbox Code Playgroud)
PHPMailer 6.0中发生的主要变化:
您可以在更改日志中看到许多其他较小的更改,以及官方升级指南,但这些更改最有可能影响您.
要通过composer升级,请更改文件require部分中 的条目composer.json,然后运行composer update:
"phpmailer/phpmailer": "~6.0"
Run Code Online (Sandbox Code Playgroud)
PHPMailer使用semver版本编号策略,该模式将匹配6.x系列中的所有未来版本.这是对先前推荐的~5.2模式的更改.
对于给出的示例脚本,我们主要需要更改类的加载方式.自动加载器不再存在,因此您需要使用composer(在这种情况下,您不需要更改任何内容 - 标准编写器自动加载器将自动执行),或者您需要自己加载类.
有作曲家:
require 'vendor/autoload.php';
Run Code Online (Sandbox Code Playgroud)
没有作曲家:
require 'src/PHPMailer.php';
require 'src/SMTP.php';
require 'src/Exception.php';
Run Code Online (Sandbox Code Playgroud)
PHPMailer类位于PHPMailer\PHPMailer命名空间中,因此您需要在该命名空间中工作,或者将它们导入到您自己的命名空间或全局命名空间中,例如:
//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
Run Code Online (Sandbox Code Playgroud)
请注意,这些必须放在require行之前.之后,您可以使用您习惯的原始类名:
$mail = new PHPMailer;
Run Code Online (Sandbox Code Playgroud)
或者,您可以直接引用其完全限定名称,而不使用use语句,例如:
$mail = new PHPMailer\PHPMailer\PHPMailer;
Run Code Online (Sandbox Code Playgroud)
这个类最终得到这个"三重名称"的原因是因为它是由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
}
Run Code Online (Sandbox Code Playgroud)
所有文档和示例代码也已针对6.0进行了更新.最好的起点是自述文件或项目维基,您可以在其中找到指向常用故障排除指南,众多教程和生成的API文档的链接.如果您刚刚开始,请将您的代码基于示例文件夹中提供的示例.
如果您在使用 PHPMailer时遇到问题,请首先在Stack Overflow上搜索特定错误消息并在PHPMailer标记下搜索.如果你认为你在PHPMailer中发现了一个错误,请在github项目中报告它(提示 - 无法从你的GoDaddy服务器连接到邮件服务器不是PHPMailer错误!)