为了利于用户体验和互动,我们回复站内评论的时候希望对方能够收到一条邮件通知,那么就可以使用回复评论后自动发送邮件通知的功能。

首先需要准备好域名邮箱,这里不再赘述。

之后在WordPress后台安装[`WP-Mail-SMTP`](https://cn.wordpress.org/plugins/wp-mail-smtp/)插件,配置 SMTP 服务器信息并测试发信是否正常。若不想安装插件,可以直接对以下代码做出相应修改后添加至 `functions.php` 文件中。之后进行下一步。

```php
//使用 smtp 发邮件
add_action('phpmailer_init', 'fanly_mail_smtp');
function fanly_mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用 SMTPAuth 服务
$phpmailer->Port = 465;//MTP 邮件发送端口,这个和下面的 SSL 验证对应,如果这里填写 25,则下面参数为空
$phpmailer->SMTPSecure ="ssl";//是否验证 ssl,与 MTP 邮件发送端口对应,如果不填写,则上面的端口须为 25
$phpmailer->Host = "smtp.exmail.qq.com";//邮箱的 SMTP 服务器地址,目前 smtp.exmail.qq.com 为 QQ 邮箱和腾讯企业邮箱 SMTP
$phpmailer->Username = "blog@leiue.com";//你的邮箱地址
$phpmailer->Password ="***************";//你的邮箱登录密码
}
add_filter( 'wp_mail_from', 'fanly_wp_mail_from' );
function fanly_wp_mail_from() {
return 'blog@leiue.com'; //发件地址要和 smtp 邮箱一致
}
```

接下来编辑网站当前主题目录下的`functions.php`文件,加入以下代码即可。

```php
// 评论回应邮件通知
function comment_mail_notify($comment_id) {
$admin_email = get_bloginfo ('admin_email');
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email)) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
$subject = '您在 [' . get_option("blogname") . '] 的留言有了新回复';
$message = '

>您在 ' . get_option('blogname') . ' 博客上的留言有回复啦!

亲爱的 ' . trim(get_comment($parent_id)->comment_author) . ', 您好!您曾在文章《' . get_the_title($comment->comment_post_ID) . '》上发表评论:

'. trim(get_comment($parent_id)->comment_content) . '

'. trim($comment->comment_author) .'给您的回复如下:

' . trim($comment->comment_content) .'

您可以点击查看回复的完整内容,欢迎再次光临' . get_option('blogname') . '

请注意:此邮件由 ' . get_option('blogname') . ' 自动发送,请勿直接回复。
如果此邮件不是您请求的,请忽略并删除!

';
$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', 'comment_mail_notify');
// -- END ----------------------------------------
```

记得要将以上代码中的`no-reply`改为实际发信使用的邮箱前缀噢~

这是对方收到的邮件截图(忽略沙雕评论…),我觉着还挺美观的哈哈哈哈

以上是本站在使用的代码,在回复所有填写了邮箱的评论后都将自动发送邮件提醒评论人。

另外,还可以使用其它代码来实现多种功能。

1.在评论框下方显示一个勾选框,让评论人自己决定是否接收邮件通知:

```php
/* 开始*/
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
global $wpdb;
if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
$wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
$wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
$spam_confirmed = $comment->comment_approved;
if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
$wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
$message = '

' . trim(get_comment($parent_id)->comment_author) . ', 您好!

您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '

' . trim($comment->comment_author) . ' 给您的回复:
'
. trim($comment->comment_content) . '

您可以点击查看回复的完整內容

还要再度光临 ' . get_option('blogname') . '

(此邮件由系统自动发送,请勿回复.)

';
$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', 'comment_mail_notify');

/* 自动加勾选栏 */
function add_checkbox() {
echo '

';
}
add_action('comment_form', 'add_checkbox');
```

2.让博客管理员决定什么情况下发邮件(详见注释):

```php
/* comment_mail_notify v1.0 by willin kan. (无勾选栏) */
function comment_mail_notify($comment_id) {
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
/* 上面的判断式,决定发出邮件的必要条件:
($parent_id != '') && ($spam_confirmed != 'spam'): 回复的, 而且不是 spam 才可发, 必需!!
($to != $admin_email) : 不发给 admin.
($comment_author_email == $admin_email) : 只有 admin 的回复才可发.
可视个人需要修改上面的条件.
*/
$wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
$message = '

' . trim(get_comment($parent_id)->comment_author) . ', 您好!

您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:
'
. trim(get_comment($parent_id)->comment_content) . '

' . trim($comment->comment_author) . ' 给您的回复:
'
. trim($comment->comment_content) . '

您可以点击 查看回复的完整內容

还要再度光临 ' . get_option('blogname') . '

(此邮件由系统自动发送,请勿回复.)

';
$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', 'comment_mail_notify');
// -- END ----------------------------------------
```

3.支持嵌套和@用户方式的评论提醒:

```php
/* 邮件通知 by Qiqiboy */
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);//根据id获取这条评论相关数据
$content=$comment->comment_content;
//对评论内容进行匹配
$match_count=preg_match_all('//si',$content,$matchs);
if($match_count>0){//如果匹配到了
foreach($matchs[1] as $parent_id){//对每个子匹配都进行邮件发送操作
SimPaled_send_email($parent_id,$comment);
}
}elseif($comment->comment_parent!='0'){//以防万一,有人故意删了@回复,还可以通过查找父级评论id来确定邮件发送对象
$parent_id=$comment->comment_parent;
SimPaled_send_email($parent_id,$comment);
}else return;
}
add_action('comment_post', 'comment_mail_notify');
function SimPaled_send_email($parent_id,$comment){//发送邮件的函数 by Qiqiboy.com
$admin_email = get_bloginfo ('admin_email');//管理员邮箱
$parent_comment=get_comment($parent_id);//获取被回复人(或叫父级评论)相关信息
$author_email=$comment->comment_author_email;//评论人邮箱
$to = trim($parent_comment->comment_author_email);//被回复人邮箱
$spam_confirmed = $comment->comment_approved;
if ($spam_confirmed != 'spam' && $to != $admin_email && $to != $author_email) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
$message = '

';
$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 );
}
}
```

最后修改:2021 年 02 月 20 日
如果觉得我的文章对你有用,请随意赞赏