醒游网

当前位置: 首页 » 网站日记 » 为网站添加评论回复邮件提醒功能

为网站添加评论回复邮件提醒功能

许多网站都评论邮件提醒功能,一来可以通过邮件提醒让你在第一时间知道谁在你网站做出了评论;二来当有人回复评论时,评论者也能在第一时间收到回复。从而增加了网站的互动性,以提高访客的回头率。

我的新主题本身没有评论邮件提醒功能,所以我准备给它加上,具体实现如下:

一、在 functions.php 中添加功能代码

打开主题的 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']));
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
    $message = '<div style="border-right:#666666 1px solid;border-radius:8px;color:#111;font-size:12px;width:95%;border-bottom:#666666 1px solid;font-family:微软雅黑,arial;margin:10px auto 0px;border-top:#666666 1px solid;border-left:#666666 1px solid"><div class="adM">
    </div><div style="width:100%;background:#666666;min-height:60px;color:white;border-radius:6px 6px 0 0"><span style="line-height:60px;min-height:60px;margin-left:30px;font-size:12px">您在<a style="color:#00bbff;font-weight:600;text-decoration:none" href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a> 上的留言有回复啦!</span> </div>
    <div style="margin:0px auto;width:90%">
    <p><span style="font-weight:bold;">' . trim(get_comment($parent_id)->comment_author) . '</span>, 您好!</p>
    <p>您于' . trim(get_comment($parent_id)->comment_date) . ' 在文章《' . get_the_title($comment->comment_post_ID) . '》上发表评论: </p>
    <p style="border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">' . nl2br(get_comment($parent_id)->comment_content) . '</p>
    <p><span style="font-weight:bold;">' . trim($comment->comment_author) . '</span> 于' . trim($comment->comment_date) . ' 给您的回复如下: </p>
    <p style="border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">' . nl2br($comment->comment_content) . '</p>
    <p>您可以点击 <a style="color:#00bbff;text-decoration:none" href="' . htmlspecialchars(get_comment_link($parent_id)) . '" target="_blank">查看回复的完整内容</a></p>
    <p>感谢你对 <a style="color:#00bbff;text-decoration:none" href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a> 的关注,如您有任何疑问,欢迎在博客留言,我会一一解答</p><p style="color:#A8979A;">(此邮件由系统自动发出,请勿回复。)</p></div></div>';
    $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
    $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
    wp_mail( $to, $subject, $message, $headers );
    // echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
    }
}
add_action('comment_post', 'comment_mail_notify');

此为美化版评论回复提醒,代码来自:boke112,邮件样式见题图,非常漂亮。你也可在此留言评论,被回复后,就能收到邮件提醒并查看样式了。

二、网站开启邮件发送功能

如果你的主机支持 mail() 函数,添加上面代码后就能正常实现评论自动邮件提醒功能,但是国内主机服务商多数不支持 mail() 函数,据说理由是怕被用于发送垃圾邮件,我们需要用 WP SMTP 插件来实现。

下载 WP SMTP 插件并安装,然后按要求设置各项参数。点击设置页上的不同邮箱图标,会显示简易教程,以 QQ 邮箱为例:

具体操作可参照此文,要注意两点:一是“发件人地址”一定要填写你正确的邮箱地址,二是“认证密码”不是登录密码,而是授权码,需要进入邮箱设置页中获取。

设置完成后,可在测试邮件区填写自己的邮箱地址进行测试,如果能正常收到,就表示成功了。

感谢 @青山 童鞋提供了实现 SMTP 发送邮件功能的代码,将其放到主题的 funtions.php 文件中也可实现 WP SMTP 插件同样的功效 。

//使用smtp发送邮件(请根据自己使用的邮箱设置SMTP)
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '醒悟的凡灵'; //发件人名称
$phpmailer->Host = 'smtp.qq.com'; //修改为你使用的邮箱SMTP服务器
$phpmailer->Port = 465; //SMTP端口
$phpmailer->Username = 'yourname@qq.com'; //邮箱账户
$phpmailer->Password = 'xxxxxx'; //邮箱密码
$phpmailer->From = 'yourname@qq.com'; //邮箱账户,以上面一致
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25时->留空,465时->ssl)
$phpmailer->IsSMTP();
}

三、添加评论通过审核后邮件通知评论人功能

为了防止垃圾评论,我们一般都启用了评论审核功能。不过,一旦评论通过审核后,WordPress默认是没有邮件通知评论人的。所以,为了更好的用户体验,我决定同时添加这个邮件通知功能。

仍然是打开主题的 funtions.php 文件,在最末尾加上如下代码:

/**
 * WordPress 评论通过审核后邮件通知评论人
 * http://www.wpdaxue.com/comment-approved-email.html
 */
add_action('comment_unapproved_to_approved', 'wpdx_comment_approved');
function wpdx_comment_approved($comment){
    if (is_email($comment->comment_author_email)){
        $post_link = get_permalink($comment->comment_post_ID);
        $title = '您在【' . get_bloginfo('name') . '】的评论已通过审核';
        $body = '您在《<a href="' . $post_link . '" target="_blank" >' . get_the_title($comment->comment_post_ID) . '</a>》中发表的评论已通过审核!<br /><br />';
        $body .= '<strong>您的评论:</strong><br />';
        $body .= strip_tags($comment->comment_content) . '<br /><br />';
        $body .= '您可以:<a href="' . get_comment_link($comment->comment_ID) . '" target="_blank">查看您的评论</a>  |  <a href="' . $post_link . '#comments" target="_blank">查看其他评论</a>  |  <a href="' . $post_link . '" target="_blank">再次阅读文章</a><br /><br />';
        $body .= '欢迎再次光临【<a href="' . get_bloginfo('url') . '" target="_blank" title="' . get_bloginfo('description') . '">' . get_bloginfo('name') . '</a>】。';
        $body .= '<br /><br />注:此邮件为系统自动发送,请勿直接回复';
        @wp_mail($comment->comment_author_email, $title, $body, "Content-Type: text/html; charset=UTF-8");
    }
}

以上代码来自:WordPress大学

谁有美化版的邮件通知样式代码,欢迎提供。哈,我们虽然没有技术,但追求完美的心是一样的。



关键字: ,
猜你喜欢
用户评论
  1. 期待

    好像苏醒老大functions.PHP打不开

  2. 期待

    文中第二个functions漏了个c字母 ➡

要发表评论,您必须先登录/注册
本类排行