9

WordPress下支持评论回复邮件提醒功能(与及不能发送邮件的解决办法)

Posted by 撒得一地 on 2015年9月21日 in wordpress笔记

在一些WP博客评论时,收到评论回复后会有邮件通知,感觉确实比较人性化。我检查了下自己的博客,发现我的博客是不支持邮件回复功能的。于是,在网上找了一些方法,方法大同小异,要么插件法,要么直接添加加代码。本着能不用插件就不用插件的原则,我试了下代码法,发现一直没有效果。

原来WP下发送邮件,默认是使用mail()函数的,如果对php比较了解的人肯定知道用mail()发送电子邮件比较麻烦,还要系统有安装sendmail组件。其实wp下还支持用smtp方法发送电子邮件,就是用开源的phpmailer类进行电子邮件的发送。用SMTP发送电子邮件,必须要注意的是你的php要支持fsockopen函数。我查了下我php的配置文件,我的系统默认是不支持的。完整的做法如下:

第一步:找到你的php安装目录下php.ini文件,在文件中查找“allow_url_fopen = On|Off”将其值设为On。 如果本身就为On那就不用修改,如果有修改,修改后重启你的web服务器。

第二步:修改 /wp-include/class-smtp.php 大概在202-208 行的位置的代码(通过查找如下):

$socket_context = stream_context_create($options);
$this->smtp_conn = @stream_socket_client(
$host . ":" . $port,
$errno,
$errstr,
$timeout,
STREAM_CLIENT_CONNECT,
$socket_context
);

修改为如下代码:
$this->smtp_conn = @fsockopen($host,$port,$errno,$errstr,$timeout);

第三步:发送邮件时一些参数的设置与及在你回复其它人时发送邮件进行提醒,在你模板目录下的functions.php文件下添加如下代码:

1、让wordpress支持SMTP方式来发送邮件:,配置发送邮件的邮箱,邮箱密码及端口号等
//smtp
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '技术拉近你我'; //发信人名称
$phpmailer->Host = 'smtp.163.com'; //smtp服务器
$phpmailer->Port = 25;  //端口

$phpmailer->Username = 'xxx@163.com';//你的邮箱邮箱帐号  
$phpmailer->Password = '********';//邮箱密码
$phpmailer->From = 'xxx@163.com'; //别人回复时显示的邮箱帐号
$phpmailer->SMTPAuth = true;  
$phpmailer->SMTPSecure = ''; //tls or ssl (port=25留空,465为ssl)
$phpmailer->IsSMTP();
}
二、让wordpress评论后回复自动发送回复邮件。
//评论回复邮件
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 = 'webmaster@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '你在 [' . get_option("blogname") . '] 的留言有了回应';
    $message = '
    <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-style: solid; border-width: 1;" bordercolor="#85C1DF" width="700" height="251" id="AutoNumber1" align="center">
          <tr>
            <td width="520" height="28" bgcolor="#F5F9FB"><font color="#1A65B6" style="font-size:14px">&nbsp;&nbsp;&nbsp;&nbsp;<b>留言回复通知 | <a href="http://www.v7v3.com" targe="blank">v7v3(维7维3)</a></b></font></td>
          </tr>
          <tr>
            <td width="800" height="210" bgcolor="#FFffff" valign="top" style=" padding:8px;">&nbsp;&nbsp;<span class="STYLE2"><strong >' . trim(get_comment($parent_id)->comment_author) . '</strong>, 你好!</span>
              <p>&nbsp;&nbsp;<span class="STYLE2">你曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />&nbsp;&nbsp;--->'
        . trim(get_comment($parent_id)->comment_content) . '<br /><br />
              &nbsp;&nbsp; ' . trim($comment->comment_author) . ' 给你的回复:<br />&nbsp;&nbsp;--->'
        . trim($comment->comment_content) . '</span></p>
        <p > &nbsp;&nbsp;<Strong>你可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复完整内容</a></Strong></p>
              <p><span class="STYLE2"> &nbsp;&nbsp;<strong>感谢你对 <a href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a> 的关注,欢迎<a href="http://list.qq.com/cgi-bin/qf_invite?id=3077e5391b4997f56a6854122b0c351be607d83c3ebf649b" target="_blank">订阅本站</a></strong>!</p>
            <p>&nbsp;&nbsp; 更多内容请:<a href="http://www.v7v3.com/group/" target="_blank">本站Q群</a> | <a href="http://wpa.qq.com/msgrd?v=3&uin=1176882511&site=v7v3&menu=yes">联系站长</a> | <a href="mailto:v7v7@qq.com">投稿</a> | <a href="http://www.v7v3.com/tougao/">投稿须知</a> | <a href="http://www.v7v3.com/about/">关于我们</a> | <a href="http://www.v7v3.com/contact/">联系我们</a></p></td>
          </tr>
          <tr>
            <td width="800" height="16" bgcolor="#85C1DF" bordercolor="#008000"><div align="center"><font color="#fff"><a href="http://www.v7v3.com">维7维3(www.v7v3.com)</a></font></div></td>
          </tr>
  </table>';
    $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 );
  }
}

add_action('comment_post', 'comment_mail_notify');

备注:我自己用的环境是Linux+Apache

标签:, ,

上一篇:

下一篇:

相关推荐

9 Comments

发表评论

电子邮件地址不会被公开。 必填项已用*标注

9 + 8 = ?

网站地图|XML地图

Copyright © 2015-2024 技术拉近你我! All rights reserved.
闽ICP备15015576号-1 版权所有©psz.