今天下午登陆网站无意发现网站的邮件功能失效了,毫无疑问,最近这段时间的评论回复邮件没能发出去。问题很快被找到,原来是 WP Mail SMTP 插件被重置了,重新做了设置,又把当前在用的评论回复邮件通知插件 Comment Email Reply 做了一番改动
改动内容
- 使之兼容最新版的 WordPress,原插件已不能在新版的 WP 上正常工作了;
- 评论回复通知邮件延迟发送,默认延迟 300 秒;
- 将评论设置为 spam 或删除评论时也会向原评论人发送通知邮件;
使用方法
- 请确认网站的邮件功能正常,可借助于 WP Mail SMTP 插件来实现邮件功能
- 将下方代码复制并保存为 php 后缀文件,如 comment_email_reply.php
- 在网站目录 wp-content/plugins 下新建一个文件夹,如 Comment Email Reply,将上一步所建的 php 文件放入其中
- 登陆网站后台,在插件页面激活插件 Comment Email Reply 即可
以下是修改后的代码:
<?php
/**
* Plugin Name: Comment Email Reply
* Plugin URI:
* Description:
* Version: 1.0.5
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
add_filter('wp_mail_content_type', function ($contentType) {
return 'text/html';
});
function cer_comment_notification($comment_id) {
$comment_object = get_comment($comment_id);
$comment_parent = $comment_object->comment_parent;
$mail_dtl = '<html><head><style>#mgy{margin:0;padding:0;font-size:16px;font-size:1rem;line-height:1.75;color:#333;background-color:#f5f5f5}#mgy a{color:#2c6eff}#mgy .a{padding:40px 10px}#mgy .b{width:600px;margin:0 auto;padding:40px;border-radius:10px;background:#fff}#mgy .c{clear:both}#mgy .d{margin:0 0 24px}#mgy .e{border-top:1px solid #e5e5e5;padding-top:10px;}#mgy .f{margin-bottom:10px}#mgy .g{margin:0 5px}#mgy .h{width:100px;height:34px;line-height:34px;padding:2px;border-radius:34px;color:#fff;background:#5d9ff5}#mgy .h:hover{background:#00b1dc}#mgy .i{margin:40px 0 0}#mgy .s{border-bottom:1px dotted;text-decoration:none}#mgy .k{text-align:center;text-decoration:none}#mgy .l{display:block}#mgy .o{margin:5px 0}#mgy .p{color:#5a5a5a}#mgy .q{border-left:2px solid #e5e5e5;color:#777;padding:6px 12px;line-height:1.625}#mgy .z{width:212px;height:84px;margin:0 auto 40px;background-image:url("");background-repeat:no-repeat}@media screen and (max-width:680px){#mgy{font-size:18px;font-size:1.125rem;line-height:2}#mgy .b{width:100%;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;padding:40px 15px}}</style></head><body>';
$mail_dtl .= '<div id="mgy" style="background-color:#f1f1f1">';
$mail_dtl .= '<div class="a"><div class="b"><div class="l z"></div><div class="o">';
$parent_object = get_comment($comment_parent);
$comment_post_id = $parent_object->comment_post_ID;
/* 特色图
if ( has_post_thumbnail($comment_post_id) ) {
$mail_dtl .= '<div style="height:200px;max-width:100%;background-image:url(';
$mail_dtl .= get_the_post_thumbnail_url($comment_post_id, 'medium' );
$mail_dtl .= ');background-repeat:no-repeat;background-position:50% 50%;background-size:cover"></div>';
}
*/
$mail_dtl .= $parent_object->comment_author . ':</div>';
$mail_dtl .= '<div class="f"> 您好,您在<a class="g s" href="' . get_permalink($comment_post_id) . '">' . get_the_title($comment_post_id) . '</a> 一文中发表的评论</div>';
$mail_dtl .= '<div class="f q">' . esc_html($parent_object->comment_content) . '</div>';
$mail_dtl .= '<div class="f"> 被<span class="g">';
// 回复者
if ($comment_object->comment_author_url !== '') {
$mail_dtl .= '<a class="s" href="' . $comment_object->comment_author_url . '">';
$mail_dtl .= $comment_object->comment_author;
$mail_dtl .= '</a>';
} else {
$mail_dtl .= $comment_object->comment_author;
}
$mail_dtl .= '</span> 作了如下回复</div>';
$mail_dtl .= '<div class="d q">' . esc_html($comment_object->comment_content) . '</div>';
$mail_dtl .= '<a class="h k l" href="' . get_comment_link($parent_object->comment_ID) . '"> 继续回复</a>';
$site_name = get_option('blogname');
$mail_dtl .= '<div class="f i e"> 欢迎订阅『' . $site_name . '』' . get_feed_link('') . '</div></div></div></div></body></html>';
$rcv = $parent_object->comment_author_email;
$title = '来自 [' . $site_name . '] 的评论回复';
wp_mail($rcv, $title, $mail_dtl);
}
add_action('set_brave_comment_mail_notify', 'cer_comment_notification', 10, 1);
function comment_mail_notify_schedule($comment_id) {
$comment_object = get_comment($comment_id);
$comment_parent = $comment_object->comment_parent;
if ($comment_object->comment_approved === '1' && $comment_parent > 0) {
wp_schedule_single_event(time() + 10, 'set_brave_comment_mail_notify', array($comment_id));
}
}
add_action('wp_insert_comment', 'comment_mail_notify_schedule', 99, 1);
function cer_comment_status_changed($comment_id, $comment_status) {
if ($comment_status === 'approve') {
comment_mail_notify_schedule($comment_id);
}
}
# Fire Email when comments gets approved later.
add_action('wp_set_comment_status', 'cer_comment_status_changed', 99, 2);
?>
貌似这个延时不大好用,延时+Gmail会失败,不发送邮件。记得以前试过是可以的。加了这个评论回复提交速度会快很多……
@老杨 好用的很,我用的QQ的域名邮箱,gmail是不是要考虑墙的问题。
收藏了