欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-11 20:39:40
本文实例讲述了phplist及phpmailer通过gmail发送邮件的配置方法。分享给大家供大家参考,具体如下: 一般来说,只要你使用的不是gmail邮箱,那么利用ph...

本文实例讲述了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";
    }
?>

结果发现访问此页面时仍然报错,真是令人无奈,官方给的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,所以要重新设置。

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

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