本站一直致力於 WordPress 主題下載WordPress 基礎教程的分享釋出,感謝大家多日來的支援和鼓勵。今天說到的這個 WordPress 評論郵件提醒和 WordPress 郵件傳送失敗的解決方案,原教程來自小編的好友兼友情連結使用者 CREEKOO 的,瞭解更多 CREEKOO 的朋友請到友情連結處訪問,感謝 CREEKOO 的分享!

我前些天製作的一款簡潔類的 WordPress 部落格主題 Laconic 分享給大家後,很多人問我,我站裡的 WordPress 評論郵件提醒功能是這麼實現的,當時我很簡單的回覆下程式碼實現,可之後發現很多朋友都有這樣的問題,所以今天小編起草了這份教程,供大家使用,望能給大家在 WordPress 使用方面帶來方便,也歡迎大家收藏我們,多多關注我們!

下面先說 WordPress 評論郵件提醒功能的實現方法吧!

其實方法很簡單,在 WordPress 主題根目錄下找到 functions.php 檔案,用專門的編寫程式碼軟體將下面的程式碼加入進去

  1. function comment_mail_notify($comment_id) {
  2.   $admin_email = get_bloginfo ('admin_email');
  3.   $comment = get_comment($comment_id);
  4.   $comment_author_email = trim($comment->comment_author_email);
  5.   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  6.   $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  7.   $spam_confirmed = $comment->comment_approved;
  8.   if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
  9.     $wp_email = 'no-reply@' . preg_replace('#^www.#', ''strtolower($_SERVER['SERVER_NAME']));
  10.     $subject = '您在 [' . get_option("blogname") . '] 的評論有新的回覆';
  11.     $message = '
  12.     <div style="font: 13px Microsoft Yahei;padding: 0px 20px 0px 20px;border: #ccc 1px solid;border-left-width: 4px; max-width: 600px;margin-left: auto;margin-right: auto;">
  13.       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  14.       <p> 您曾在 [' . get_option("blogname") . '] 的文章 《' . get_the_title($comment->comment_post_ID) . '》 上發表評論:<br />'
  15.        . nl2br(get_comment($parent_id)->comment_content) . '</p>
  16.       <p>' . trim($comment->comment_author) . ' 給您的回覆如下:<br>'
  17.        . nl2br($comment->comment_content) . '</p>
  18.       <p style="color:#f00"> 您可以點選 <a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '"> 檢視回復的完整內容</a></p>
  19.       <p style="color:#f00"> 歡迎再次光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
  20.       <p style="color:#999">(此郵件由系統自動發出,請勿回覆。)</p>
  21.     </div>';
  22.     $message = convert_smilies($message);
  23.     $from = "From: "" . get_option('blogname') . "" <$wp_email>";
  24.     $headers = "$from
    Content-Type: text/html; charset="
     . get_option('blog_charset') . "
    "
    ;
  25.     wp_mail( $to$subject$message$headers );
  26.   }
  27. }
  28. add_action('comment_post', 'comment_mail_notify');

透過以上程式碼即可實現 WordPress 評論郵件提醒功能,腫麼樣,是否很簡單,免去了這個外掛那個外掛的使用了?

但是從這個教程中,會衍生出另一個問題,那就是很多 win 主機還是傳送不了郵件,或者 linux 的主機發郵件過於緩慢,均會造成評論速度變慢的問題,那麼如何解決 WordPress 郵件傳送失敗的問題呢?那麼就繼續看教程吧

方法很簡單,那就是用程式碼的方法將 WordPress 自帶傳送郵件的功能用外部郵箱的 SMTP 來實現!這樣的方法好處是評論速度快,且 win 主機和 linux 主機均可以快速發出郵件,從讓解決上面那個教程帶來的 WordPress 郵件傳送失敗的問題!

那麼教程同樣是在在 WordPress 主題根目錄下找到 functions.php 檔案,加入下面的程式碼

  1. add_action('phpmailer_init', 'mail_smtp');
  2. function mail_smtp( $phpmailer ) {
  3. $phpmailer->FromName = 'CreeKoo'; 
  4. $phpmailer->Host = 'smtp.qq.com'; 
  5. $phpmailer->Port = 25; 
  6. $phpmailer->Username = '****@qq.com'; 
  7. $phpmailer->Password = '*********'; 
  8. $phpmailer->From = '*****@qq.com'; 
  9. $phpmailer->SMTPAuth = true;
  10. $phpmailer->SMTPSecure = ''
  11. $phpmailer->IsSMTP();
  12. }

將程式碼裡的資訊替換成自己的即可,這裡要說到的是郵箱的 SMTP 埠有 25 或者是 465,大家可以試試。。。
教程就到這了。。需要的朋友可以嘗試下哈
如果看不懂此教程的朋友可以加入我們的 WordPress 技術交流群,群號文章的下方藍色文字有!也可以找小編我付費輔助!哈哈

2013/8/31 更新

這裡要再特殊的說一下,在 lnmp 環境下,無論你是安裝了 sendmail 元件還是 Shell 下的 mail 命令都 OK 的情況下,還是不能傳送郵件的話,那麼就需要到 php.ini 檔案裡修改配置了,

軍哥的 lnmp 的 php.ini 檔案在/usr/local/php/etc/php.ini  查詢到 sendmail_path 修改為:sendmail_path = /usr/sbin/sendmail -t -i 或者開放 pfsockopen 、 fsockopen 兩個函式即可 OK!!!