phplist与phpmailer Gmail配置概述
本实例详解了将phplist与phpmailer结合,利用Gmail账户发送邮件的具体配置步骤,供读者参考实践。
通常若不使用Gmail邮箱,参照《PHP的邮件群发系统phplist配置方法详细总结》即可完成配置。但若必须使用需SSL验证的Gmail邮箱,则需额外处理。经过多次尝试,成功实现了phplist与Gmail的组合,现将经验整理分享,同时也适用于其他希望使用phpmailer通过Gmail发送邮件但遭遇失败的用户,因为phplist的核心组件正是phpmailer。
首先依据前述文章的方法进行基础配置,但在测试邮件发送时,phplist会提示发送失败,事件日志(eventlog)中显示“Mailer Error: The following From address failed:...”,指出发件人地址有误。为确认是否已连接至SMTP服务器,可故意在config.php中填错密码进行测试,若仍报相同错误,说明根本未建立连接,而非发送过程中的问题。
既然未能连接SMTP服务器,问题根源指向phplist发送邮件的核心组件——另一款知名开源软件phpmailer。
查阅资料发现,旧版phpmailer不支持SSL验证,无法连接Gmail的SMTP服务器,而该问题已在较新版本中得到修复。
查看lists/admin/phpmailer/ChangeLog.txt可知,当前phplist自带的phpmailer版本为1.73,发布于2005年,确实较为陈旧。因此,从phpmailer官网下载了最新的5.1版本进行替换。
为探究新版如何解决SSL验证问题,查阅了其自带文档,发现在PHPMailer_v5.1/docs目录下存在use_gmail.txt文件,这是官方提供的Gmail使用示例。该文件是一个完整的PHP页面,理论上修改扩展名、用户名和密码即可运行。但直接修改后访问测试页面仍会报错,原因是调用了未定义的函数且包含多余代码。为了仅测试邮件发送功能,对其进行了简化修改:
<?php
// example on using PHPMailer with GMAIL
include("class.phpmailer.php");
include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded
$mail = new PHPMailer();
$body = "test";
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = "myname@gmail.com"; // GMAIL username
$mail->Password = "mypassword"; // GMAIL password
$mail->From = "myname@gmail.com";
$mail->FromName = "Webmaster";
$mail->Subject = "This is the subject";
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddReplyTo("myname@gmail.com","Webmaster");
$mail->AddAddress("myname@gmail.com","First Last");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
然而,访问此简化后的页面依然报错,令人困惑,为何官方提供的demo无法正常运作?
此时想起PHPMailer_v5.1/docs目录下名为Note_for_SMTP_debugging.txt的文件,鉴于当前正面临SMTP连接难题,不妨查看其中提供的调试方法。
阅读该文件第一行即发现这正是所需方案!使用方法十分简便,只需在
$mail->IsSMTP();
前插入
$mail->SMTPDebug = 1;
便可在报错时获取更详尽的错误信息。这确实是个实用的工具^_^
按照上述方式修改后,再次访问页面获得了更详细的错误说明:“SMTP -> ERROR: Failed to connect to server: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? (28593608)”。
问题明朗了,随即打开PHP配置文件(C://Windows/php.ini)搜索ssl,果然找到了关于ssl的扩展配置项。
;extension=php_openssl.dll
但该扩展处于被注释状态。去除其前的注释符号“;”,重启服务器后,再次访问测试页面use_gmail.php,错误提示依旧。
无奈之下,上网搜索PHP及Apache的SSL配置相关文章,得知仅开启ssl扩展模块尚不足够,还需配置openssl。在Windows环境下,配置相对简单——找到php安装目录下的ssleay32.dll和libeay32.dll,将这两个文件复制到windows/system32目录中即可(当然,在php.ini中开启extension=php_openssl.dll也是必要的)。若不愿“污染”system32目录,也可通过修改环境变量,确保ssleay32.dll和libeay32.dll位于系统路径下。(非Windows用户请查找对应系统的SSL配置方法,通常不难找到)。
此次再访问use_gmail.php,发现邮件发送成功了!
基于此成果,phplist的问题也随之解决:使用新版phpmailer中的class.phpmailer.php和class.smtp.php覆盖lists/admin/phpmailer目录下的同名文件,然后修改lists/admin/class.phplistmailer.php中第36行左右的代码:
$this->SMTPAuth = true;
$this->Helo = getConfig("website");
$this->Host = PHPMAILERHOST;
将其修改为:
$this->IsSMTP(); # Add
$this->SMTPAuth = true;
$this->SMTPSecure = "ssl"; # Add
$this->Helo = getConfig("website");
$this->Host = PHPMAILERHOST;
$this->Port = 465 # Add
phpmailer默认端口号为25,这是大多数SMTP服务器的标准端口,但Gmail使用的端口号为465,因此需要重新设置端口值。
希望本文所述内容能对大家的PHP程序设计有所帮助。