开始之前请确认你的服务器可以使用 wp_mail 发送邮件,否则以下代码就不用看了。邮件模版写起来和普通网站模版是有些区别的,加上很多旧代码写的真是无法直视……
实现方法:
下面代码加到 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’)) {
$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
Content-Type: text/html; charset=” . get_option(‘blog_charset’) . “
”;
wp_mail( $to, $subject, $message, $headers );
}
}
add_action(‘comment_post’, ‘comment_mail_notify’);
请确保你的主题不包含评论回复邮件通知功能和相关功能插件,否则可能会发送多封邮件。