前言

目标解决两个问题:

  1. 用户在文章中提交评论后,我们无法及时获取用户评论的内容,同时也就没办法及时回复;
  2. 从后台发现用户评论,虽然回复了,但是用户已经离开网站,无法接受到您给他的评论回复。

方法一:插件 WordPress Mail SMTP

WP 后台直接安装即可,全中文操作。如果使用 qq 邮箱,邮件系统类型选择 other smtp 。stmp 地址填入 smtp.qq.com ,勾选 ssl ,端口 465 。帐号为发件邮箱,密码为 stmp 服务授权码。

方法二:手动修改代码

找到你当前使用的主题目录下的 functions.php :

在文件末尾加上下面代码,请自行配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//使用 smtp 发邮件
add_action('phpmailer_init', 'fanly_mail_smtp');
function fanly_mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用 SMTPAuth 服务
$phpmailer->Port = 465;//SMTP 邮件发送端口,这个和下面的 SSL 验证对应,如果这里填写 25,则下面参数为空
$phpmailer->SMTPSecure ="ssl";//是否验证 ssl,与 MTP 邮件发送端口对应,如果不填写,则上面的端口须为 25
$phpmailer->Host = "smtp.qq.com";//邮箱的 SMTP 服务器地址,目前 smtp.qq.com 为 QQ 邮箱 SMTP
$phpmailer->Username = "[email protected]";//smtp帐号邮箱地址(等于发件地址)
$phpmailer->Password ="***************";//smtp授权码
}
add_filter( 'wp_mail_from', 'fanly_wp_mail_from' );
function fanly_wp_mail_from() {
return '[email protected]';//发件邮箱地址(等于smtp帐号邮箱地址)
}

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

参考