最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】给wordpress添加评论回复发邮件通知功能

WordPress crifan 10277浏览 0评论

【问题】

之前在这里Comment Mail Notify:适用于Twenty Ten主题的WordPress评论邮件回复代码看到有这样的插件,所以就打算试试,给wordpress添加功能,支持让评论的人,选择,在别人回复其评论时,是否给评论原作者发邮件通知.

【折腾过程】

1.在localhost测试这个功能。

先装上我所需要的retina主题。

在wordpress后台的插件->安装插件->搜索中搜“Comment reply”,第一个找到了

WordPress 留言回复通知插件:Comment Reply Notification

的“Comment Reply Notification”,看着其评价很好,就点击安装了。

2.安装Comment Reply Notification后,就去启用该插件,然后将默认对于“邮件通知被回复评论的作者当评论被回复时“的“禁用”改为“评论者自己选择是否接收邮件(默认选择)”,然后保存设置后。

去先退出登陆,用某个用户名和邮箱测试发了个帖子,然后再用本身管理员账户登陆去给其回复,结果试了几次,原先评论者,都还是无法收到邮件。

3.然后想要回来继续测试Comment Mail Notify,但是原贴中原作者地址已失效,然后网上找到

Comment Mail Notify 免插件实现嵌套回复邮件提醒发送

参考其中的验证是否支持main()函数的部分

必须注意的是: 你的服务器一定要有mail() 功能. 测试方式: 在登入页故意按下’忘记密码’, 收到邮件就有mail() 功能; 没收到邮件的可以下课了. 请先安装好smtp 插件, 确定服务器可发信了再回来吧~

去自己的localhost中测试了下,结果发现自己此处不支持main()函数:

无法发送电子邮件。
可能原因:您的主机禁用了 mail() 函数…

所以,现在还要先去解决发邮件的问题。

弄了半天,结果也还是没解决:

【未解决】给wordpress添加smtp

但是后来去自己的在线的网站crifan.com上面测试,发现在线网站,是支持mail()函数,点击忘记密码,输入邮件后,是可以发送相应的重置密码的邮件的。

只是本地的localhost不支持mail()函数。

4.先去按照:

Comment Mail Notify 免插件实现嵌套回复邮件提醒发送

把代码拷贝过去试试.

但是又注意到了,没有弄好发邮件的支持,这段代码是没用的。所以还是要继续折腾邮件的功能。

5.后来,想到了,既然在线网站支持mail()函数,那就可以直接去在线网站尝试一下,把上述代码贴过去,看看是否有效。

然后就去通过filezilla下载了retina主题中的function.php文件,下载到本地,然后把Comment Mail Notify 免插件实现嵌套回复邮件提醒发送中的第一种,用户可以选择的是否回复的那个代码:

 

/* comment_mail_notify v1.0 by willin kan. (有勾选栏, 由访客决定) */
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; 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, array('type' => 'comment'))) . '">查看回应完整内容</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');

// -- END ----------------------------------------

贴到原先的<?php和?>中间,然后刷新了某个页面,点击回复评论,的确可以在评论框下面看到一个勾选上的文字,但是是乱码,然后才发现,原来此function.php文件时本地编码的,所以去改为UTF-8然后重新上传,刷新一下页面,就可以正常显示汉字“有人回覆时邮件通知我”了:

有人回覆时邮件通知我

6.然后就去测试了一下,发现的确是可以收到回复邮件的。

测试的评论和回复如下:

comment test data

收到的回复邮件通知内容是:

reply mail

7. 目前虽然可以工作,但是还是有几点不爽:

a。字写错了,把回复写成回覆了,所以替换为回复即可。

b。回复邮件时,所用的email,不是我所期望的管理员的email:[email protected]

而是xxx [email protected]

去看了下代码,好像是

$admin_email = get_bloginfo (‘admin_email’); // $admin_email可改为你指定的e-mail.

去获得系统的email的。

然后去查了下get_bloginfo和admin_email,得知:

http://codex.wordpress.org/Function_Reference/get_bloginfo

admin_email‘ – Returns the ‘E-mail address‘ set in Settings > General. This data is retrieved from the ‘admin_email‘ record in the wp_options table.

但是去看了下,我的网站中的 设置->常规->电子邮件地址 中的值,就是我自己之前设置的[email protected],而不是此处所看到的xxx [email protected]所以,还是很奇怪的。

所以就去调试了一下。

测试了半天,把:

$wp_email = ‘no-reply@’ . preg_replace(‘#^www\.#’, ”, strtolower($_SERVER[‘SERVER_NAME’])); // e-mail发出点, no-reply可改为可用的e-mail.

改为固定的:

$wp_email = [email protected];

最后可以显示是此邮件发的了,但是却是代发的:

代发的

去新建了个[email protected]

然后代码再改回来,看看是否还是代发的。

结果还是代发的。

有空再搞定这个代发的事情吧。

8.再参考:

Comment Mail Notify:适用于Twenty Ten主题的WordPress评论邮件回复代码

去装上wp-mail-smtp试试,然后按照提示去设置一下:

wp mail smtp 设置

最后再去测试,结果真的解决了邮件代发问题:

真的解决了邮件代发问题

在此,感谢一下typemylife。

另外,等有空好好研究一下,尽量不用此插件而解决邮件代发问题。

 

 

【总结】

我此处,实际上在线的网站已经支持mail()函数的,所以直接拷贝对应代码,就可以实现对应的评论回复邮件通知的功能了。

最终使用的代码为:

/* comment_mail_notify v1.0 by willin kan. (有勾选栏, 由访客决定) */
function comment_mail_notify($comment_id) {
  $admin_notify = '1'; // admin要不要收回复通知( '1'=要; '0'=不要)
  $admin_email = get_bloginfo ('admin_email'); // $admin_email可改为你指定的e-mail.
  //echo "admin_email is " . $admin_email;
  //echo "server name is " . $_SERVER['SERVER_NAME'];
  //echo "blog name is " . get_option('blogname');
  $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') {
    //echo "strtolower($_SERVER['SERVER_NAME'])";
    $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; 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, array('type' => 'comment'))) . '">查看回应完整内容</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');

// -- END ----------------------------------------

 

只是有点小问题,包括邮件代发问题,通过wp-mail-smtp插件,最终得以解决。

 

【后记】

果然如

bluehost下wordpress改变自动发信人为自己的邮箱地址(sender)

所说,无论如何改From格式,比如我都把:

$from = "From: \"" . get_option(‘blogname’) . "\" <$wp_email>";

改为了固定的:

$from = "From: \"crifan\" <[email protected]>";

这样确保不会由于get_option(‘blogname’) 得到啥生僻的字符,导致From非法,但是即使如此,结果也hostmonster那边,也还是无法识别,也还是会导致邮件代发。

(关于hostmonster只识别合法的from头信息,

参见其官网解释:Hostmonster Web Hosting Help Scripted E-mails appear to come from username@host###.hostmonster.com,此官网解释,是从

HostMonster主机中修改WordPress的发件人地址

中找到的。)

接着去改php.ini。

但是找了半天,在public_html中也找不到php.ini,结果是参考:

HostMonster主机中修改WordPress的发件人地址

Where is my php.ini master file located?

去使用cpanel中的php config:

php config

进入后,点击Save Changes,然后会在public_html下面生成php.ini文件:

生成php ini文件

(如果已经用filezilla通过ftp访问public_html,需要刷新一下,才能看到新生成的php.ini!)

然后去修改:

sendmail_path = /usr/sbin/sendmail -t –i

为:

sendmail_path = /usr/sbin/sendmail -t -i -f'"crifan" <[email protected]>'

此处需要提示一下,关于sendmail的参数问题:

1. 之前这位:

bluehost下wordpress改变自动发信人为自己的邮箱地址(sender)

所说的,-f后面跟的邮件地址,两者之间没空格,其实和这人的解释一样:

WordPress Sender Email incorrect on HostMonster

但是实际上,人家官方:

Hostmonster Web Hosting Help Scripted E-mails appear to come from username@host###.hostmonster.com

的写法是:

sendmail_path = /usr/sbin/sendmail -t -i -f'[email protected]'

即,-f后面的邮件地址,是用单引号括起来的。

相应的,我专门去(通过google搜man sendmail)查了查sendmail的用法:

sendmail

-Ffullname
Set the full name of the sender.
-fname

Sets the name of the ”from” person (i.e., the envelope sender of the mail). This address may also be used in the From: header if that header is missing during initial submission. The envelope sender address is used as the recipient for delivery status notifications and may also appear in a Return-Path: header. -f should only be used by ”trusted” users (normally root, daemon, and network) or if the person you are trying to become is the same as the person you are. Otherwise, an X-Authentication-Warning header will be added to the message.

 

即,本身sendmail的官网解释,也是用单引号括起来的。

而我上面写成:

-f’"crifan" <[email protected]>’

是写出了发件人的全名fullname,即包括了发件人的名字和发件人的邮箱地址。

因此也就解决了:

bluehost下wordpress改变自动发信人为自己的邮箱地址(sender)

所遇到的,只能写邮件地址,不能加发件人名字的问题了。

 

2. 根据其他地方队sendmail的-F和-f参数格式的解释:

Linux and Unix sendmail command

中间可以有空格的,自己懒得再测了,感兴趣的可以自己去试试。

 

最终,终于解决了邮件代发问题:

修改sendmail后 解决了邮件代发问题

 

【邮件代发的问题的总结】

看来的确是hostmonster对于邮件头信息不能正确识别,导致其误以为你所发的邮件的头信息是非法的,不符合其From:部分的规定(虽然实际上的确已经符合其规定了),结果导致邮件代发,变成username@host###.hostmonster.com的地址。

解决问题的逻辑是,由于php的邮件功能,是调用sendmail去发送邮件的,所以可以修改sendmail的参数,以实现设置相应的发件人的信息。

对应的sendmail的参数配置,在php.ini中,所以解决办法就是去找到php.ini,然后修改sendmail参数。

(其中,要知道一点,邮件头信心中的From:部分,可以重写sendmail中-f参数的配置)

而对应的php.ini,一般是在public_html下面。

(如果没有,可以通过cpanel中的php cofig这个工具,进入后点击save changes,以生成新的php.ini到public_html文件夹中)

然后把默认的:

sendmail_path = /usr/sbin/sendmail -t –i

改为:

sendmail_path = /usr/sbin/sendmail -t -i -f’"yourName" <yourEmailAddress>’

比如:

sendmail_path = /usr/sbin/sendmail -t -i -f’"crifan" <[email protected]>’

其中,要确保你的邮件地址,是(在cpanel中就可以看到的)真实存在的一个邮件账号。

(如果没有合适的邮件账号,可以通过cpanel中“电子邮件账户”去创建)

 

【后记2】

又花点时间,给帖子标题添加了链接,最终代码如下:

/*
 comment_mail_notify v1.0 by willin kan. (有勾选栏, 由访客决定)
 updated by crifan li, 20120803:
    add post perma link for post title
    changed some text notification
 */
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 : '';
  $postId = $comment->comment_post_ID;
  $postPermaLink = esc_url(get_permalink($postId));
  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; border-radius:5px;">
      <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《<a href="' . $postPermaLink . '">'. get_the_title($comment->comment_post_ID) . '</a>》的留言:<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, array('type' => 'comment'))) . '"> 查看完整的回复内容</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');

// -- END ----------------------------------------

转载请注明:在路上 » 【记录】给wordpress添加评论回复发邮件通知功能

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (8)

  1. 你好,现在发现邮件通知中的文章标题链接是错误的,不能正常跳转。
    5年前 (2019-10-23)回复
  2. 感谢··学到了,我也加一个
    知性良品9年前 (2014-12-26)回复
  3. 我的主机不支持mail(),但是我用了SMTP可以正常接收到新评论的邮件,找回密码的时候也可以接收到邮件,但就是回复访客的评论时,访客总是收不到我的提醒邮件 用的是comment_mail_notify v1.0 by willin kan的代码,我把no-reply的邮箱地址改为了找回密码邮件的发件人地址,但是还是不行...有解决办法吗?
    Capbone11年前 (2013-08-03)回复
    • 这个真不清楚。。。
      crifan11年前 (2013-08-08)回复
  4. 真有兴致,大半夜的还折腾啊,哈哈~不错,这下子可以省去一个插件了,感谢分享。
    Neverland Zhang12年前 (2012-08-03)回复
97 queries in 0.218 seconds, using 22.21MB memory