比較實用的WordPress評論郵件自動通知功能,主要的目的在于提高回訪性,如果有朋友去你的網站咨詢問題,但是過后他可能忘記這回事,有可能就不了了之了,這時候有郵件回復通知就不會讓他錯過了.
實現評論郵件自動通知的功能只要把以下代碼添加到functions.php即可,此方法僅限于Linux主機使用,代碼如下:
- //評論郵件自動通知
- 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_(www.111cn.net)content) . '</p>
- <p style="color:#f00">您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回復的完整?熱?lt;/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 = "$fromnContent-Type: text/html; charset=" . get_option('blog_charset') . "n";
- wp_mail( $to, $subject, $message, $headers );
- }
- }
- add_action('comment_post', 'comment_mail_notify');
如果系統不支持mail函數我們需要要安裝一個SMTP的插件,下載好Configure SMTP插件、通過SVN上傳到SAE博客上,登陸后臺啟用插件并開始配置、請仔細看下面的配置選項.
Send e-mail via GMail? 不用勾選
SMTP host:smtp.163.com,俺使用的偉大的163郵箱(推薦使用國內的郵箱,否則有可能收不到郵件)
SMTP port:25,一般SMTP服務器都是使用的這個端口
Secure connection prefix:留白、不用選
Use SMTPAuth? 必選
SMTP username:你的用戶名,比如安的是[email protected] SMTP password:郵箱密碼,123456789
Wordwrap length:留白
Enable debugging? 啟用調試模式,不選
Sender e-mail:發送者郵箱,還寫上面的[email protected]
Sender name:發送者的姓名,把你希望顯示的名稱填上即可.
保存之后我們自己測試一把去,點擊下面的 Send Test e-mail 按鈕,到此你的SAE上的wordpress博客在有評論的時候都會發送郵件給你,然后如果你在回復其他人的評論的時候也會有郵件過去,當然如果新用戶注冊的時候也會有郵件通知的.
新聞熱點
疑難解答
圖片精選