WordPress 评论回复通知邮件

目前在用 WordPress 评论回复通知邮件代码

代码放主题 functions.php 文件即可(可能需要根据主题实际修改匹配)

/* 评论回复邮件通知 */
function comment_mail_notify_sendmail($parent_id,$comment){
	$tobackup = get_option('admin_email');//同时发送一份备份通知邮件到管理员邮箱
	$author_email=$comment->comment_author_email;//评论人邮箱
	$to = trim(get_comment($parent_id)->comment_author_email);//被回复人邮箱
	$spam_confirmed = $comment->comment_approved;//comment_approved 1:通过审核的评论;0:待审核;trash:回收站;spam:垃圾评论
	$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // 发送 e-mail 地址, no-reply 根据实际修改为可用的 e-mail.
	if ( ($spam_confirmed == '1') &&  ( $to != $author_email ) ){
		$subject = 'Re:《'.get_the_title($comment->comment_post_ID).' - ' . 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_the_title($comment->comment_post_ID).' - ' . 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_the_title($comment->comment_post_ID).' - ' . 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 = "Reply-To: ".trim($comment->comment_author)."-".get_option('blogname')." <".$author_email."> " . "\n";
	wp_mail($to, $subject, $message_html,$headers);
	wp_mail($tobackup, $subject, $message_html,$headers);
}
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){//对每个子匹配都进行邮件发送操作
			comment_mail_notify_sendmail($parent_id,$comment);
		}
	} elseif ($comment->comment_parent!='0'){//以防万一,有人故意删了@回复,还可以通过查找父级评论id来确定邮件发送对象
		$parent_id=$comment->comment_parent;
		comment_mail_notify_sendmail($parent_id,$comment);
	} else {
		$parent_id=$comment_id;
		comment_mail_notify_sendmail($parent_id,$comment);
	}
}
add_action('comment_mail_notify_async', 'comment_mail_notify');//异步发送

function comment_mail_schedule_notify_async($comment_id) {
	//不延时仅仅异步发送
	//如果需要延时将 time() 改成 time()+33 形式,33表示延时33秒
	wp_schedule_single_event( time(), 'comment_mail_notify_async', [$comment_id] );
}
add_action('comment_post', 'comment_mail_schedule_notify_async');
add_action('wp_set_comment_status', 'comment_mail_schedule_notify_async');
/* 评论回复邮件通知 end */

WordPress 评论回复通知邮件代码(@ZE3kr 版本)

@ZE3kr 代码地址:Github,同样代码放主题 functions.php 文件即可(可能需要根据主题实际修改匹配)

<?php
function tlo_comment_mail_notify($comment_id) {
	global $comment_author;
	$comment = get_comment($comment_id);
	$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
	$spam_confirmed = $comment->comment_approved;
	$from = $comment->comment_author_email;
	$to = get_comment($parent_id)->comment_author_email;
	if (($parent_id != '') && ($spam_confirmed != 'spam') && $from != $to && $to != get_bloginfo('admin_email') ) {
		$blog_name = get_option('blogname');
		$blog_url = site_url();
		$post_url = get_permalink( $comment->comment_post_ID );
		$comment_author = $comment->comment_author;
		$subject = 'Re: '.html_entity_decode(get_the_title($comment->comment_post_ID));
		$headers[] = 'Reply-To: '.$comment_author.' <'.$comment->comment_author_email.'>';
		$comment_parent = get_comment($parent_id);
		$comment_parent_date = tlo_get_comment_date( $comment_parent );
		$comment_parent_time = tlo_get_comment_time( $comment_parent );
		$message = <<<HTML
<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
		<title>$blog_name</title>
	</head>
	<body>
		<style type="text/css">
		img {
			max-width: 100%; height: auto;
		}
		</style>
		<div class="content">
			<div>
				<p>$comment->comment_content</p>
			</div>
		</div>
		<div class="footer" style="margin-top: 10px">
			<p style="color: #777; font-size: small">
				&mdash;
				<br>
				直接回复此邮件可单独与回复者交流,或者<a href="$post_url#comment-$comment_id">在 $blog_name 上查看</a>。
				<br>
				你收到这封邮件是因为你在 $blog_name 上的评论得到了回复。
			</p>
		</div>
		<blockquote type="cite">
			<div>在 {$comment_parent_date},{$comment_parent_time},$comment_parent->comment_author &lt;<a href="mailto: $comment_parent->comment_author_email">$comment_parent->comment_author_email</a>&gt; 写到:</div>
			<br>
			<div class="content">
				<div>
					<p>$comment_parent->comment_content</p>
				</div>
			</div>
		</blockquote>
	</body>
</html>
HTML;
		add_filter( 'wp_mail_content_type', 'tlo_mail_content_type' );
		add_filter( 'wp_mail_from_name', 'tlo_mail_from_name' );
		wp_mail( $to, $subject, $message, $headers );
	}
}
add_action('tlo_comment_post_async', 'tlo_comment_mail_notify');

function tlo_comment_mail_notify_async($comment_id) {
	wp_schedule_single_event( time(), 'tlo_comment_post_async', [$comment_id] );
}
add_action('comment_post', 'tlo_comment_mail_notify_async');
// add_action('comment_post', 'tlo_comment_mail_notify');

function tlo_mail_content_type() {
	return 'text/html';
}
function tlo_mail_from_name() {
	global $comment_author;
	return $comment_author;
}

function tlo_get_comment_time( $comment ) {
	$date = mysql2date(get_option('time_format'), $comment->comment_date, true);

	return apply_filters( 'tlo_get_comment_time', $date, $comment );
}
function tlo_get_comment_date( $comment ) {
	$date = mysql2date(get_option('date_format'), $comment->comment_date);

	return apply_filters( 'tlo_get_comment_date', $date, $comment );
}

发布者:LiuCi

辛苦是获得一切的定律。