WordPress实现邮件自动回复评论人以及邮件通知内容的css样式

目录 Wordpress相关2017年4月2日

email

只要使用wordpress建站都必然遇到邮件回复相关的问题,本文做一个小小的整理,介绍一下wordpress修改默认邮箱发信人、wordpress为审核通过的评论人自动发送邮件的功能、wordpress为评论者收到回复后提供邮件通知的功能以及相关样式的修改。

修改邮箱的发信地址和发件人(仅适用于mail()函数发送邮件)

当我们使用wordpress的mail函数发送邮件时,默认的发件人是WordPress,默认的发件邮箱是wordpress@xxxx.com ,如果我们想修改地址可以用下面的代码实现:

  1. function new_from_name($email){
  2.     $wp_from_name = get_option('blogname');//换成别的名字
  3.     return $wp_from_name;
  4. }
  5. function new_from_email($email) {
  6.     $wp_from_email = get_option('admin_email');//换成想要设置的邮箱
  7.     return $wp_from_email;
  8. }
  9. add_filter('wp_mail_from_name', 'new_from_name');
  10. add_filter('wp_mail_from', 'new_from_email');

建议:因多数邮箱都把wordpress自动发的邮件当为垃圾邮件甚至根本收不到,建议不要使用wordpress发送邮件,使用第三方邮箱smtp发信才是最好的选择。

wordpress评论回复邮件通知功能

注意事项:本地编辑php文件时不要使用记事本,可以用UltraEdit、notepad++、Dreamweaver等工具;如果对于修改代码不是很有把握,请预先备份好原来的文件;以下代码都是放置于主题function.php文件<php?>之间(以下代码均收集与网络)

1.所有回复都发送邮件通知

  1. /* comment_mail_notify v1.0 by willin kan. (所有回复都发邮件) */
  2. function comment_mail_notify($comment_id) {
  3.   $comment = get_comment($comment_id);
  4.   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  5.   $spam_confirmed = $comment->comment_approved;
  6.   if (($parent_id != '') && ($spam_confirmed != 'spam')) {
  7.     $wp_email = 'no-reply@' . preg_replace('#^www\.#', ''strtolower($_SERVER['SERVER_NAME'])); //e-mail 发出点, no-reply 可改为可用的 e-mail.
  8.     $to = trim(get_comment($parent_id)->comment_author_email);
  9.     $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
  10.     $message = '
  11.     <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;">
  12.       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  13.       <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
  14.        . trim(get_comment($parent_id)->comment_content) . '</p>
  15.       <p>' . trim($comment->comment_author) . ' 给您的回复:<br />'
  16.        . trim($comment->comment_content) . '<br /></p>
  17.       <p>您可以点击 查看回复完整內容</p>
  18.       <p>欢迎再度光临 ' . get_option('blogname') . '</p>
  19.       <p>(此邮件由系统自动发送,请勿回复.)</p>
  20.     </div>';
  21.     $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  22.     $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  23.     wp_mail( $to$subject$message$headers );
  24.     //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  25.   }
  26. }
  27. add_action('comment_post', 'comment_mail_notify');
  28. // -- END ----------------------------------------

2.让访客自己选择是否邮件通知

启用该代码会在评论框下面生成一个按钮按钮如下图所示,让评论者要不要接收有人回复时的邮件通知。

  1. /* 开始*/  
  2. function comment_mail_notify($comment_id) {
  3.   $admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
  4.   $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
  5.   $comment = get_comment($comment_id);
  6.   $comment_author_email = trim($comment->comment_author_email);
  7.   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  8.   global $wpdb;
  9.   if ($wpdb->query("describe {$wpdb->comments} comment_mail_notify") == '')
  10.     $wpdb->query("alter table {$wpdb->comments} add column comment_mail_notify tinyint not null default 0;");
  11.   if (($comment_author_email != $admin_email && isset($_post['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
  12.     $wpdb->query("update {$wpdb->comments} set comment_mail_notify='1' where comment_id='$comment_id'");
  13.   $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  14.   $spam_confirmed = $comment->comment_approved;
  15.   if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
  16.     $wp_email = 'no-reply@' . preg_replace('#^www\.#', ''strtolower($_server['server_name'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
  17.     $to = trim(get_comment($parent_id)->comment_author_email);
  18.     $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
  19.     $message = '
  20.     <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;">
  21.       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  22.       <p>您曾在《' . get_the_title($comment->comment_post_id) . '》的留言:<br />'
  23.        . trim(get_comment($parent_id)->comment_content) . '</p>
  24.       <p>' . trim($comment->comment_author) . ' 给您的回复:<br />'
  25.        . trim($comment->comment_content) . '<br /></p>
  26.       <p>您可以点击查看回复的完整內容</p>   
  27.       <p>还要再度光临 ' . get_option('blogname') . '</p>   
  28.       <p>(此邮件由系统自动发送,请勿回复.)</p>   
  29.     </div>';   
  30.     $from = "from: \"" . get_option('blogname') . "\" <$wp_email>";
  31.     $headers = "$from\ncontent-type: text/html; charset=" . get_option('blog_charset') . "\n";
  32.     wp_mail( $to$subject$message$headers );
  33.     //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  34.   }   
  35. }   
  36. add_action('comment_post', 'comment_mail_notify');   
  37.     
  38. /* 自动加勾选栏 */
  39. function add_checkbox() {
  40.   echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label>';
  41. }   
  42. add_action('comment_form', 'add_checkbox');  

3.让博客管理员决定什么情况下发邮件

  1. /* comment_mail_notify v1.0 by willin kan. (无勾选栏) */  
  2. function comment_mail_notify($comment_id) {
  3.   $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
  4.   $comment = get_comment($comment_id);
  5.   $comment_author_email = trim($comment->comment_author_email);
  6.   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  7.   $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  8.   $spam_confirmed = $comment->comment_approved;
  9.   if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
  10.     /* 上面的判断式,决定发出邮件的必要条件:
  11.     ($parent_id != '') && ($spam_confirmed != 'spam'): 回复的, 而且不是 spam 才可发, 必需!!  
  12.     ($to != $admin_email) : 不发给 admin.  
  13.     ($comment_author_email == $admin_email) : 只有 admin 的回复才可发.  
  14.     可视个人需修改上面的条件.  
  15.     */  
  16.     $wp_email = 'no-reply@' . preg_replace('#^www\.#', ''strtolower($_server['server_name'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
  17.     $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
  18.     $message = '
  19.     <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;">
  20.       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  21.       <p>您曾在《' . get_the_title($comment->comment_post_id) . '》的留言:<br />'
  22.        . trim(get_comment($parent_id)->comment_content) . '</p>
  23.       <p>' . trim($comment->comment_author) . ' 给您的回复:<br />'
  24.        . trim($comment->comment_content) . '<br /></p>
  25.       <p>您可以点击 查看回复的完整內容</p>   
  26.       <p>还要再度光临 ' . get_option('blogname') . '</p>   
  27.       <p>(此邮件由系统自动发送,请勿回复.)</p>   
  28.     </div>';   
  29.     $from = "from: \"" . get_option('blogname') . "\" <$wp_email>";
  30.     $headers = "$from\ncontent-type: text/html; charset=" . get_option('blog_charset') . "\n";
  31.     wp_mail( $to$subject$message$headers );
  32.     //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  33.   }   
  34. }
  35. add_action('comment_post', 'comment_mail_notify');   
  36. // -- end ----------------------------------------

4.通知评论回复者(不包括博主)

  1. add_action('comment_post','CommentsReplyNotification');
  2. function CommentsReplyNotification($comment_id){
  3. //取得插入评论的id
  4. $c = get_comment($comment_id);
  5. //取得评论的父级id
  6. $comment_parent = $c->comment_parent;
  7. //取得评论的内容
  8. $c_content = $c->comment_content;
  9. //评论者email
  10. $c_author_email = $c->comment_author_email;
  11. if($comment_parent != 0){
  12. $pc = get_comment($comment_parent);
  13. $comment_ID = $pc->comment_ID;
  14. $comment_author = $pc->comment_author;
  15. $comment_author_email = $pc->comment_author_email;
  16. $comment_post_ID = $pc->comment_post_ID;
  17. $comment_content = $pc->comment_content;
  18. $ps = get_post($comment_post_ID);
  19. $author_id = $ps->post_author;
  20. $u_email = get_user_meta($author_id,'email',true);
  21. //判断自己的回复,如果自己参与评论,不给自己发送邮件通知
  22. if($c_author_email == $comment_author_email || $comment_author_email == $u_email ){
  23. return;
  24. }
  25. $post_title = $ps->post_title;
  26. $link = get_permalink($comment_post_ID);
  27. //邮件内容,可以自定义内容
  28. $content = "尊敬的".$comment_author."您好,你发布于\" ".$post_title."\"的评论:\r\n".$comment_content."\r\n有了回复:\r\n".$c_content."\r\n点击链接回复评论:".$link."#comment-".$comment_ID;
  29. //发送邮件
  30. wp_mail($comment_author_email,'评论回复:'.$post_title$content);
  31. }
  32. }

邮件通知内容样式美化

我们如果根据上面代码设置后给访客的回复界面是非常简陋的,如下图所示:email通知

邮件通知的样式改变一下说不定会留住更多的访客,样式美化后如图:email通知

样式美化后是不是好看了许多,如果你有心建站的话邮件通知的样式也要考虑到。

  1. <div style="background:#ececec;width: 100%;padding: 50px 0;text-align:center;">
  2.        <div style="background:#fff;width:750px;text-align:left;position:relative;margin:0 auto;font-size:14px;line-height:1.5;">
  3.              <div style="zoom:1;padding:25px 40px;background:#518bcb; border-bottom:1px solid #467ec3;">
  4.                     <h1 style="color:#fff; font-size:25px;line-height:30px; margin:0;"><a href="' . get_option('home') . '" style="text-decoration: none;color: #FFF;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</a>                               </h1>
  5.                     </div>
  6.              <div style="padding:35px 40px 30px;">
  7.                     <h2 style="font-size:18px;margin:5px 0;">Hi ' . trim(get_comment($parent_id)->comment_author) . ':</h2>
  8.                     <p style="color:#313131;line-height:20px;font-size:15px;margin:20px 0;">您有一条留言有了新的回复,摘要信息请见下表。</p>
  9.                         <table cellspacing="0" style="font-size:14px;text-align:center;border:1px solid #ccc;table-layout:fixed;width:500px;">
  10.                             <thead>
  11.                                 <tr>
  12.                                     <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="235px;">原文</th>
  13.                                     <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="235px;">回复</th>
  14.                                     <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="100px;">作者</th>
  15.                                     <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="90px;" >操作</th>
  16.                                 </tr>
  17.                             </thead>
  18.                             <tbody>
  19.                                 <tr>
  20.                                     <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . trim(get_comment($parent_id)->comment_content) . '</td>
  21.                                     <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">'. trim($comment->comment_content) . '</td>
  22.                                     <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">' . trim($comment->comment_author) . '</td>
  23.                                     <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><a href="'.get_comment_link( $comment->comment_ID ).'" style="color:#1E5494;text-decoration:none;vertical-align:middle;" target="_blank">查看回复</a></td>
  24.                                 </tr>
  25.                             </tbody>
  26.                         </table>
  27.                         <br>
  28.                     <div style="font-size:13px;color:#a0a0a0;padding-top:10px">该邮件由系统自动发出,如果不是您本人操作,请忽略此邮件。</div>
  29.                     <div class="qmSysSign" style="padding-top:20px;font-size:12px;color:#a0a0a0;">
  30.                         <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</p>
  31.                         <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;"><span style="border-bottom:1px dashed #ccc;" t="5" times="">' . date("Y年m月d日",time()) . '</span></p>
  32.                     </div>
  33.                 </div>
  34.             </div>
  35.         </div>

把上述代码替换掉前面邮件回复相关代码$message = ''$content = ""里面的内容即可。

代码来自 VTROIS'S BLOG,原文地址:https://www.vtrois.com/wordpress-theme-email-receipt-system.html

评论审核通过通知留言者

当开启评论审核时,以下代码可以实现审核通过的评论自动发送邮件给评论者。

  1. //评论邮件回复系统
  2. add_action('comment_unapproved_to_approved', 'sirius_comment_approved');
  3. function sirius_comment_approved($comment) {
  4.     if(is_email($comment->comment_author_email)) {
  5.         $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
  6.         $to = trim($comment->comment_author_email);
  7.         $post_link = get_permalink($comment->comment_post_ID);
  8.         $subject = '[通知]您的留言已经通过审核';
  9.         $message = '
  10.             <div style="background:#ececec;width: 100%;padding: 50px 0;text-align:center;">
  11.             <div style="background:#fff;width:750px;text-align:left;position:relative;margin:0 auto;font-size:14px;line-height:1.5;">
  12.                     <div style="zoom:1;padding:25px 40px;background:#518bcb; border-bottom:1px solid #467ec3;">
  13.                         <h1 style="color:#fff; font-size:25px;line-height:30px; margin:0;"><a href="' . get_option('home') . '" style="text-decoration: none;color: #FFF;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</a></h1>
  14.                     </div>
  15.                 <div style="padding:35px 40px 30px;">
  16.                     <h2 style="font-size:18px;margin:5px 0;">Hi ' . trim($comment->comment_author) . ':</h2>
  17.                     <p style="color:#313131;line-height:20px;font-size:15px;margin:20px 0;">您有一条留言通过了管理员的审核并显示在文章页面,摘要信息请见下表。</p>
  18.                         <table cellspacing="0" style="font-size:14px;text-align:center;border:1px solid #ccc;table-layout:fixed;width:500px;">
  19.                             <thead>
  20.                                 <tr>
  21.                                     <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="280px;">文章</th>
  22.                                     <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="270px;">内容</th>
  23.                                     <th style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-weight:normal;color:#a0a0a0;background:#eee;border-color:#dfdfdf;" width="110px;" >操作</th>
  24.                                 </tr>
  25.                             </thead>
  26.                             <tbody>
  27.                                 <tr>
  28.                                     <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">《' . get_the_title($comment->comment_post_ID) . '》</td>
  29.                                     <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">'. trim($comment->comment_content) . '</td>
  30.                                     <td style="padding:5px 0;text-indent:8px;border:1px solid #eee;border-width:0 1px 1px 0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;"><a href="'.get_comment_link( $comment->comment_ID ).'" style="color:#1E5494;text-decoration:none;vertical-align:middle;" target="_blank">查看留言</a></td>
  31.                                 </tr>
  32.                             </tbody>
  33.                         </table>
  34.                         <br>
  35.                     <div style="font-size:13px;color:#a0a0a0;padding-top:10px">该邮件由系统自动发出,如果不是您本人操作,请忽略此邮件。</div>
  36.                     <div class="qmSysSign" style="padding-top:20px;font-size:12px;color:#a0a0a0;">
  37.                         <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</p>
  38.                         <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;"><span style="border-bottom:1px dashed #ccc;" t="5" times="">' . date("Y年m月d日",time()) . '</span></p>
  39.                     </div>
  40.                 </div>
  41.             </div>
  42.         </div>';
  43.         $from = "From: \"" . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . "\" <$wp_email>";
  44.         $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  45.         wp_mail( $to, $subject, $message, $headers );
  46.     }
  47. }

暂无评论

发表评论