讓架設在Windows上的WordPress也能正常發郵件(Smtp)!

windows smtp send mail

目前的另外一個項目“麥客航班”,因為目前服務器托管在江蘇,並且居然裝的系統是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!

21 Responses to 讓架設在Windows上的WordPress也能正常發郵件(Smtp)!

  1. 颜小诗 says:

    well done 服务器还是linux或unix的好啊 windows的实在是很麻烦的说

    “過完年,我一定得把服務器裝成Linux!” 服务器是你自己在维护的啊?

  2. Leeiio says:

    @颜小诗:對的,不過win也其實還好,可以在服務器上拖電驢哈哈。

  3. xiaorsz says:

    用WIN是挺不爽的!!
    这种方法能成功,以前我试过好多都不行!!!

  4. Leeiio says:

    @xiaorsz:過完年就把系統收拾了 :mrgreen:

  5. plidezus says:

    发现win下wp老是出问题,今天就被tags问题搞了好久才搞定。
    而且发现反应速度明显变得很低……
    方法收鸟后面用的到哈哈~

  6. Leeiio says:

    @plidezus:php还是用linux+apache好阿~哎

  7. plidezus says:

    @Leeiio:服务器基本上都是别人帮我搞的,我的半吊子水平还不会搞这些,顶多就是做做界面设计之类的哈哈~

  8. Leeiio says:

    @plidezus:哈,以後多多交流~

  9. ptubuntu says:

    你可以使用google的gmail功能呀.就不必在自己的服务器上有mail server.??
    我现在自己的都是在使用这个.觉的很不错.

  10. Leeiio says:

    @ptubuntu 這裡指的是wordpress系統發送給用戶的郵件~

  11. Andrew says:

    2.8 英文,早期版本,,,这个方法不起作用,我用了国内的几个邮箱都不行,不过后来服务器被国安抱走了,我也死心了

  12. Leeiio says:

    @Andrew
    呃,你的经历异常强大!

  13. Pingback: 创造 » 让架设在Windows上的Wordpress也能正常发邮件(Smtp)!

  14. lee says:

    请问LEEIIO,回复用 @username ,并移动上去显示内容(即贵站的样子)用什么修改或插件? 请指教!

  15. lee says:

    有小白教程么 🙁

  16. lee says:

    谢谢 已经找到 reply-to 插件 🙂

Leave a Reply to plidezus Cancel reply

Your email address will not be published. Required fields are marked *