不凡博客(Bufanz.com)

版权声明

未经允许,请勿转载本博客的文章、图片和视频,谢谢!
首页 > WordPress > 正文

WordPress回复评论发送邮件通知(代码版)

WordPress | 2022年01月13日23:21:58

腾讯云轻量服务器2核2G3M仅88元/年

前言

  WordPress有发送评论时给管理员发送邮件通知的功能,如果站长或者博主回复游客的评论时,系统是不会给游客发送邮件。

WordPress回复评论发送邮件通知(代码版)

解决方法

在后台打开主题编辑器,按照下图操作:

WordPress回复评论发送邮件通知(代码版)

代码如下

发信邮箱推荐使用阿里邮箱:https://mail.aliyun.com,有五角星标记是粘贴到模板函数文件中后,必须要修改参数。

使用阿里邮箱,就不用修改第一个五角星的参数。

// 回复评论发送邮件通知
add_action('phpmailer_init', 'fanly_mail_smtp');
function fanly_mail_smtp( $phpmailer ) {
	$phpmailer->IsSMTP();
	$phpmailer->SMTPAuth = true;
	$phpmailer->Port = 465;
	$phpmailer->SMTPSecure ="ssl";
	$phpmailer->Host = "smtp.aliyun.com";// ★ 邮箱的 SMTP 服务器地址
	$phpmailer->Username = "example@aliyun.com";// ★ 邮箱账号或者发信邮箱
	$phpmailer->Password ="***************";// ★ 密码或者授权码
}
add_filter( 'wp_mail_from', 'fanly_wp_mail_from' );
function fanly_wp_mail_from() {
	return 'example@aliyun.com';// ★ 发件邮箱
}

function fanly_comment_mail_notify($comment_id) {
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
    $comment = get_comment($comment_id);
    $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
    $spam_confirmed = $comment->comment_approved;
    if (($parent_id != '') && ($spam_confirmed != 'spam')) {
        $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
        $to = trim(get_comment($parent_id)->comment_author_email);
        $subject = trim(get_comment($parent_id)->comment_author) . ',您在 [' . $blogname . '] 中的留言有新的回复啦!';
        $message = '<div style="color:#555;font:12px/1.5 微软雅黑,Tahoma,Helvetica,Arial,sans-serif;max-width:550px;margin:50px auto;border-top: none;" ><table border="0" cellspacing="0" cellpadding="0"><tbody><tr valign="top" height="2"><td valign="top"><div style="background-color:white;border-top:2px solid #00A7EB;box-shadow:0 1px 3px #AAAAAA;12px;max-width:550px;color:#555555;font-family:微软雅黑, Arial;;font-size:12px;">
<h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:8px 0 10px 8px;"><span style="color: #00A7EB;font-weight: bold;">> </span>您在 <a style="text-decoration:none; color:#58B5F5;font-weight:600;" href="' . home_url() . '">' . $blogname . '</a> 的留言有回复啦!</h2><div style="padding:0 12px 0 12px;margin-top:18px">
<p>您好, ' . trim(get_comment($parent_id)->comment_author) . '! 您发表在文章 《' . get_the_title($comment->comment_post_ID) . '》 的评论:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">' . nl2br(strip_tags(get_comment($parent_id)->comment_content)) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复如下:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">' . nl2br(strip_tags($comment->comment_content)) . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#5692BC" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回复內容</a>,也欢迎再次光临 <a style="text-decoration:none; color:#5692BC"
href="' . home_url() . '">' . $blogname . '</a>。祝您生活愉快!</p>
<p style="padding-bottom: 15px;">(此邮件由系统自动发出,请勿直接回复!)</p></div></div></td></tr></tbody></table></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 );
    }
}
add_action('comment_post', 'fanly_comment_mail_notify');
//////////////////////

游客收到评论回复邮件通知的邮件内容

WordPress回复评论发送邮件通知(代码版)

The End
淘宝购物先领券,更省钱

本文标题:WordPress回复评论发送邮件通知(代码版)

本文链接:https://bufanz.com/post/196.html

版权声明:本文章是 不凡博客(Bufanz.com) 的原创文章,未经允许请勿转载本文章!