Godaddy的WIN主机实现Wordpress评论回复邮件通知(非插件)

2010/09/10 | 分类: Wordpress | 编辑: 乐龙 | | 发表评论

有网友问Yuelongr: 你博客的评论回复自动邮件通知的功能是怎么实现的啊. 当时忙没马上回答,现在抽点时间写下来吧,分享让更多的新手知道吧,其实我也都是从网上搜索来的,根据实际情况整合和修改下而已.其中的评论回复邮件通知功能是通用的,其他主机也可以使用,而且是非插件形式.

本文[Godaddy的WIN主机实现Wordpress评论回复邮件通知(非插件/通用)]地址:
http://www.yuelongr.com/archives/assorted/wordpress/2010/09/10/2143.html

打开你Wordpress主题的functions.php文件,在<?php和?>之间添加以下代码:

//comment_mail_notify(所有的回复都会发邮件通知)
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
//此处是发件人的邮箱地址,默认是自动以你的域名为后缀,比如我的是"@yuelongr.com",也可自行修改为其他的.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在['.get_option("blogname").']的留言有了回复';
//此处是邮件的标题,如果是godaddy主机,标题太长邮件可能发送失败,比如"您在[乐龙博客]的留言有了回复"可正常发送
//以下为邮件内容,也可自行修改为其他的.
$message = '
<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;">
<p>'.trim(get_comment($parent_id)->comment_author).', 您好!</p>
<p>这是您在《'.get_the_title($comment->comment_post_ID).'》中的留言:<br />'
.trim(get_comment($parent_id)->comment_content).'</p>
<p>以下是 '.trim($comment->comment_author).' 给您的回复:<br />'
.trim($comment->comment_content).'<br /></p>
<p>您可以<a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">点击这里查看回复的完整内容.</a></p>
<p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(注:此邮件由系统自动发出,请勿回复!)</p>
</div>';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
}
}//Modify by Yuelongr.com
add_action('comment_post', 'comment_mail_notify');

主要原理应该是用wp_mail函数代替了mail()函数,因为Godaddy的WIN主机禁用了mail()函数.

如果是Godaddy的WIN主机那么还需要继续接着加入以下代码才能真正实现该功能,其他主机就不用了.

//配置开启GODADDY空间的SMTP发送邮件功能,如果不是GODADDY的WIN空间,则不需要这段.
function Mailer(&$mailer){
$mailer->CharSet = "UTF-8";
$mailer->IsSMTP();
$mailer->SMTPAuth   = false;
$mailer->SMTPSecure = "";//"ssl"
$mailer->Host       = "relay-hosting.secureserver.net";
$mailer->Port       = 25;
$mailer->Username   = "";
$mailer->Password   = "";
}//Modify by Yuelongr.com
add_action('phpmailer_init','Mailer');

Godaddy的WIN主机是支持SMTP功能的,但是,他只支持他自己的主机,即"relay-hosting.secureserver.net",其他的邮箱我试了都不行,如果可以的有请告诉我一声,谢谢.

如果以上两段代码都添加好了,那么恭喜你,可以像乐龙博客一样,具体评论回复自动邮件通知的功能了,具体效果可以到我的留言本里留言测试查看~

备注:
1."relay-hosting.secureserver.net"每天限制只能发送250封(一般够用了吧?);
2.比较郁闷的是:不支持发送到QQ邮件和其他个别邮件,超级郁闷这一点,据说是被Godaddy屏蔽掉了;
3.如果是带SSL功能的,可能上面的$mailer->SMTPSecure = "";//"ssl"需要改动,我没试过.

-------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------

最后,附上另外两种回复形式的代码:"让访客自己选择是否邮件通知"和"让博客管理员决定什么情况下发邮件",请自行测试.

一. 如果想让访客自己选择是否邮件通知,那么就在functions.php文件中的<?php和?>之间添加以下函数,该函数将会在评论框底部生成要不要收回复通知的选项:

function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回覆通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
$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 ? get_comment($parent_id)->comment_mail_notify : '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'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
$message = '
<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;">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 給您的回應:<br />'
. trim($comment->comment_content) . '<br /></p>
<p>您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</a></p>
<p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此郵件由系統自動發出, 請勿回覆.)</p>
</div>';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
/* 自動加勾選欄 */
function add_checkbox() {
echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回覆時郵件通知我</label>';
}
add_action('comment_form', 'add_checkbox');

二. 如果想让博客管理员决定什么情况下发邮件,就在functions.php文件中的<?php和?>之间添加以下函数:

/* comment_mail_notify v1.0 by willin kan. (無勾選欄) */
function comment_mail_notify($comment_id) {
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
/* 上面的判斷式,決定發出郵件的必要條件:
($parent_id != '') && ($spam_confirmed != 'spam'): 回覆的, 而且不是 spam 才可發, 必需!!
($to != $admin_email) : 不發給 admin.
($comment_author_email == $admin_email) : 只有 admin 的回覆才可發.
可視個人需求修改以上條件.
*/
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
$subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
$message = '
<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;">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 給您的回應:<br />'
. trim($comment->comment_content) . '<br /></p>
<p>您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</a></p>
<p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此郵件由系統自動發出, 請勿回覆.)</p>
</div>';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');

本文的comment_mail_notify代码来自"willin",感谢分享.

已经有10个评论了

  1. julor-学习啦
    2010/09/11 17:23:53
    1 楼 回复

    不错啊,我也刚用这些代码搞好的,装了几个插件不行

  2. jluy
    2010/09/12 11:14:48
    2 楼 回复

    多谢LZ,这么快就好了。干净试试

  3. jluy
    2010/09/12 13:26:43
    3 楼 回复

    LZ按照你的设置,我的站也可以邮件通知回复了。非常感谢!

  4. Proe
    2010/10/16 11:55:32
    4 楼 回复

    送个IP,再给LZ增加点收入。呵呵

    • proe
      2010/10/27 17:20:53

      唉,换了主机有不行了,主机准备换到国内。

  5. 无涯博客
    2010/11/01 20:30:32
    5 楼 回复

    晕了,我的主机商这么回复:
    您好,服务器不支持PHP函数sendmail。此功能一旦开设可能会被一些用户利用服务器进行群发邮件,导致服务器IP被诸多地方屏蔽。您可以选择通过 SOCKET 连接 SMTP 服务器发送(支持 ESMTP 验证)进行发送邮件,一般网站都是选择此模式。

  6. willin
    2010/11/12 09:02:28
    6 楼 回复
  7. [...] 装了一个插件即可。 回复: 我已经整理分享出来了,比较长,具体请看: Godaddy的WIN主机实现Wordpress评论回复邮件通知(非插件) 回复: configure-smtp插件加QQ邮箱应该可以解决此问题 回复: [...]

  8. [...] 回复: 看看这篇吧: Godaddy的WIN主机实现Wordpress评论回复邮件通知(非插件) 回复: 这个第一需要主机支持~,第二我用的是插件:Comment Reply Notification [...]

  9. [...]   发你个好人卡 回复: 还可以再看看我刚整理的这篇文章: Godaddy的WIN主机实现Wordpress评论回复邮件通知(非插件) 回复: 你的主机支持邮件发送吗 回复: 饿 我不懂 学习下 回复: [...]

发表评论

----------------------------------------

您的昵称 *

您的邮箱 *

您的网站

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif 
返回首页 | 关于我们 | 联系我们 | 广告合作 | 网站地图 | 标签页 | 友情链接 | 版权声明
Top