目前的另外一個項目“麥客航班”,因為目前服務器托管在江蘇,並且居然裝的系統是Windows 2003,很讓我無奈,因為我們架設的是Wordpress程序,wordpress在缺省無sendmail等UNIX下的郵件服務器時,是無法發送郵件的,諸如用戶注冊的時候,諸如使用Mail To Commenter插件的時候都需要發送郵件。這時候搜索就是您最好的老師了。果不然,google了一下,我已經有解決方案了。Follow me!
1.在/wp-includes/目錄下新建立mail.inc.php文件。文檔內容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php require("class-phpmailer.php"); class MyMailer extends PHPMailer { // Set default variables for all new objects var $Mailer = "smtp"; // Alternative to IsSMTP() var $CharSet = "utf-8"; var $From = "你的郵件地址"; var $FromName = "想叫啥就叫啥"; var $Host = "smtp服务器地址"; var $Port = 25; //smtp server port var $SMTPAuth = true; var $Username = "你邮件的帐号"; var $Password = "你邮件的密码"; //var $SMTPDebug = true; var $WordWrap = 75; } ?> |
2.打開/wp-includes/pluggable.php,查找到function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() )
,在global $phpmailer;
之前添加如下代碼
1 2 3 4 5 6 7 8 9 | require("mail.inc.php"); $mail = new MyMailer; $mail->AddAddress($to); $mail->Subject = $subject; $mail->Body = $message; return $mail->Send(); |
3.繼續在此文檔中查找到wp_new_user_notification函數,修改其中的一行代碼:
把
wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
修改成
@wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
4.在文檔結尾?>前添加如下代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 | if ( !function_exists('wp_mail_attachment') ) : function wp_mail_attachment($to, $subject, $message, $string, $filename, $encoding, $type) { require("mail.inc.php"); $mail = new MyMailer; $mail->AddAddress($to); $mail->Subject = $subject; $mail->Body = $message; $mail->AddStringAttachment($string, $filename, $encoding, $type); return $mail->Send(); } endif; |
OK,大功告成。現在可以使用非SSL SMTP Server(比如126)發送郵件了。
以上方法在Wordpress 2.7中測試通過。
代碼由CSDN這位兄弟提供。過完年,我一定得把服務器裝成Linux!
Pingback: 创造 » 让架设在Windows上的Wordpress也能正常发邮件(Smtp)!()