分类
菜鸟建站

免插件实现博客评论邮件通知功能

访问者对博客进行评论且填写了邮箱,然后博主或其他访问者对此评论进行了回复后,访问者的邮箱会收到一封对评论回复的通知,这将大大提升博客互动的友好性。

然而,使用一些邮箱作为邮件通知功能的邮箱,如果查看“邮件原文”的话,会暴露博客所在服务器的ip地址,这将极大危害博客的安全。

经测试, QQ 邮箱、网易邮箱、飞书邮箱等,邮件中的“邮件原文”都会暴露服务器 ip 地址,即便使用 CDN 服务也不行。

最终发现,使用阿里免费企业邮箱,作为邮件通知功能的邮箱,则不会暴露服务器 ip 地址。

阿里免费企业邮箱申请地址:https://common-buy.aliyun.com/

登录地址:https://qiye.aliyun.com/ (或 mail.yourwebsite.com )

客户端安全密码开启方法:https://help.aliyun.com/document_detail/444269.html

SMTP 服务器地址:smtp.qiye.aliyun.com

服务器端口号( SSL 加密):465

使用 WordPress 程序的博客,则可以通过在 functions.php 文件增加一段代码的形式,简单实现评论的邮件通知功能。

WordPress 程序的 functions.php 文件一般位于当前所使用主题的目录下,对其修改,一般只会影响当前所使用的主题。如果出现错误或者没有达到预期的效果,然后进行删除即可。

一般 functions.php 文件地址位于如下:

wp-content/themes/当前主题目录

以下即为 WordPress 评论的邮件通知两段代码,直接放入 functions.php 文件即可。且以阿里云免费个人邮箱为例,不会暴露服务器 ip 地址。

两段代码中,主要修改第一段代码中的第9、10、14行相关信息,用自己邮箱信息进行替换即可,参看后面的注释说明。

//WordPress 评论回复邮件通知代码:使用 SMTP 发邮件实现评论通知
add_action('phpmailer_init', 'fanly_mail_smtp');
function fanly_mail_smtp( $phpmailer ) {
	$phpmailer->IsSMTP();
	$phpmailer->SMTPAuth = true;//启用 SMTPAuth 服务
	$phpmailer->Port = 465;//SMTP 邮件发送端口, SSL 加密端口为465
	$phpmailer->SMTPSecure ="ssl";//验证 SSL
	$phpmailer->Host = "smtp.qiye.aliyun.com";//SMTP 服务器地址
        $phpmailer->Username = "admin@eeee.me";//发件邮箱地址
        $phpmailer->Password ="******";//客户端安全密码
}
add_filter( 'wp_mail_from', 'fanly_wp_mail_from' );
function fanly_wp_mail_from() {
return 'admin@eeee.me';//发件邮箱地址
}

//WordPress 评论回复邮件通知代码:回复配置。此次内容基本不需要修改
function fanly_comment_mail_notify($comment_id) {
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    $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 = trim(get_comment($parent_id)->comment_author) . ',您在 [' . $blogname . '] 中的留言有新的回复啦!';
        $message = '<div style="color:#555;font:12px/1.5 微软雅黑,Tahoma,Helvetica,Arial,sans-serif;max-width:550px;margin:50px auto;border-top: none;" ><table border="0" cellspacing="0" cellpadding="0"><tbody><tr valign="top" height="2"><td valign="top"><div style="background-color:white;border-top:2px solid #00A7EB;box-shadow:0 1px 3px #AAAAAA;12px;max-width:550px;color:#555555;font-family:微软雅黑, Arial;;font-size:12px;">
<h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:8px 0 10px 8px;"><span style="color: #00A7EB;font-weight: bold;">> </span>您在 <a style="text-decoration:none; color:#58B5F5;font-weight:600;" href="' . home_url() . '">' . $blogname . '</a> 的留言有回复啦!</h2><div style="padding:0 12px 0 12px;margin-top:18px">
<p>您好, ' . trim(get_comment($parent_id)->comment_author) . '! 您发表在文章 《' . get_the_title($comment->comment_post_ID) . '》 的评论:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">' . nl2br(strip_tags(get_comment($parent_id)->comment_content)) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复如下:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">' . nl2br(strip_tags($comment->comment_content)) . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#5692BC" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回复內容</a>,也欢迎再次光临 <a style="text-decoration:none; color:#5692BC"
href="' . home_url() . '">' . $blogname . '</a>。思意博客博客祝您生活愉快!</p>
<p style="padding-bottom: 15px;">(此邮件由系统自动发出,请勿直接回复!)</p></div></div></td></tr></tbody></table></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 );
    }
}
add_action('comment_post', 'fanly_comment_mail_notify');

“免插件实现博客评论邮件通知功能”上的2条回复

你好,确实不好找,已经给文中“阿里免费企业邮箱申请地址”加了申请地址的链接了,你试试。
记得最好申请时间长的五年,控制台进去后也可以更换域名。
祝使用愉快!

发表回复

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