WordPress实现邮件自动回复评论人以及邮件通知内容的css样式
只要使用wordpress建站都必然遇到邮件回复相关的问题,本文做一个小小的整理,介绍一下wordpress修改默认邮箱发信人、wordpress为审核通过的评论人自动发送邮件的功能、wordpress为评论者收到回复后提供邮件通知的功能以及相关样式的修改。
修改邮箱的发信地址和发件人(仅适用于mail()函数发送邮件)
当我们使用wordpress的mail函数发送邮件时,默认的发件人是WordPress
,默认的发件邮箱是wordpress@xxxx.com
,如果我们想修改地址可以用下面的代码实现:
- function new_from_name($email){
- $wp_from_name = get_option('blogname');//换成别的名字
- return $wp_from_name;
- }
- function new_from_email($email) {
- $wp_from_email = get_option('admin_email');//换成想要设置的邮箱
- return $wp_from_email;
- }
- add_filter('wp_mail_from_name', 'new_from_name');
- add_filter('wp_mail_from', 'new_from_email');
建议:因多数邮箱都把wordpress自动发的邮件当为垃圾邮件甚至根本收不到,建议不要使用wordpress发送邮件,使用第三方邮箱smtp发信才是最好的选择。
wordpress评论回复邮件通知功能
注意事项:本地编辑php文件时不要使用记事本,可以用UltraEdit、notepad++、Dreamweaver等工具;如果对于修改代码不是很有把握,请预先备份好原来的文件;以下代码都是放置于主题function.php
文件<php
和?>
之间(以下代码均收集与网络)
1.所有回复都发送邮件通知
- /* comment_mail_notify v1.0 by willin kan. (所有回复都发邮件) */
- 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 发出点, no-reply 可改为可用的 e-mail.
- $to = trim(get_comment($parent_id)->comment_author_email);
- $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
- $message = '
- <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;">
- <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
- <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
- . trim(get_comment($parent_id)->comment_content) . '</p>
- <p>' . trim($comment->comment_author) . ' 给您的回复:<br />'
- . trim($comment->comment_content) . '<br /></p>
- <p>您可以点击 查看回复完整內容</p>
- <p>欢迎再度光临 ' . get_option('blogname') . '</p>
- <p>(此邮件由系统自动发送,请勿回复.)</p>
- </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 );
- //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
- }
- }
- add_action('comment_post', 'comment_mail_notify');
- // -- END ----------------------------------------
2.让访客自己选择是否邮件通知
启用该代码会在评论框下面生成一个按钮按钮如下图所示,让评论者要不要接收有人回复时的邮件通知。
- /* 开始*/
- function comment_mail_notify($comment_id) {
- $admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
- $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
- $comment = get_comment($comment_id);
- $comment_author_email = trim($comment->comment_author_email);
- $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
- global $wpdb;
- if ($wpdb->query("describe {$wpdb->comments} comment_mail_notify") == '')
- $wpdb->query("alter table {$wpdb->comments} add column comment_mail_notify tinyint not null default 0;");
- if (($comment_author_email != $admin_email && isset($_post['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
- $wpdb->query("update {$wpdb->comments} set comment_mail_notify='1' where comment_id='$comment_id'");
- $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
- $spam_confirmed = $comment->comment_approved;
- if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
- $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_server['server_name'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
- $to = trim(get_comment($parent_id)->comment_author_email);
- $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
- $message = '
- <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;">
- <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
- <p>您曾在《' . get_the_title($comment->comment_post_id) . '》的留言:<br />'
- . trim(get_comment($parent_id)->comment_content) . '</p>
- <p>' . trim($comment->comment_author) . ' 给您的回复:<br />'
- . trim($comment->comment_content) . '<br /></p>
- <p>您可以点击查看回复的完整內容</p>
- <p>还要再度光临 ' . get_option('blogname') . '</p>
- <p>(此邮件由系统自动发送,请勿回复.)</p>
- </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 );
- //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
- }
- }
- add_action('comment_post', 'comment_mail_notify');
- /* 自动加勾选栏 */
- function add_checkbox() {
- 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>';
- }
- add_action('comment_form', 'add_checkbox');
3.让博客管理员决定什么情况下发邮件
- /* comment_mail_notify v1.0 by willin kan. (无勾选栏) */
- function comment_mail_notify($comment_id) {
- $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
- $comment = get_comment($comment_id);
- $comment_author_email = trim($comment->comment_author_email);
- $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
- $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
- $spam_confirmed = $comment->comment_approved;
- if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
- /* 上面的判断式,决定发出邮件的必要条件:
- ($parent_id != '') && ($spam_confirmed != 'spam'): 回复的, 而且不是 spam 才可发, 必需!!
- ($to != $admin_email) : 不发给 admin.
- ($comment_author_email == $admin_email) : 只有 admin 的回复才可发.
- 可视个人需修改上面的条件.
- */
- $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_server['server_name'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
- $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
- $message = '
- <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;">
- <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
- <p>您曾在《' . get_the_title($comment->comment_post_id) . '》的留言:<br />'
- . trim(get_comment($parent_id)->comment_content) . '</p>
- <p>' . trim($comment->comment_author) . ' 给您的回复:<br />'
- . trim($comment->comment_content) . '<br /></p>
- <p>您可以点击 查看回复的完整內容</p>
- <p>还要再度光临 ' . get_option('blogname') . '</p>
- <p>(此邮件由系统自动发送,请勿回复.)</p>
- </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 );
- //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
- }
- }
- add_action('comment_post', 'comment_mail_notify');
- // -- end ----------------------------------------
4.通知评论回复者(不包括博主)
- add_action('comment_post','CommentsReplyNotification');
- function CommentsReplyNotification($comment_id){
- //取得插入评论的id
- $c = get_comment($comment_id);
- //取得评论的父级id
- $comment_parent = $c->comment_parent;
- //取得评论的内容
- $c_content = $c->comment_content;
- //评论者email
- $c_author_email = $c->comment_author_email;
- if($comment_parent != 0){
- $pc = get_comment($comment_parent);
- $comment_ID = $pc->comment_ID;
- $comment_author = $pc->comment_author;
- $comment_author_email = $pc->comment_author_email;
- $comment_post_ID = $pc->comment_post_ID;
- $comment_content = $pc->comment_content;
- $ps = get_post($comment_post_ID);
- $author_id = $ps->post_author;
- $u_email = get_user_meta($author_id,'email',true);
- //判断自己的回复,如果自己参与评论,不给自己发送邮件通知
- if($c_author_email == $comment_author_email || $comment_author_email == $u_email ){
- return;
- }
- $post_title = $ps->post_title;
- $link = get_permalink($comment_post_ID);
- //邮件内容,可以自定义内容
- $content = "尊敬的".$comment_author."您好,你发布于\" ".$post_title."\"的评论:\r\n".$comment_content."\r\n有了回复:\r\n".$c_content."\r\n点击链接回复评论:".$link."#comment-".$comment_ID;
- //发送邮件
- wp_mail($comment_author_email,'评论回复:'.$post_title, $content);
- }
- }
邮件通知内容样式美化
我们如果根据上面代码设置后给访客的回复界面是非常简陋的,如下图所示:
邮件通知的样式改变一下说不定会留住更多的访客,样式美化后如图:
样式美化后是不是好看了许多,如果你有心建站的话邮件通知的样式也要考虑到。
- <div style="background:#ececec;width: 100%;padding: 50px 0;text-align:center;">
- <div style="background:#fff;width:750px;text-align:left;position:relative;margin:0 auto;font-size:14px;line-height:1.5;">
- <div style="zoom:1;padding:25px 40px;background:#518bcb; border-bottom:1px solid #467ec3;">
- <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>
- </div>
- <div style="padding:35px 40px 30px;">
- <h2 style="font-size:18px;margin:5px 0;">Hi ' . trim(get_comment($parent_id)->comment_author) . ':</h2>
- <p style="color:#313131;line-height:20px;font-size:15px;margin:20px 0;">您有一条留言有了新的回复,摘要信息请见下表。</p>
- <table cellspacing="0" style="font-size:14px;text-align:center;border:1px solid #ccc;table-layout:fixed;width:500px;">
- <thead>
- <tr>
- <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>
- <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>
- <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>
- <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>
- </tr>
- </thead>
- <tbody>
- <tr>
- <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>
- <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>
- <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>
- <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>
- </tr>
- </tbody>
- </table>
- <br>
- <div style="font-size:13px;color:#a0a0a0;padding-top:10px">该邮件由系统自动发出,如果不是您本人操作,请忽略此邮件。</div>
- <div class="qmSysSign" style="padding-top:20px;font-size:12px;color:#a0a0a0;">
- <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</p>
- <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>
- </div>
- </div>
- </div>
- </div>
把上述代码替换掉前面邮件回复相关代码$message = ''
或$content = ""
里面的内容即可。
代码来自 VTROIS'S BLOG,原文地址:https://www.vtrois.com/wordpress-theme-email-receipt-system.html。
评论审核通过通知留言者
当开启评论审核时,以下代码可以实现审核通过的评论自动发送邮件给评论者。
- //评论邮件回复系统
- add_action('comment_unapproved_to_approved', 'sirius_comment_approved');
- function sirius_comment_approved($comment) {
- if(is_email($comment->comment_author_email)) {
- $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
- $to = trim($comment->comment_author_email);
- $post_link = get_permalink($comment->comment_post_ID);
- $subject = '[通知]您的留言已经通过审核';
- $message = '
- <div style="background:#ececec;width: 100%;padding: 50px 0;text-align:center;">
- <div style="background:#fff;width:750px;text-align:left;position:relative;margin:0 auto;font-size:14px;line-height:1.5;">
- <div style="zoom:1;padding:25px 40px;background:#518bcb; border-bottom:1px solid #467ec3;">
- <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>
- </div>
- <div style="padding:35px 40px 30px;">
- <h2 style="font-size:18px;margin:5px 0;">Hi ' . trim($comment->comment_author) . ':</h2>
- <p style="color:#313131;line-height:20px;font-size:15px;margin:20px 0;">您有一条留言通过了管理员的审核并显示在文章页面,摘要信息请见下表。</p>
- <table cellspacing="0" style="font-size:14px;text-align:center;border:1px solid #ccc;table-layout:fixed;width:500px;">
- <thead>
- <tr>
- <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>
- <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>
- <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>
- </tr>
- </thead>
- <tbody>
- <tr>
- <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>
- <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>
- <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>
- </tr>
- </tbody>
- </table>
- <br>
- <div style="font-size:13px;color:#a0a0a0;padding-top:10px">该邮件由系统自动发出,如果不是您本人操作,请忽略此邮件。</div>
- <div class="qmSysSign" style="padding-top:20px;font-size:12px;color:#a0a0a0;">
- <p style="color:#a0a0a0;line-height:18px;font-size:12px;margin:5px 0;">' . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . '</p>
- <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>
- </div>
- </div>
- </div>
- </div>';
- $from = "From: \"" . htmlspecialchars_decode(get_option('blogname'), ENT_QUOTES) . "\" <$wp_email>";
- $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
- wp_mail( $to, $subject, $message, $headers );
- }
- }
发表评论
要发表评论,您必须先登录。