|
|
—
本帖被 ralix 设置为精华(2010-10-04)
—
这是由《Ajax comments回应邮件通知》所改进的评论回应邮件通知(Comment Mail Notify). 主要改进两个项目: 1. 所有内置嵌套模板都适用, 不只是用于Ajax comments. 2. 管理者在后台回覆也可自动发邮件. 效果: 安装步骤: 1. 如果你用了上一版的"Ajax comments 回应邮件通知", 请先在comments-ajax.php 删除上一版的代码. 2. 在下面三种方式, 选择你想用的代码, copy 到主题的functions.php 的<?php ..... ?> 区域内. 《评论回应邮件通知》的三种代码 一、有勾选栏, 由访客决定是否要回应邮件通知 二、无勾选栏, 由管理者决定在什么条件下发邮件 三、所有回覆都发邮件 必须注意的是: 你的服务器一定要有mail() 功能. 测试方式: 在登入页故意按下'忘记密码', 收到邮件就有mail() 功能; 没收到邮件的可以下课了. 请先安装好smtp 插件, 确定服务器可发信了再回来吧~ 一、有勾选栏, 由访客决定是否要回应邮件通知: (会在模板自动加勾选栏, 如果不想自动加, 可把后面一小段删除.) - /* comment_mail_notify v1.0 by willin kan. (有勾选栏, 由访客决定) */
- 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; 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>您可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回应完整内容</a></p>
- <p>欢迎再度光临<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></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');
- // -- END ----------------------------------------
二、无勾选栏, 由管理者决定在什么条件下发邮件: - /* 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; 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>您可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回应完整内容</a></p>
- <p>欢迎再度光临<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></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 ----------------------------------------
三、所有回覆都发邮件: (当然, 在底层的评论不发邮件, 回覆的才发) - /* 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; 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>您可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回应完整内容</a></p>
- <p>欢迎再度光临<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></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 ----------------------------------------
其实以上三段代码都是同一个程式, 内容几乎完全相同, 只是怕有人不会改, 出错时又来找我发问, 所以放了三种写法供各位直接copy. 这样就节省了一个插件 实现了回复邮件提醒了。 此篇文章整理至 willin的博客我使用的回复模板 供大家参考 - $subject = 'Your comment on [' . get_option("blogname") . '] just been replied by ' . trim($comment->comment_author) . ' ';
- $message = '
- <p>Hello,' . trim(get_comment($parent_id)->comment_author) . '.<br/>
- Your comment on "<strong>' . get_the_title($comment->comment_post_ID) . '</strong>" just been replied by <strong>' . trim($comment->comment_author) . ' </strong>.Why not check it right now?<br/>
- <div style="margin:5px;padding:5px;border:1px solid #eee;background-color:#f8f8f8;color:#aaa;">Your comment:<br />'. trim(get_comment($parent_id)->comment_content) . '<br /></div>
- <div style="margin-left:5px;margin-right:5px;padding:5px;border:1px solid #ccc;background-color:#f2f2f2;color:#333;">New reply:<br />'. trim($comment->comment_content) . '</div>
- <div style="margin-top:10px;padding-bottom:10px;border-bottom:1px solid #ccc;">
- <a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">View reply</a>,or click <a href="mailto:lin@blueandhack.com">here</a> to send mail to Admin</div>
- <div style="font-style:italic;margin-top:5px;">[DO Not reply this mail]</div>
- <p><a href="' . get_option('home') . '">' . get_option('blogname') . '</a>.Welcom to subscribe to our <a href="http://feed.blueandhack.com">BLUEANDHACK</a>.<br/>
- </div>';
剩下怎么发挥~全靠你了~哈哈~
|