wordpress实现回复文章评论后发送邮件通知的功能

[复制链接]
树苗收集系 发表于 2019-11-26 23:55:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题
本文实例讲述了WordPress实现回复文章评论后发送邮件通知的功能。分享给大家供大家参考,具体如下:

很多时候,人们都希望在自己的评论被管理员回复后会收到通知。该函数的作用就是回复后自动邮件通知评论者。

把下面的代码加到wordpress的主题函数里面,然后修改下邮件帐号密码。

该函数是针对SAE平台的wordpress,非SAE平台不能使用,有需要的话留言我也会写出相应方法。
  1. //邮件回复
  2. function comment_mail_notify($comment_id) {
  3. define('MAIL_SMTP', 'smtp.exmail.qq.com'); //smtp服务器
  4. define('MAIL_PORT', 25); //smtp端口
  5. define('MAIL_SENDEMAIL', '123456789@qq.com'); //发送邮件帐号
  6. define('MAIL_PASSWORD', '123456'); //发送邮件密码
  7. $admin_notify = '1';
  8. $admin_email = get_bloginfo ('admin_email');
  9. $comment = get_comment($comment_id);
  10. $comment_author_email = trim($comment->comment_author_email);
  11. $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  12. global $wpdb;
  13. if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
  14. $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  15. if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
  16. $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
  17. $notify = $parent_id ? '1' : '0';
  18. $spam_confirmed = $comment->comment_approved;
  19. if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
  20. $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
  21. $to = trim(get_comment($parent_id)->comment_author_email);
  22. $subject = '你在' . get_option("blogname") . '回复被关注啦~';
  23. $message = '
  24. <div style="width: 502px; height: auto; margin-bottom: 50px; margin-left: auto; margin-right: auto; font-size: 13px; line-height: 14px;">
  25. <div style="width: 502px; margin-top: 10px;">
  26. <div style="font-size: 16px; color: #373737; text-align: center;">'.get_bloginfo("name").'</div>
  27. <div style="font-size: 15px; color: #f0f7eb; padding: 9px; margin-top: 20px; overflow: hidden; background: #299982; padding-left: 30px; padding-right: 40px;">你在 '. get_the_title($comment->comment_post_ID) .' 的评论有了回复:</div>
  28. </div>
  29. <div style="width: 420px; margin-top: 30px; padding: 0 40px 20px; border-left: 1px dashed #299982; border-right: 1px dashed #299982; color: rgba(0,0,0,0.7); background: #f9f9f9; overflow: hidden;">
  30. <div class="one origin" style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0;"><span style="color: #299982;">'. trim(get_comment($parent_id)->comment_author) .'</span>:'. trim(get_comment($parent_id)->comment_content) .'</div>
  31. <div class="one reply" style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0 1em 60px;"><span style="color: #299982;">'. trim($comment->comment_author) .'</span>:'. trim($comment->comment_content) .'</div>
  32. <p style="margin-bottom: 10px;">点击<a href="' . htmlspecialchars(get_comment_link($parent_id)) . ' style=">查看完整内容</a></p>
  33. <p style="margin-bottom: 10px;">(此邮件由系统发出,无需回复.)</p>
  34. </div>
  35. </div>
  36. ';
  37. $from = "From: "" . get_option('blogname') . "" <$wp_email>";
  38. $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  39. $mail = new SaeMail(); //对象
  40. $mail->setOpt(array( 'from' => 'admin@xtwind.com', 'to' => trim($to),//接收信箱
  41. 'smtp_host' => MAIL_SMTP , //host
  42. 'smtp_port' => MAIL_PORT, //port
  43. 'smtp_username' => MAIL_SENDEMAIL,
  44. 'smtp_password' => MAIL_PASSWORD,
  45. 'subject' => $subject,
  46. 'content' => $message,
  47. 'content_type' => 'HTML'
  48. // 'tls' => true,
  49. //'charset' => 'gbk' ) );
  50. $ret = $mail->send();
  51. }
  52. }
  53. add_action('comment_post', 'comment_mail_notify');
复制代码
如果使用上面的不行,可以看看下面的内容:

本文实例讲述了php使用SAE原生Mail类实现各种类型邮件发送的方法。分享给大家供大家参考,具体如下:

用过SAE的都知道,SAE所有服务中,就数Mail服务最不行了,时不时邮件就发不出去。特别是企业邮局,连新浪自家的企业邮局都出问题。今天就给出解决方案。

先来看看SAE文档中给出的DEMO:
  1. $mail = new SaeMail();
  2. $mail->setAttach( array( 'my_photo' => '照片的二进制数据' ) );//附件发送方法
  3. $ret = $mail->quickSend( 'to@sina.cn' , '邮件标题' , '邮件内容' , 'smtpaccount@unknown.com' , 'password' , 'smtp.unknown.com' , 25 ); // 指定smtp和端口
复制代码
SAE给出的这个DEMO使用的是quicksend()方法,该方法经我测试,在使用非企业邮局的时候是可以完美发送的,而且到信率很高。但是注意只能使用smtp的25端口,不能使用SSL连接,不知道是不是打开方式不对,希望高人指点。

但是对于网站来说,有一个自有的独立域名邮箱很重要,这时企业邮局就派上用场了。只是使用quicksend()方法总是发送失败。所以我们要使用send()方法来。send()方法使用稍微复杂些:

1、先设置发送参数setOpt(),设置的这个发送参数对quicksend()方法无效,只对send()有效。
  1. $mail = new SaeMail();
  2. $mail->setOpt(array(
  3.   'from' => '发件邮箱',
  4.   'to' => trim($to),//接收信箱
  5.   'smtp_host' => 'smtp服务器' ,
  6.   'smtp_port' => 25, //port
  7.   'smtp_username' => '账户全名',
  8.   'smtp_password' => '密码',
  9.   'subject' => '主题',
  10.   'content' => '内容',
  11.   'content_type' => 'HTML' //发送格式,默认是text
  12.         )
  13.       );
  14. $ret = $mail->send();
复制代码
如此,就可以。更多参数可以去官方文档查看。

由于本站只是对评论回复进行邮件提示,所以在send()并没有对SSL测试,有需要可以自己测试。

到此这就结束了,如果不喜欢该方法,还可以自己百度第三方的Mail类库,也是可以的。经测试发信与收信大概都在3秒以内,可以满足大部分需求了。

回复

使用道具 举报

发布主题
推荐阅读 更多
阅读排行 更多
广告位
全国统一客服电话
400-1234-5678

24x7小时免费咨询

  • 官方在线客服

    QQ客服:小西

    点击交谈

    QQ客服:良子

    点击交谈

    QQ客服:闵月

    点击交谈
  • 安徽省合肥市高新区创新产业园

  • 手机扫码查看手机版

    手机查找资源更方便

  • 扫一扫关注官方微信

    加入官方微信群