Dokuwiki_logo

在dokuwiki中使用PHPMailer发送邮件

doku wiki是一款轻量级的wiki软件,它以文件形式保存资料,不依赖传统数据库,部署简单方便,并支持多国语言,使用简单的语法,就能编排出一份很好的文档。

DokuWiki 是一个使用,用途多样的开源 Wiki 软件,并且不需要数据库。它因简洁易读的语法受到用户的喜爱。而容易维护、备份和整合则使它成为管理员的最爱。内置的访问控制列表和认证连接器使 DokuWiki 在企业环境下特别有用,由充满活力的社区贡献的众多插件则令它拥有比传统维基更广阔的应用范围。

doku wiki默认使用php内置函数mail()发送邮件,理论上来说,只要linux服务器安装了sendmail,且php.ini中配置了endmail_path = /usr/sbin/sendmail -t -i,就可以直接发送邮件,但由于接收服务器的安全策略,mail()发送出去的“三无”邮件,往往会延时很久或者根本无法被正常投递。
鉴于此,我决定使用PHPMailer来替换mail()发送邮件。

– Probably the world’s most popular code for sending email from PHP!
– Used by many open-source projects: Drupal, SugarCRM, Yii, Joomla! and many more
– Integrated SMTP support – send without a local mail server
– Send emails with multiple TOs, CCs, BCCs and REPLY-TOs
– Multipart/alternative emails for mail clients that do not read HTML email
– Support for UTF-8 content and 8bit, base64, binary, and quoted-printable encodings
– SMTP authentication with LOGIN, PLAIN, NTLM and CRAM-MD5 mechanisms over SSL and TLS transports

1.修改conf/local.php 添加SMTP配置参数
以QQ邮箱为例,具体配置请参考QQ邮箱帮助中心

<?php
$conf['smtp']['host'] = 'smtp.qq.com'; //smtp server
$conf['smtp']['port'] = 465;//smtp port
$conf['smtp']['username'] = 'xxxxxxx@qq.com';//smtp user name
$conf['smtp']['password'] = 'yourpassword';//smtp password
$conf['smtp']['auth'] = true;//smtp authentication
$conf['smtp']['secure'] = 'ssl';//or tls
$conf['smtp']['from'] = 'xxxxxxx@qq.com';
$conf['smtp']['fromname'] = 'yourname';

 

2.下载PHPMailer 解压重命名为PHPMailer后放到inc目录下

3.在inc/Mailer.class.php最底部添加方法sendmail()

<?php
/**
* Send mail via PHPMailer
* @param $to string,reciever
* @param $subject string subject
* @param $body mail body
* @param $header string extra header
* @return bool
*/
public static function sendmail($to,$subject,$body,$header,$params = null){
        global $conf;//from conf/local.php
        require_once(DOKU_INC.'inc/PHPMailer/PHPMailerAutoload.php');
        $mail = new PHPMailer;

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = $conf['smtp']['host'];  // Specify main and backup SMTP servers
        $mail->Port = $conf['smtp']['port'];  // Specify main and backup SMTP servers port
        $mail->SMTPAuth = $conf['smtp']['auth'];                               // Enable SMTP authentication
        $mail->Username = $conf['smtp']['username'];                 // SMTP username
        $mail->Password = $conf['smtp']['password'];                           // SMTP password
        $mail->SMTPSecure = $conf['smtp']['secure'];                            // Enable encryption, 'ssl' also accepted

        $mail->From = $conf['smtp']['from'];
        $mail->FromName = $conf['smtp']['fromname'];
        $to = preg_match('/<(?P.*)>/i',$to,$matches)?$matches['email']:$to;//grep email from 'allen<allen@gmail.com>'
        $mail->addAddress($to);
        //$mail->addReplyTo('info@example.com', 'Information');
        //$mail->addCC('cc@example.com');
        //$mail->addBCC('bcc@example.com');

        $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
        //$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 = $subject;
        $mail->Body    = $body;
        //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        return $mail->send();
}

 

4.修改inc/Mailer.class.php 文件 send()方法把@mail($to, $subject, $body, $headers)函数为

<?php
self::sendmail($to,$subject,$this->text,$headers);

=========================== Attention =================================
smtp ssl安全认证需要openssl的支持,请确认你的linux机器上安装了openssl,并且php.ini中开启了openssl.so模块
如果未安装openssl,请按以下步骤操作:
1.安装openssl
sudo yum -y install openssl
2.安装php-openssl模块
==yum 安装== sudo yum -y install php-openssl
===编译安装====
a.)下载对应版本的php源码,解压
b.)cd php-master/ext/openssl && mv config0.m4 config.m4
c.)phpize
d.)./configure –with-openssl-dir=/usr/sbin/openssl/ –with-php-config=/usr/bin/php-config #如果php-config也没有,请先安装php-devel
e.)make && make install
f.)php.ini中添加extention = openssl.so;

安装完成后,别忘了重启web服务器使配置生效!

%1 $ S

发表回复