使用插件實現太容易了,但是我們想讓自己的博客飛起來就少用插件吧。
不使用插件提供通知訪客其評論有回覆的前提條件是服務器開啓了 mail() 函數,linux 的虛擬主機一般都開啓了的,沒有開啓需要找客服幫忙。如果是 linux VPS 可以自己開啓,方法如下。
一、服務器已經開啓了 mail() 函數的情況下 (未開啓請看第二大段):
進入 WordPress 後台 「外觀」—->「編輯」—-> 右邊找到 「functions.php」—-> 在
下一行回車,空出一行後,然後粘貼以下代碼,並保存即可:
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME']));//發件人 e-mail 地址
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 ['.get_option("blogname").'] 的留言有了回覆';
$message = '
<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
<p>'.trim(get_comment($parent_id)->comment_author).', 您好!</p>
<p> 這是您在 《'.get_the_title($comment->comment_post_ID).'》 中的留言:<br />'
.trim(get_comment($parent_id)->comment_content).'</p>
<p> 以下是'.trim($comment->comment_author).' 給您的回覆:<br />'
.trim($comment->comment_content).'<br /></p>
<p> 您可以<a href="' . htmlspecialchars(get_comment_link($parent_id)) . '"> 點擊這裏查看回復的完整內容.</a></p>
<p><a href="' . get_option('home') . '">' . get_option('blogname') . '</a> 內容已經更新,歡迎再度光臨!</p>
<p>(注: 此為系統郵件, 請勿回覆!)</p>
</div>';
$from = "From: "" . get_option('blogname') . "" <$wp_email>";
$headers = "$from
Content-Type: text/html; charset=" . get_option('blog_charset') . "
";
wp_mail( $to, $subject, $message, $headers );
//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
二、 linux 服務器開啓 mail() 函數的方法
先是 ssh 連接上 VPS,然後輸入以下命令
在命令模式,使用 VI 的查找命令 「/」 進行內容查找:
如果第一次沒有查找到,請再次輸入/mail 查找,直到找到 [mail function]
位置,會看看到以下代碼:
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
然後將 sendmail_path =修改為:
然後重啓 php-fpm 進程
以上步驟完了過後如果發現還不行,那可能是安裝或啓動 sendmail 組件,那麼按以下步驟重新安裝:
安裝組件:
重啓進程
檢查運行情況
如果現實 running 則安裝運行成功。
sendmail 啓動停止重啓命令,根據需要選擇後面的 star 、 stop 、 restart