这是自用的评论回复邮件通知代码,源码来自 Willin、Qiqiboy,有兴趣者可以去这两位作者博客看看。
评论回复通知是支持嵌套和@用户方式的。
用法很简单,把下面的代码扔到主题的 functions.php 里面即可(原则上要打开 WordPress 原生嵌套,具体木有去研究。)
/* 邮件通知 by Qiqiboy */ function comment_mail_notify($comment_id) { $comment = get_comment($comment_id);//根据id获取这条评论相关数据 $content=$comment->comment_content; //对评论内容进行匹配 $match_count=preg_match_all('/<a href="#comment-([0-9]+)?" rel="nofollow">/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 = '<div style="background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding:0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;"> <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p> <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />' . trim(get_comment($parent_id)->comment_content) . '</p> <p>' . trim($comment->comment_author) . ' 给你的回复:<br />' . trim($comment->comment_content) . '<br /></p> <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id,array("type" => "all"))) . '">查看回复的完整內容</a></p> <p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p> <p>(此邮件有系统自动发出, 请勿回复.)</p></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 ); } }
另外一个版本
/* 评论回复邮件通知 */ function comment_mail_notify_sendmail($parent_id,$comment){ $tobackup = get_option('admin_email');//同时发送一份备份通知邮件到管理员邮箱 $author_email=$comment->comment_author_email;//评论人邮箱 $to = trim(get_comment($parent_id)->comment_author_email);//被回复人邮箱 $spam_confirmed = $comment->comment_approved;//comment_approved 1:通过审核的评论;0:待审核;trash:回收站;spam:垃圾评论 $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // 发送 e-mail 地址, no-reply 根据实际修改为可用的 e-mail. if ( ($spam_confirmed == '1') && ( $to != $author_email ) ){ $subject = 'Re:《'.get_the_title($comment->comment_post_ID).' - ' . get_option("blogname") . '》'; $message_html = '<!DOCTYPE html><html style="font-size:100%;margin:0;padding:0"><meta name="viewport" content="width=device-width"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>您在《'.get_the_title($comment->comment_post_ID).' - ' . get_option("blogname") . '》的评论有新回复</title><style type="text/css">.wp-smiley{width:23px!important;height:auto!important;max-height:none!important;margin-right:5px;}</style><div bgcolor="#FFF" style="clear:both!important;display:block!important;max-width:600px!important;margin:0 auto;padding:16px;border-width:0;box-shadow:0 1px 2px rgba(9,2,1,.1),0 0 10px rgba(0,0,0,.06)"><h1 style="font-weight:400;font-size:1.35em;color:#333;margin:0 0 10px;padding-bottom:10px;border-bottom:1px solid rgba(0,0,0,.08)"><a style="text-decoration:none;color:#333" href="'.get_option('home').'" target="_blank">'.get_option("blogname").'</a></h1><p style="font-size:14px;color:#354450;font-weight:400;margin:20px 0 0;padding:0">'.trim(get_comment($parent_id)->comment_author).',您好!您在文章《'.get_the_title($comment->comment_post_ID).'》发表的评论:</p><p style="background-color:#EFEFEF;padding:15px;margin:10px 0;font-size:14px;color:#354450;line-height:1.6em;font-weight:normal">'.nl2br(get_comment($parent_id)->comment_content).'</p><p style="font-size:14px;color:#354450;font-weight:400;margin:20px 0 0;padding:0">'.trim($comment->comment_author).'给您回复如下:</p><p style="background-color:#EFEFEF;padding:15px;margin:10px 0;font-size:14px;color:#354450;line-height:1.6em;font-weight:normal">'.nl2br($comment->comment_content).'</p><p style="font-size:14px;color:#354450;line-height:1.6em;font-weight:400;margin:20px 0;padding:0">您可以点击 <a style="text-decoration:none;color:#5692BC" href="'.htmlspecialchars(get_comment_link($parent_id,array("type" => "all"))).'" target="_blank">查看完整回复</a>,也欢迎您再次光临 <a style="text-decoration:none;color:#5692BC" href="'. get_option("home").'" target="_blank">'.get_option("blogname").'</a>。祝您天天开心!</p><p style="color:#999;font-size:12px;font-weight:400;margin:0;padding:10px 0 0;border-top:1px solid rgba(0,0,0,.08)">本邮件由博客评论系统自动发出,意在新评论通知。可直接回复联系博主。</p></div>'; $message_plain = '您在《'.get_the_title($comment->comment_post_ID).' - ' . get_option("blogname") . '》的评论有新回复'."\r\n\r\n".trim(get_comment($parent_id)->comment_author).',您好!您在文章《'.get_the_title($comment->comment_post_ID).'》发表的评论:'."\r\n".nl2br(strip_tags(get_comment($parent_id)->comment_content))."\r\n\r\n".trim($comment->comment_author).'给您回复如下:'."\r\n".nl2br(strip_tags($comment->comment_content))."\r\n\r\n".'您可以通过下面链接查看完整回复,也欢迎再次光临'.get_option("blogname").'。祝您天天开心!'."\r\n".htmlspecialchars(get_comment_link($parent_id,array("type" => "all")))."\r\n\r\n".'本邮件由博客评论系统自动发出,意在新评论通知。可直接回复联系博主。'; } $message_html = str_replace("*uniqueID*", substr(md5(uniqid() . microtime()), 0, 5), convert_smilies($message_html)); global $phpmailer; add_action('phpmailer_init', function (&$phpmailer) use ($message_html, $message_plain){ $phpmailer->isHTML(true); $phpmailer->Body = $message_html; $phpmailer->AltBody = $message_plain; $phpmailer->CharSet = 'utf-8'; $phpmailer->Encoding = 'base64'; $phpmailer->MessageID = '<' . md5($message_plain.microtime().uniqid()).'@'.preg_replace("/^www\./", "", parse_url(esc_url( home_url('/') ), PHP_URL_HOST)).'>'; }); $headers = "Reply-To: ".trim($comment->comment_author)."-".get_option('blogname')." <".$author_email."> " . "\n"; wp_mail($to, $subject, $message_html,$headers); wp_mail($tobackup, $subject, $message_html,$headers); } function comment_mail_notify($comment_id) { $comment = get_comment($comment_id);//根据id获取这条评论相关数据 $content=$comment->comment_content; //对评论内容进行匹配 $match_count=preg_match_all('/<a href="#comment-([0-9]+)?" rel="nofollow">/si',$content,$matchs); if($match_count>0){//如果匹配到了 foreach($matchs[1] as $parent_id){//对每个子匹配都进行邮件发送操作 comment_mail_notify_sendmail($parent_id,$comment); } } elseif ($comment->comment_parent!='0'){//以防万一,有人故意删了@回复,还可以通过查找父级评论id来确定邮件发送对象 $parent_id=$comment->comment_parent; comment_mail_notify_sendmail($parent_id,$comment); } else { $parent_id=$comment_id; comment_mail_notify_sendmail($parent_id,$comment); } } add_action('comment_mail_notify_async', 'comment_mail_notify');//异步发送 function comment_mail_schedule_notify_async($comment_id) { //不延时仅仅异步发送 //如果需要延时将 time() 改成 time()+33 形式,33表示延时33秒 wp_schedule_single_event( time(), 'comment_mail_notify_async', [$comment_id] ); } add_action('comment_post', 'comment_mail_schedule_notify_async'); add_action('wp_set_comment_status', 'comment_mail_schedule_notify_async'); /* 评论回复邮件通知 end */