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

所属分类: CMS教程 / WordPress 阅读数: 2042
收藏 0 赞 0 分享

本文实例讲述了WordPress实现回复文章评论后发送邮件通知的功能。分享给大家供大家参考,具体如下:

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

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

该函数是针对SAE平台的wordpress,非SAE平台不能使用,有需要的话留言我也会写出相应方法。

复制代码
代码如下:
//邮件回复
function comment_mail_notify($comment_id) {
define('MAIL_SMTP', 'smtp.exmail.qq.com'); //smtp服务器
define('MAIL_PORT', 25); //smtp端口
define('MAIL_SENDEMAIL', '123456789@qq.com'); //发送邮件帐号
define('MAIL_PASSWORD', '123456'); //发送邮件密码
$admin_notify = '1';
$admin_email = get_bloginfo ('admin_email');
$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 ? '1' : '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']));
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '你在' . get_option("blogname") . '回复被关注啦~';
$message = '
<div style="width: 502px; height: auto; margin-bottom: 50px; margin-left: auto; margin-right: auto; font-size: 13px; line-height: 14px;">
<div style="width: 502px; margin-top: 10px;">
<div style="font-size: 16px; color: #373737; text-align: center;">'.get_bloginfo("name").'</div>
<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-&gt;comment_post_ID) .' 的评论有了回复:</div>
</div>
<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;">
<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)-&gt;comment_author) .'</span>:'. trim(get_comment($parent_id)-&gt;comment_content) .'</div>
<div class="one reply" style="border: 1px solid #EEE; overflow: auto; padding: 10px; margin: 1em 0 1em 60px;"><span style="color: #299982;">'. trim($comment-&gt;comment_author) .'</span>:'. trim($comment-&gt;comment_content) .'</div>
<p style="margin-bottom: 10px;">点击<a href="' . htmlspecialchars(get_comment_link($parent_id)) . ' style=">查看完整内容</a></p>
<p style="margin-bottom: 10px;">(此邮件由系统发出,无需回复.)</p>
</div>
</div>
';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
$mail = new SaeMail(); //对象
$mail->setOpt(array( 'from' => 'admin@xtwind.com', 'to' => trim($to),//接收信箱
'smtp_host' => MAIL_SMTP , //host
'smtp_port' => MAIL_PORT, //port
'smtp_username' => MAIL_SENDEMAIL,
'smtp_password' => MAIL_PASSWORD,
'subject' => $subject,
'content' => $message,
'content_type' => 'HTML'
// 'tls' => true,
//'charset' => 'gbk' ) );
$ret = $mail->send();
}
}
add_action('comment_post', 'comment_mail_notify');

如果使用上面的不行,可以看看前面这篇文章:

php使用SAE原生Mail类实现各种类型邮件发送的方法

希望本文所述对大家基于wordpress的网站建设有所帮助。

更多精彩内容其他人还在看

WordPress博客系统搜索引擎优化seo全攻略

关于wordpress的seo,网上有很多类似的教程,比如月光博客的wordpress十大seo技巧等等,今天就结合自己的经验,谈谈wordpress的seo,需要的朋友可以参考下
收藏 0 赞 0 分享

无需插件wordpress生成静态缓存文件的方法

今天介绍的非插件搞定wordpress生成静态缓存文件方法,即可为大家去掉这款强大的wordpress缓存插件WP Super Cache的使用
收藏 0 赞 0 分享

wordpress静态化首页及去除url中的index.html

使用wordpass博客程序的朋友都知道他的默认首页是index.php,然而地球人都知道index.html的太首页能够提高网站首页的访问速度。原因的话。地球人都知道的
收藏 0 赞 0 分享

优化WordPress分类链接及WP-No-Category-Base的卸载方法

这篇文章主要介绍了优化WordPress分类链接及WP-No-Category-Base的卸载方法 ,需要的朋友可以参考下
收藏 0 赞 0 分享

wordpress定时任务(wp-cron.php)造成主机CPU比较高的解决办法

这篇文章主要介绍了wordpress定时任务(wp-cron.php)造成主机CPU比较高的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

php-cgi进程cpu负载过高:禁用wordpress定时任务wp-cron.php

这篇文章主要介绍了php-cgi进程cpu负载过高:禁用wordpress定时任务wp-cron.php,需要的朋友可以参考下
收藏 0 赞 0 分享

WORDPRESS文章中微信打赏功能的2种实现方法

这篇文章主要介绍了WORDPRESS文章中微信打赏功能的2种实现方法,结合实例形式分析了2种简单的微信打赏实现技巧,涉及针对手机端的判断与微信支付二维码的调用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

WordPress在SAE平台实现的静态化插件及用法示例

这篇文章主要介绍了WordPress在SAE平台实现的静态化插件及用法,分析了静态化插件的原理与实现方法,并提供了插件下载地址,需要的朋友可以参考下
收藏 0 赞 0 分享

WordPress实现评论后可显示内容中附件下载地址的方法

这篇文章主要介绍了WordPress实现评论后可显示内容中附件下载地址的方法,可实现用户在评论后显示内容中附件下载地址的功能,涉及wordpress针对数据库的相关操作与判定技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

WordPress实现的首页幻灯片展示功能示例【附demo源码】

这篇文章主要介绍了WordPress实现的首页幻灯片展示功能的方法,可实现后台勾选推送到首页幻灯片的功能,末尾还附带demo源码供读者下载参考,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多