切换到宽版
  • 1422阅读
  • 4回复

【主题美化&增强系列】Comment Mail Notify 免插件实现嵌套回复邮件提醒发送 [复制链接]

上一主题 下一主题
 

只看楼主 倒序阅读 使用道具 0楼 发表于: 2010-10-04
— 本帖被 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 插件, 确定服务器可发信了再回来吧~

一、有勾选栏, 由访客决定是否要回应邮件通知:

(会在模板自动加勾选栏, 如果不想自动加, 可把后面一小段删除.)

  1. /* comment_mail_notify v1.0 by willin kan. (有勾选栏, 由访客决定) */
  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; 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>您可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回应完整内容</a></p>
  27.       <p>欢迎再度光临<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></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. function add_checkbox() {
  39.   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>';
  40. }
  41. add_action('comment_form', 'add_checkbox');
  42. // -- END ----------------------------------------


二、无勾选栏, 由管理者决定在什么条件下发邮件:

  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; 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>您可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回应完整内容</a></p>
  26.       <p>欢迎再度光临<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></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 ----------------------------------------


三、所有回覆都发邮件:

(当然, 在底层的评论不发邮件, 回覆的才发)

  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; 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>您可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回应完整内容</a></p>
  18.       <p>欢迎再度光临<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></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 ----------------------------------------



其实以上三段代码都是同一个程式, 内容几乎完全相同, 只是怕有人不会改, 出错时又来找我发问, 所以放了三种写法供各位直接copy.
这样就节省了一个插件
实现了回复邮件提醒了。

此篇文章整理至willin的博客

我使用的回复模板
供大家参考

  1.     $subject = 'Your comment on [' . get_option("blogname") . '] just been replied by ' . trim($comment->comment_author) . ' ';
  2.     $message = '
  3.     <p>Hello,' . trim(get_comment($parent_id)->comment_author) . '.<br/>
  4.     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/>
  5.     <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>
  6.     <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>
  7.     <div style="margin-top:10px;padding-bottom:10px;border-bottom:1px solid #ccc;">
  8.     <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>
  9.     <div style="font-style:italic;margin-top:5px;">[DO Not reply this mail]</div>
  10.     <p><a href="' . get_option('home') . '">' . get_option('blogname') . '</a>.Welcom to subscribe to our <a href="http://feed.blueandhack.com">BLUEANDHACK</a>.<br/>
  11.     </div>';


剩下怎么发挥~全靠你了~哈哈~

只看该作者 1楼 发表于: 2010-10-04
回 楼主(blueandhack) 的帖子
看了,感觉需要美化。最好美化成这样子的,同时支持嵌套评论。




[ 此帖被jinriit在2010-10-04 01:58重新编辑 ]

只看该作者 2楼 发表于: 2010-10-04
回 1楼(jinriit) 的帖子
……要是美化成那种有点难度的~~

只看该作者 3楼 发表于: 2010-10-04
回 2楼(blueandhack) 的帖子
很多这类的插件都可以自定义邮件通知内容的CSS样式的   上图那个也是无插件实现的 不过很遗憾不支持嵌套。

只看该作者 4楼 发表于: 2010-10-04
回 3楼(jinriit) 的帖子
我收到了你的邮件啊~
快速回复
限100 字节
 
上一个 下一个