Word Press 评论回复通知邮件优化

in #cn7 years ago (edited)

很久之前看到 kn007 文章 [Postfix进阶]利用Fail2ban防止爆破和攻击 提到邮件提醒同时包含 text/plain 与 text/html 两个版本,因此能获得满分。

值得一提的是,目前 Wordpress 评论回复通知邮件提醒同时包含 text/plain 与 text/html 两个版本,这使得 HTML 版本的邮件也能获得满分。并且全新的邮件风格,具有不错的兼容性(响应式)。

那时候就蠢蠢欲动想折腾,无奈用的虚拟主机 ip 已在垃圾邮件黑名单,加上没有现成的代码,又不好意思伸手要,就放下了。

九月底把博客搬到廉价的 Bandwagon Host KVM VPS,用上 Postfix,又忍不住要折腾。几经周折,用 davidnknight 的 send_email.php 代码勉强折腾得差不多,不过邮件评分只有 7.2。

只好厚着脸皮找 kn007 要方法,前几天在他博客新文章看到提供单封邮件双版本思路和方法作者:LWL。在他博客找到了双版本邮件实现代码:记弃用多说牵扯出来的一堆坑

不过文章贴的代码有点小坑,我是在 LWL Hermit X - 全新 WordPress 播放器插件代码(发送更新通知)里找到了实例。

//Hook phpmailer init action, then force change message_type to alt.
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()).'@hermit.x.update>';
});

把代码集成到原本评论回复通知邮件代码中,双版本邮件是实现了,不过并不能满分,说是 html 代码百分比不达标,扣分。言下之意是 html 版本邮件不能太简洁?

于是扒了 kn007 的邮件模板测试,mail-tester 妥妥的打了十分。

 574-email-test

下面是老杨目前在用的 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);
}

注意:可能需要根据实际主题代码进行修改才能正常使用。

不过,测试十分又怎样?该进垃圾邮件的还是会进,折腾罢了。

感谢 kn007 & LWL 两位博主。


Originally Posted on Steemit & Blog by @zhijun.

Sort:  

你好!cn区点赞机器人 @cnbuddy 感谢你对cn区作出成长的贡献。让我们携手努力,共同促进cn区快速发展。更多cn区动态,请留意我的主页。如果我打扰到你,请回复“取消”。

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 59176.07
ETH 2667.95
USDT 1.00
SBD 2.42