[代码版] 有人回复时邮件通知我

林继 WordPress 6,939 共写了118个字 (2010-08-31 21:29:25) 1条评论 打印 扫描二维码 百度已收录

直接加入主题下面的 functions.php 文件里。

  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>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回应完整内容</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. /* 自动加勾选栏 */
  39. function add_checkbox() {
  40.   echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="width: auto;margin-left:60px;text-align:center;" /><label for="comment_mail_notify" style="width: auto;margin-left:-185px;">有人回复时邮件通知我</label>';
  41. }
  42. add_action('comment_form', 'add_checkbox');

留言短信通知,何需插件?直接在邮箱回复的代码后面添加一段代码即可

  1. <?php
  2.  
  3. /* comment_mail_notify v1.0 by willin kan. (有勾選欄, 由訪客決定) */
  4. function comment_mail_notify($comment_id) 
  5. {
  6. $admin_notify = '0'; // admin 要不要收回覆通知 ( '1'=要 ; '0'=不要 )
  7. $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
  8. $comment = get_comment($comment_id);
  9. $comment_author_email = trim($comment->comment_author_email);
  10. $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  11. global $wpdb;
  12. if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
  13. $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  14. if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
  15. $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
  16. $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  17. $spam_confirmed = $comment->comment_approved;
  18. if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
  19. $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['HTTP_HOST'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
  20. $to = trim(get_comment($parent_id)->comment_author_email);
  21. $subject = '您在 [' . get_option("blogname") . '] 的留言有了新回复';
  22. $message = '
  23. <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;">
  24. <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  25. <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />
  26. ' . trim(get_comment($parent_id)->comment_content) . '</p>
  27. <p>' . trim($comment->comment_author) . ' 给您的回复:<br />
  28. ' . trim($comment->comment_content) . '<br /></p>
  29. <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '"> 查看完整回复內容</a></p>
  30. <p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
  31. <p>(此邮件由系统自动发出, 请勿回复.)</p>
  32. </div>';
  33. $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  34. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  35. @wp_mail( $to, $subject, $message, $headers );
  36. //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  37.  
  38. }
  39.  
  40.  
  41. //发信到139邮箱
  42. ##########################################################
  43. if($admin_email != $comment_author_email )
  44. {
  45. $to='你的139邮箱账号(如xxx@139.com)';
  46. $from = "From: \"" . get_option('blogname') . "\" <$admin_email>";
  47. $subject = trim($comment->comment_author) .'在 [' . get_the_title($comment->comment_post_ID) . '] 留言:';
  48. $message =trim($comment->comment_content);
  49. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  50. @wp_mail( $to, $subject, $message, $headers );
  51. }
  52. ##########################################################
  53.  
  54.  
  55. }
  56. add_action('comment_post', 'comment_mail_notify');
  57.  
  58. /* 自動加勾選欄 */
  59. function add_checkbox() {
  60. echo '<input style="margin-left:20px;width:20px;" type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" /><label for="comment_mail_notify">有人回复时邮件通知我</label>';
  61. }
  62. add_action('comment_form', 'add_checkbox');
  63. // -- END ----------------------------------------

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

一条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

< >