本站一直致力於 WordPress 主題下載和 WordPress 基礎教程的分享釋出,感謝大家多日來的支援和鼓勵。今天說到的這個 WordPress 評論郵件提醒和 WordPress 郵件傳送失敗的解決方案,原教程來自小編的好友兼友情連結使用者 CREEKOO 的,瞭解更多 CREEKOO 的朋友請到友情連結處訪問,感謝 CREEKOO 的分享!
我前些天製作的一款簡潔類的 WordPress 部落格主題 Laconic 分享給大家後,很多人問我,我站裡的 WordPress 評論郵件提醒功能是這麼實現的,當時我很簡單的回覆下程式碼實現,可之後發現很多朋友都有這樣的問題,所以今天小編起草了這份教程,供大家使用,望能給大家在 WordPress 使用方面帶來方便,也歡迎大家收藏我們,多多關注我們!
下面先說 WordPress 評論郵件提醒功能的實現方法吧!
其實方法很簡單,在 WordPress 主題根目錄下找到 functions.php 檔案,用專門的編寫程式碼軟體將下面的程式碼加入進去
- function comment_mail_notify($comment_id) {
- $admin_email = get_bloginfo ('admin_email');
- $comment = get_comment($comment_id);
- $comment_author_email = trim($comment->comment_author_email);
- $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
- $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
- $spam_confirmed = $comment->comment_approved;
- if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
- $wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME']));
- $subject = '您在 [' . get_option("blogname") . '] 的評論有新的回覆';
- $message = '
- <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;">
- <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
- <p> 您曾在 [' . get_option("blogname") . '] 的文章 《' . get_the_title($comment->comment_post_ID) . '》 上發表評論:<br />'
- . nl2br(get_comment($parent_id)->comment_content) . '</p>
- <p>' . trim($comment->comment_author) . ' 給您的回覆如下:<br>'
- . nl2br($comment->comment_content) . '</p>
- <p style="color:#f00"> 您可以點選 <a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '"> 檢視回復的完整內容</a></p>
- <p style="color:#f00"> 歡迎再次光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
- <p style="color:#999">(此郵件由系統自動發出,請勿回覆。)</p>
- </div>';
- $message = convert_smilies($message);
- $from = "From: "" . get_option('blogname') . "" <$wp_email>";
- $headers = "$from
Content-Type: text/html; charset=" . get_option('blog_charset') . "
"; - wp_mail( $to, $subject, $message, $headers );
- }
- }
- add_action('comment_post', 'comment_mail_notify');
透過以上程式碼即可實現 WordPress 評論郵件提醒功能,腫麼樣,是否很簡單,免去了這個外掛那個外掛的使用了?
但是從這個教程中,會衍生出另一個問題,那就是很多 win 主機還是傳送不了郵件,或者 linux 的主機發郵件過於緩慢,均會造成評論速度變慢的問題,那麼如何解決 WordPress 郵件傳送失敗的問題呢?那麼就繼續看教程吧
方法很簡單,那就是用程式碼的方法將 WordPress 自帶傳送郵件的功能用外部郵箱的 SMTP 來實現!這樣的方法好處是評論速度快,且 win 主機和 linux 主機均可以快速發出郵件,從讓解決上面那個教程帶來的 WordPress 郵件傳送失敗的問題!
那麼教程同樣是在在 WordPress 主題根目錄下找到 functions.php 檔案,加入下面的程式碼
- add_action('phpmailer_init', 'mail_smtp');
- function mail_smtp( $phpmailer ) {
- $phpmailer->FromName = 'CreeKoo';
- $phpmailer->Host = 'smtp.qq.com';
- $phpmailer->Port = 25;
- $phpmailer->Username = '****@qq.com';
- $phpmailer->Password = '*********';
- $phpmailer->From = '*****@qq.com';
- $phpmailer->SMTPAuth = true;
- $phpmailer->SMTPSecure = '';
- $phpmailer->IsSMTP();
- }
將程式碼裡的資訊替換成自己的即可,這裡要說到的是郵箱的 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!!!