代码实现WordPress评论邮件通知功能

4
(1)

这篇文章说下如何实现WordPress的评论邮件通知功能。今天,古哥测试了下网站的注册登录功能,添加了滑动验证功能。然后,意外发现Godaddy的WordPress托管服务,自带了邮件功能,即注册时验证邮箱会给目标邮箱发送一封邮件,而古哥不需要自己设置发信服务器。

然后,就测试了下评论功能,会发送邮件到管理员邮箱。然后,我在网站里回复那条评论,目标邮箱收不到那条回复的邮件。一般是因为评论部分的邮件通知,是WordPress程序发送的邮件。而站长回复评论,对方收不到邮件的原因是主题没添加这个功能。下面就开始说主题如何添加这个功能。

代码实现WordPress评论邮件通知功能

PHP代码实现邮件通知功能

编辑你主题目录下的function.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 = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="056461686c6b45">[email protected]</a>' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); //改为你的邮箱 
 $to = trim(get_comment($parent_id)->comment_author_email); 
 $subject = '[' . get_option("blogname") . '] 您的留言有了新回复'; 
 $message = ' 
 <div style="width: 60%;margin: 0 auto"> 
 <div style="font-size: 28px;line-height: 28px;text-align: center;"><p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p></div> 
 <div style="border-bottom: 1px solid #eee;padding-top: 10px;"> 
 <p style="color: #999;">您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:</p> 
 <p style="font-size: 18px;">' . trim(get_comment($parent_id)->comment_content) . '</p> 
 </div> 
 <div style="border-bottom: 1px solid #eee;padding-top: 10px;"> 
 <p style="color: #999;">' . trim($comment->comment_author) . ' 给您的回复:</p> 
 <p style="font-size: 18px;">' . trim($comment->comment_content) . '</p>
 <p style="text-align: center;font-size: 12px;padding-bottom: 20px;"><a style="border: 1px solid #3297fb;color: #3297fb;padding: 7px 14px;text-decoration: none;-moz-border-radius: 4px;-webkit-border-radius: 4px;border-radius:4px;" href="' . esc_attr(get_comment_link($parent_id, array('type' => 'comment'))) . '">点击查看</a></p> 
 </div> <div style="font-size: 12px;color: #999;text-align: center;"> 
 <p>此邮件由系统自动发送,请勿回复</p> 
 <p>© <a href="http://www.newpm.net" style="color: #999;text-decoration: none;">' . get_option('blogname') . '</a></p> 
 </div> 
 </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', 'comment_mail_notify');
//评论邮件通知结束

详情如下图所示:

代码实现WordPress评论邮件通知功能

代码中的wp_email,大约第七行,表示发件所用的邮箱,如果主机自带发信功能,会直接用这个邮箱发信,而事实上你无法访问这个邮箱。不过,对于一些评论回复通知功能,站长也不用理会别人回复这个邮箱的内容。

代码中的message,大约第十行表示发送邮件的内容,可以自行修改样式,文中的样式如下图所示:

代码实现WordPress评论邮件通知功能

PHP代码实现Smtp发信功能

如果你的服务器或主机不支持发信功能,那么你需要再在function.php中添加如下代码,紧跟在刚才代码后面即可。

//使用smtp发邮件
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用SMTPAuth服务
$phpmailer->Port = 465; //SMTP邮件发送端口,常用端口有:25、465和587(后两个为ssl安全连接端口)。
$phpmailer->SMTPSecure ="ssl"; //是否通过 ssl 连接,如果端口为25,则此处将"ssl"改为空白即"",否则不必改动
$phpmailer->Host = "smtp.gmail.com"; // SMTP服务器地址,在邮箱设置或者帮助中心中可以找到
$phpmailer->Username = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="671214021509060a0227000a060e0b4904080a">[email protected]</a>"; //你的邮箱地址
$phpmailer->Password ="******"; //你的邮箱登陆密码
}
//使用smtp发邮件结束

使用上面的代码,需要你有一个可用邮箱,并且开通了Smtp服务,具体如何开通邮箱的帮助页面会有。如果有哪个不懂,可以在下方给古哥留言。

共计1人评分,平均4

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

文章目录

原创文章,作者:古哥,转载需经过作者授权同意,并附上原文链接:https://iymark.com/articles/67.html

(2)
微信公众号
古哥的头像古哥管理团队
上一篇 2020年09月14日 21:44
下一篇 2020年09月14日 21:56

你可能感兴趣的文章

发表回复

登录后才能评论
微信小程序
微信公众号