在平时我们为站点实现邮件发送功能基本都是通过一些SMTP插件来实现,使用插件的好处就是配置简单,即装即用。今天我们分享的是一种不需要插件实现评论回复邮件提醒的方法。
实现方法
将下面代码修改后复制到functions.PHP文件,再测试发送邮件功能:
//评论回复邮件
function comment_mail_notify($comment_id) {
$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']));
//发件人e-mail地址
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回应';
$content='
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body style="width:100%;height:800px;background-color:#EEF3FA; font-size:14px;font-family:Microsoft YaHei;">
<div style="margin:100px auto;background-color:#fff; width:866px; border:1px solid #F1F0F0;box-shadow: 0 0 5px #F1F0F0;">
<div style="width:838px;height: 78px; padding-top: 10px;padding-left:28px; background-color:#F7F7F7;">
<a style="cursor:pointer; font-size:30px; color:#333;text-decoration: none; font-weight: bold;"
href="'. get_option('home') .'">' . get_option('blogname') . '</a><span
style="color:#999; font-size:14px;padding-left:20px;">一个技术型主机测评网站</span>
</div>
<div style="padding:30px;">
<div style="height:50px; line-height:50px; font-size:16px; color:#9e9e9e;">你有一条新的评论</div>
<div style="line-height:30px; font-size:16px; margin-bottom:20px; text-indent: 2em;">
您在<a href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a> 上的留言有回复啦!
</div>
<div style="line-height:40px; font-size:14px;">
<label style="color:#999;">评论人:</label>
<span style="color:#333;">'.trim($comment->comment_author).' </span>
</div>
<div style="line-height:40px; font-size:14px;">
<label style="color:#999;">评论地址:</label>
<a href="'.htmlspecialchars(get_comment_link($parent_id)).'" style="color:#333;">'.htmlspecialchars(get_comment_link($parent_id)).'</a>
</div>
<div style="line-height:40px; font-size:14px;">
<label style="color:#999;">评论时间:</label>
<span style="color:#333;">'.trim($comment->comment_date).'</span>
</div>
<div style="line-height:40px; font-size:14px;">
<label style="color:#999;">PS:</label>
<span style="color:#333;">请不要回复此邮件,此邮件来自博客自动发送</span>
</div>
</div>
</div>
</body>
</html>
';
$from = "From: "" . get_option('blogname') . "" <$wp_email>";
$headers = "$fromnContent-Type: text/html; charset=" . get_option('blog_charset') . "n";
wp_mail( $to, $subject, $content, $headers );
//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
此代码转自https://wpbom.com/2499.html,邮件内容样式由朱纯树博客修改。