phplist(及phpmailer)通过gmail发送邮件的配置方法

来源:互联网 发布:24周大排畸数据看男女 编辑:程序博客网 时间:2024/05/16 07:02
phplist(及phpmailer)通过gmail发送邮件的配置方法
一般来说,只要你使用的不是gmail邮箱,那么利用phplist发送邮件只要按照《邮件群发系统phplist的配置方法总结》配 置就够了。但若你如同我一样不幸,必须使用gmail这种有ssl验证的邮箱,那么恭喜你,我的不幸现在已然成为你的幸运,经过数天的尝试,我终于成功将 gmail与phplist组合在了一起。现将经验分享于此,希望对各位同我一般境遇的同志有用。另外,phplist的核心是phpmailer,我提 出的解决方案也主要是围绕phpmailer的,所以需要使用phpmailer通过gmail发送邮件而不能成功者也可以参考我的方法。
首先按照《邮件群发系统phplist的配置方法总结》中 的配置方法通过gmail发送邮件,在发送测试邮件时phplist会报告发送邮件失败,在事件日志(eventlog)里会有错误提示“Mailer Error: The following From address failed:...”,说是发件人地址存在问题。难道是已经连上smtp服务器,但是发送邮件过程中存在问题吗?可以用一个方法试验一下到底连没连上 smtp服务器:我把config.php文件中的邮箱帐户密码故意填错,结果发送测试邮件时仍然报同样的错误,看来是根本就没连上smtp服务器,这 phplist的错误报告也太……
知道是没连上smtp服务器那就说明问题出现在phplist发送邮件的核心——另一款著名开源软件phpmailer。
上网查了一下phpmailer发送gmail邮件的资料,发现人们说旧版本的phpmailer不支持ssl验证,不能连接gmail的smtp服务器,而此问题已在新版的phpmailer中解决了。
打开lists/admin/phpmailer/ChangeLog.txt,发现最新版的phplist自带的phpmailer的版本是1.73,是2005年出的,确实不算新。于是上phpmailer的官网下了个最新的5.1的。
我想先研究一下新版的phpmailer是如何解决ssl验证的问题的,于是看了一下其自带的一些说明文档,碰巧发现在 PHPMailer_v5.1/docs下有一个use_gmail.txt,看来是官方比较重视gmail问题,专门出了一个demo供人参考。打开一 看也确实是一个完整的php页面文件,基本上修改了文件扩展名、邮箱用户名和密码就能使用,但如果仅仅如此修改,在访问该测试页面时会报错,也不知官方出 的demo怎么会有这样的错误,居然会调用一个未定义的函数,而且有一些没有必要的成分。我们只不过想先测试一下能否正常发送邮件,所以我将其修改为:

    

view plaincopy to clipboardprint?
  1. <?php  
  2.   
  3.         // example on using PHPMailer with GMAIL  
  4.   
  5.         include("class.phpmailer.php");  
  6.         include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded  
  7.   
  8.         $mail             = new PHPMailer();  
  9.   
  10.         $body             = "test";  
  11.   
  12.         $mail->IsSMTP();  
  13.         $mail->SMTPAuth   = true;                  // enable SMTP authentication  
  14.         $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier  
  15.         $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server  
  16.         $mail->Port       = 465;                   // set the SMTP port  
  17.   
  18.         $mail->Username   = "myname@gmail.com";  // GMAIL username  
  19.         $mail->Password   = "mypassword";            // GMAIL password  
  20.   
  21.         $mail->From       = "myname@gmail.com";  
  22.         $mail->FromName   = "Webmaster";  
  23.         $mail->Subject    = "This is the subject";  
  24.         $mail->AltBody    = "This is the body when user views in plain text format"//Text Body  
  25.         $mail->WordWrap   = 50; // set word wrap  
  26.   
  27.         $mail->MsgHTML($body);  
  28.   
  29.         $mail->AddReplyTo("myname@gmail.com","Webmaster");  
  30.   
  31.         $mail->AddAddress("myname@gmail.com","First Last");  
  32.   
  33.         $mail->IsHTML(true); // send as HTML  
  34.   
  35.         if(!$mail->Send()) {  
  36.           echo "Mailer Error: " . $mail->ErrorInfo;  
  37.         } else {  
  38.           echo "Message has been sent";  
  39.         }  
  40.   
  41. ?>  
<?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在系统路径下就可以了。(如果你 使用的不是winidows操作系统,请上网查找针对你的操作系统的配置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,所以要重新设置。


原创粉丝点击