方法三:让博客管理员决定什么情况下发邮件
你可以根据自己的需求,配置下面代码(看代码注释),决定什么情况才发邮件。
/* 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 ----------------------------------------
方法四:支持嵌套和@用户方式的评论提醒
此方法转载自zww.me,这版本的评论回复通知是支持嵌套和@用户方式的。有什么问题,请到作者页面反馈。
/* 邮件通知 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 = '