利用十一假期给WP换了身新衣服,感觉还不错。其中看到别人的博客有回复评论通过邮件通知的功能,感觉很贴心。现在把实现的过程记录在此。
首先找到主题包中的functions.php文件中加入如下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | //* 邮件通知 by Qiqiboy */ function comment_mail_notify($comment_id) { $comment = get_comment($comment_id);//根据id获取这条评论相关数据 $content=$comment->comment_content;<BR ?>//对评论内容进行匹配 $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){//对每个子匹配都进行邮件发送操作 SimPaled_send_email($parent_id,$comment); } }elseif($comment->comment_parent!='0'){//以防万一,有人故意删了@回复,还可以通过查找父级评论id来确定邮件发送对象 $parent_id=$comment->comment_parent; SimPaled_send_email($parent_id,$comment); }else return; } add_action('comment_post', 'comment_mail_notify'); function SimPaled_send_email($parent_id,$comment){//发送邮件的函数 by Qiqiboy.com $admin_email = get_bloginfo ('admin_email');//管理员邮箱 $parent_comment=get_comment($parent_id);//获取被回复人(或叫父级评论)相关信息 $author_email=$comment->comment_author_email;//评论人邮箱 $to = trim($parent_comment->comment_author_email);//被回复人邮箱 $spam_confirmed = $comment->comment_approved; if ($spam_confirmed != 'spam' && $to != $admin_email && $to != $author_email) { $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail. $subject = '您在 [' . get_option("blogname") . '] 的留言有了回應'; $message = ' </a> <div style="background-color: #eef2fa; border: 1px solid #d8e3e8; color: #111; padding: 0 15px; -moz-border-radius: 5px; -webkit-border-radius: 5px; -khtml-border-radius: 5px;"> ' . trim(get_comment($parent_id)->comment_author) . ', 您好! 您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言: ' . trim(get_comment($parent_id)->comment_content) . ' ' . trim($comment->comment_author) . ' 给你的回复: ' . trim($comment->comment_content) . ' 您可以点击 <a type="" href="' . htmlspecialchars(get_comment_link($parent_id,array(">"all"))) . '">查看回复的完整內容</a> 欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a> (此邮件有系统自动发出, 请勿回复.) </div> '; $from = "From: "" . get_option('blogname') . "" "; $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n"; wp_mail( $to, $subject, $message, $headers ); } } |
之后,新增插件“Configure SMTP”进行设置。如果域名还没有关联邮箱,那么可以申请QQ免费的企业邮箱绑定。点击注册。申请好后在“Configure SMTP”下的设置如下:
保存后,试着发一封邮件,收到后就表示成功了。现在有人回复了你的文章,并得到了你的回复后,他填写的邮箱就会收到一封通知了。
其实也可以用插件来实现。不过对于插件不感冒的人来说,还是很有用的 😉
Thanks for finally writing about >Wordpress 实现评论邮件通知 :
空无一人的沙滩 <Liked it!
whoah this blog is magnificent i really like studying your
posts. Keep up the great work! You understand, lots of persons are
searching around for this information, you can help them greatly.
I’ve read several excellent stuff here. Definitely value bookmarking for revisiting. I surprise how so much attempt you set to create this sort of excellent informative web site.