phplist及phpmailer(组合使用)通过gmail发送邮件的配置方法_php技巧

WBOY
Release: 2016-05-16 19:55:11
Original
1218 people have browsed it

本文实例讲述了phplist及phpmailer通过gmail发送邮件的配置方法。分享给大家供大家参考,具体如下:

一般来说,只要你使用的不是gmail邮箱,那么利用phplist发送邮件只要按照前面《PHP的邮件群发系统phplist配置方法详细总结》配置就够了。但若你如同我一样不幸,必须使用gmail这种有ssl验证的邮箱,那么恭喜你,我的不幸现在已然成为你的幸运,经过数天的尝试,我终于成功将gmail与phplist组合在了一起。现将经验分享于此,希望对各位同我一般境遇的同志有用。另外,phplist的核心是phpmailer,我提出的解决方案也主要是围绕phpmailer的,所以需要使用phpmailer通过gmail发送邮件而不能成功者也可以参考我的方法。

首先按照《PHP的邮件群发系统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怎么会有这样的错误,居然会调用一个未定义的函数,而且有一些没有必要的成分。我们只不过想先测试一下能否正常发送邮件,所以我将其修改为:

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

Copy after login

结果发现访问此页面时仍然报错,真是令人无奈,官方给的demo怎么会无法运行?

这时我忽然想起PHPMailer_v5.1/docs下有一个名为Note_for_SMTP_debugging.txt的文件,现在我不正是在为连不上smtp服务器而烦恼吗,不妨看一下里面提供的调试方法。

打开文件看完第一行就眼前一亮,这正是我所需要的!其实使用方法也很简单,只要在

$mail->IsSMTP();

Copy after login

前插入

$mail->SMTPDebug = 1;

Copy after login

便可在报错同时得到更见详细的错误信息。真是好东西^_^

按照这样修改完后,我在访问页面时得到了更加详细的说明——“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

Copy after login

它没有被打开。去掉其前面用于注释的“;”,然后重启服务器,再次访问测试页面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;

Copy after login

为:

$this->IsSMTP();            # Add
$this->SMTPAuth = true;
$this->SMTPSecure = "ssl";       # Add
$this->Helo = getConfig("website");
$this->Host = PHPMAILERHOST;
$this->Port = 465            # Add

Copy after login

其中phpmailer默认端口号为25,是大多数smtp服务器的端口号,但是gmail使用的端口号是465,所以要重新设置。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!