把代码集成到原本评论回复通知邮件代码中,双版本邮件是实现了,不过并不能满分,说是 html 代码百分比不达标,扣分。言下之意是 html 版本邮件不能太简洁?
目前在用的 WordPress 评论回复邮件通知完整代码:
add_action('comment_post','comment_mail_notify'); add_action('comment_unapproved_to_approved', 'comment_mail_notify');//未通过审核的评论,审核后再通知评论者 function comment_mail_notify($comment_id) { $comment = get_comment($comment_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'){ $parent_id=$comment->comment_parent; comment_mail_notify_sendmail($parent_id,$comment); }else return; } function comment_mail_notify_sendmail($parent_id,$comment){ $author_email=$comment->comment_author_email;//评论人邮箱 $to = trim(get_comment($parent_id)->comment_author_email);//被回复人邮箱 $spam_confirmed = $comment->comment_approved; if ( ! ( ($spam_confirmed == '1') && ( $to != $author_email ) && ( get_bloginfo ('admin_email') != $to ) ) ) return; $wp_email = 'noreply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); $subject = '您在『' . 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_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_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 = "From: ".get_option('blogname')." <".$wp_email.">" . "\n"; wp_mail($to, $subject, $message_html,$headers); }