WordPress评论回复邮件通知设置

设置让有人评论的时候和回复评论的时候能发邮件通知给对方。

首先设置邮件通知服务,在主题的模板函数中添加下面的代码

//邮件通知smtp
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '王庆资的部落格'; //发信人名称
$phpmailer->Host = 'smtp.exmail.qq.com'; //smtp服务器
$phpmailer->Port = 465;  //端口
$phpmailer->Username = 'adminadmin@support.wangqingzi.com';//邮箱帐号   
$phpmailer->Password = '********';//邮箱密码
$phpmailer->From = 'adminadmin@support.wangqingzi.com'; //邮箱帐号
$phpmailer->SMTPAuth = true;   
$phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25留空,465为ssl)
$phpmailer->IsSMTP();
}

其次设置评论回复邮件通知,在模板函数中添加下面的代码

//评论回复邮件
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://wangqingzi.com" targe="blank">王庆资的部落格</a></b></font></td>
          </tr>
          <tr>
            <td width="800" height="210" bgcolor="#FFffff" valign="top" style=" padding:8px;">&nbsp;&nbsp;<span class="STYLE2"><strong >' . trim(get_comment($parent_id)->comment_author) . '</strong>, 你好!</span>
              <p>&nbsp;&nbsp;<span class="STYLE2">你曾在《' . 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="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复完整内容</a></Strong></p>
              </td>
          </tr>

  </table>';
    $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');

这样当别人评论,给其回复之后,对方就会收到邮件通知了。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注