通常在 WordPress 站點開發中,我們為了增強網站使用者的粘度會開啟評論系統,但是管理員回覆了訪客的評論,訪客卻不能立即知道,這樣就降低了網站開啟評論系統的效果。今天小編就來教大家如何來給空間不支援 mai() 函式的 WordPress 站點增加評論回覆郵件提醒的方法,為什麼說是給空間不支援 mai() 函式的 WordPress 站點增加評論回覆郵件提醒的方法呢?因為小編這次是使用 SMTP 的方式來發郵件的, 下面上程式碼:

一、讓 WordPress 支援 SMTP 方式來傳送郵件。

//smtp
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '薇曉朵 (v7v3)'; //發信人名稱
$phpmailer->Host = 'smtp.exmail.qq.com'; //smtp 伺服器
$phpmailer->Port = 465;  //埠
$phpmailer->Username = 'admin@weixiaoduo.com';//郵箱帳號
$phpmailer->Password = '********';//郵箱密碼
$phpmailer->From = 'admin@weixiaoduo.com'; //郵箱帳號
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPSecure = 'ssl'; //tls or ssl(port=25 留空,465 為 ssl)
$phpmailer->IsSMTP();
}

二、讓 WordPress 評論後回覆自動傳送回覆郵件。

//評論回覆郵件
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 = 'webmaster@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME']));
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '你在 [' . get_option("blogname") . '] 的留言有了回應';
    $message = '
    <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-style: solid; border-width: 1;" bordercolor="#85C1DF" width="700" height="251" id="AutoNumber1" align="center">
          <tr>
            <td width="520" height="28" bgcolor="#F5F9FB"><font color="#1A65B6" style="font-size:14px">&nbsp;&nbsp;&nbsp;&nbsp;<b> 留言回覆通知 | <a href="http://www.weixiaoduo.com" targe="blank">v7v3(薇曉朵)</a></b></font></td>
          </tr>
          <tr>
            <td width="800" height="210" bgcolor="#FFffff" valign="top" style=" padding:8px;">&nbsp;&nbsp;<span ><strong >' . trim(get_comment($parent_id)->comment_author) . '</strong>, 你好!</span>
              <p>&nbsp;&nbsp;<span > 你曾在 《' . get_the_title($comment->comment_post_ID) . '》 的留言:<br />&nbsp;&nbsp;--->'
        . trim(get_comment($parent_id)->comment_content) . '<br /><br />
              &nbsp;&nbsp; ' . trim($comment->comment_author) . ' 給你的回覆:<br />&nbsp;&nbsp;--->'
        . trim($comment->comment_content) . '</span></p>
        <p > &nbsp;&nbsp;<Strong> 你可以點選 <a href="'%20.%20htmlspecialchars(get_comment_link($parent_id))%20.%20'"> 檢視回復完整內容</a></Strong></p>
              <p><span > &nbsp;&nbsp;<strong> 感謝你對 <a href="'%20.%20get_option('home')%20.%20'" target="_blank">' . get_option('blogname') . '</a> 的關注,歡迎<a href="http://list.qq.com/cgi-bin/qf_invite?id=3077e5391b4997f56a6854122b0c351be607d83c3ebf649b" target="_blank"> 訂閱本站</a></strong>!</p>
            <p>&nbsp;&nbsp; 更多內容請:<a href="https://www.weixiaoduo.com/group/" target="_blank"> 本站 Q 群</a> | <a href="http://wpa.qq.com/msgrd?v=3&uin=1176882511&site=v7v3&menu=yes"> 聯絡站長</a> | <a href="mailto:v7v7@qq.com"> 投稿</a> | <a href="https://www.weixiaoduo.com/tougao/"> 投稿須知</a> | <a href="https://www.weixiaoduo.com/about/"> 關於我們</a> | <a href="https://www.weixiaoduo.com/contact/"> 聯絡我們</a></p></td>
          </tr>
          <tr>
            <td width="800" height="16" bgcolor="#85C1DF" bordercolor="#008000"><div align="center"><font color="#fff"><a href="http://www.weixiaoduo.com"> 薇曉朵 (www.weixiaoduo.com)</a></font></div></td>
          </tr>
  </table>';
    $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');

以上程式碼加入到 functions.php 裡呼叫即可。