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

教程大全 2026-01-08 02:03:49 浏览

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

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

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 = '
'.get_bloginfo("name").'
你在 '. get_the_title($comment->comment_post_ID) .' 的评论有了回复:
'. trim(get_comment($parent_id)->comment_author) .':'. trim(get_comment($parent_id)->comment_content) .'
'. trim($comment->comment_author) .':'. trim($comment->comment_content) .'

点击(此邮件由系统发出,无需回复.)

';$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');

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

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐