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

所属分类: CMS教程 / WordPress 阅读数: 2056
收藏 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实现评论后可显示内容中附件下载地址的方法

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

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

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

WordPress后台地址被改导致无法登陆后台的简单解决方法

这篇文章主要介绍了WordPress后台地址被改导致无法登陆后台的简单解决方法,简单分析了后台无法登陆的原因与相应的解决方法,涉及针对wordpress配置项的简单修改,需要的朋友可以参考下
收藏 0 赞 0 分享

WordPress使用自定义文章类型实现任意模板的方法

这篇文章主要介绍了WordPress使用自定义文章类型实现任意模板的方法,可通过自定义文章类型来实现任意模版的使用,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

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

这篇文章主要介绍了WordPress实现回复文章评论后发送邮件通知的功能,涉及wordpress针对评论与邮件的相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

WordPres限制非管理员访问管理面板的方法

大家可能发现了当实现了前端用户中心,后台控制面板就失去了作用,那么限制其他用户进入后台控制面板就很有必要了!那么我们要怎么做呢?通过下面这篇文章分享的方法后,只有管理员才能进入,否则是其他注册用户进入则会跳转首页。下面来一起看看吧。
收藏 0 赞 0 分享

wordpress自定义上传文件类型的方法

这篇文章主要给大家介绍了wordpress自定义上传文件类型的方法,如WordPress默认允许上传 .exe 后缀名的可运行文件,那么我们怎么禁止用户在WordPress后台发表文章时上传 .exe 后缀名的文件呢?以及如何让wrodpress允许上传zip文件格式,下面来一起
收藏 0 赞 0 分享

WordPress取消英文标点符号自动替换中文标点符号的优雅方法

这篇文章主要介绍了WordPress取消英文标点符号自动替换中文标点符号的优雅方法,需要的朋友可以参考下
收藏 0 赞 0 分享

wordpress在安装使用中出现404、403、500及502问题的分析与解决方法

wordpress是很多新手站长搭建个人博客最喜爱的程序,但是最近在使用WordPress的时候遇到了一些问题,所以想着将遇到问题总结分享出来,下面这篇文章主要给大家介绍了关于wordpress在安装使用中出现404、403、500及502问题的解决方法,需要的朋友可以参考下。
收藏 0 赞 0 分享

WordPress中删除垃圾评论的方法

这篇文章主要介绍了WordPress中删除垃圾评论的方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多