这是自用的评论回复邮件通知代码,源码来自 Willin、Qiqiboy,有兴趣者可以去这两位作者博客看看。
2020-07-07 新版本:https://cyhour.com/1382
这版本的评论回复通知是支持嵌套和@用户方式的。
用法很简单,把下面的代码扔到主题的 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 );
}
}
-----------------分割线---------------------
Update:大发版 - 2015-02-22
下面代码加到functions.php中
/**
* mail when reply to someone
*
* @since Pure 1.0
*/
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
$logo = get_template_directory_uri().'/images/logo.png';//LOGO 地址
if (($parent_id != '') && ($spam_confirmed != 'spam') && (get_option('admin_email') != get_comment($parent_id)->comment_author_email)) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); //可以修改为你自己的邮箱地址
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '你在 [' . get_option("blogname") . '] 的留言有了新回复';
$message = '<table class="email" style=" width: 600px; margin-top: 10px; margin-right: auto; margin-bottom: 0; margin-left: auto; font-size: 16px; line-height: 1.4;">
<tbody>
<tr>
<td style="padding-top:40px;padding-right:5%;padding-bottom:46px;padding-left:5%;color:#333332">
<div class="email-header" style="margin-bottom: 20px;">
<div class="email-logo-wrapper" style="width: 50px; margin-top: 0; margin-right: auto; margin-bottom: 0; margin-left: auto;">
<img class="email-logo" style="display: block; width: 50px;" src="'. $logo .'">
</div>
</div>
<div>
<p style="margin-top:0;margin-right:0;margin-bottom:20px;margin-left:0;font-size:18px;line-height:1.4;text-align:center;color:#333332">' . trim(get_comment($parent_id)->comment_author) . ',你好。</p>
<p><span style="color:#3eae5f;">' . trim($comment->comment_author) . '</span> 回复了您在文章<strong style="font-weight:bold">' . get_the_title($comment->comment_post_ID) . '</strong>中的评论"' . trim(get_comment($parent_id)->comment_content) . '"</p>
<hr style="width:50px;border:0;border-bottom:1px solid #e5e5e5;margin-top:20px">
<p style="margin-top:20px;margin-right:0;margin-bottom:20px;margin-left:0">If you like what you read, keep the conversation going!</p>
<div style="margin-top:30px;padding-top:26px;border-top:1px solid #e5e5e5;font-size:16px;color:#333332;overflow:hidden">
<div><a target="_blank" style="text-decoration:none;display:block;width:50px;float:left;margin-left:0;line-height:0;margin-right:10px;margin-top:5px" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">'. get_avatar($comment->comment_author_email,50). '</a>' . trim($comment->comment_content) . '</div>
<div style="padding-top:0;padding-right:0;padding-bottom:0;padding-left:0;margin-top:10px;margin-right:0;margin-bottom:0;margin-left:60px;overflow:hidden"><a target="_blank" style="color:#ffffff;text-decoration:none;display:inline-block;min-height:26px;line-height:27px;padding-top:0;padding-right:16px;padding-bottom:0;padding-left:16px;outline:0;background:#3eae5f;font-size:12px;text-align:center;font-style:normal;font-weight:400;border:0;vertical-align:bottom;white-space:nowrap;border-radius:999em" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看</a></div>
</div>
<div style="color:#b3b3b1;font-size:14px;text-align:center;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0;margin-top:50px;margin-right:0;margin-left:0">本邮件由' . get_option("blogname") . '自动生成,<span style="color:#3eae5f">请勿回复</span>。</div>
</div>
</td>
</tr>
<tr>
<td style="padding-top:0;padding-right:0;padding-bottom:0;padding-left:0;font-size:12px;text-align:center;color:#b3b3b1">
<div style="padding-top:13px;border-top:1px solid #e5e5e5">Sent by <a target="_blank" style="color:#b3b3b1" href="' . home_url() . '">' . get_option("blogname") . '</a> · Since 2011 </div>
</td>
</tr>
</tbody>
</table>';
$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');
本文首发于:WordPress 评论回复邮件通知代码-垃圾站
有个疑问,我是用插件配置的WP SMTP邮件发送,这个插件里面能指定发送的sent from名称,sent from邮件地址。
但是通常的评论回复插件与修改都会自定义这段,它们到底会怎样?以SMTP插件配置的为准吗?
@YaYa 目测是没修改这个字段的话就按 WP SMTP 配置的参数,若自定义了就按自定义的。
@YaYa 刚试了一下,是按 Configure SMTP 配置的去发送的。(我用的是 Configure SMTP 插件配置 SMTP)
@老头 谢谢,刚刚吃饭想了下,其实打开编辑看看PHP代码就知道了。。SMTP配置插件的内容处在数据流流出的方向,它可能会丢弃上游的也更正常。