您的位置:首页 > 编程语言 > PHP开发

wordpress二次开发教程手记:评论回复邮件通知SMTP版

2013-06-29 22:06 901 查看
通常在wordpress站点开发中,我们为了增强网站用户的粘度会开启评论系统,但是管理员回复了访客的评论,访客却不能立即知道,这样就降低了网站开启评论系统的效果。今天小V就来教大家如何来给空间不支持mai()函数的wordpress站点增加评论回复邮件提醒的方法,为什么说是给空间不支持mai()函数的wordpress站点增加评论回复邮件提醒的方法呢?因为小V这次是使用SMTP的方式来发邮件的,下面上代码:
一、让wordpress支持SMTP方式来发送邮件。
//smtp
add_action('phpmailer_init', 'mail_smtp');
function mail_smtp( $phpmailer ) {
$phpmailer->FromName = '维7维3(v7v3)'; //发信人名称
$phpmailer->Host = 'smtp.exmail.qq.com'; //smtp服务器
$phpmailer->Port = 465;  //端口
$phpmailer->Username = 'admin@v7v3.com';//邮箱帐号   
$phpmailer->Password = '********';//邮箱密码
$phpmailer->From = 'admin@v7v3.com'; //邮箱帐号
$phpmailer->SMTPAuth = true;   
$phpmailer->SMTPSecure = 'ssl'; //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">    <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;">  <span class="STYLE2"><strong >' . trim(get_comment($parent_id)->comment_author) . '</strong>, 你好!</span>
              <p>  <span class="STYLE2">你曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />  --->'
        . trim(get_comment($parent_id)->comment_content) . '<br /><br />
                 ' . trim($comment->comment_author) . ' 给你的回复:<br />  --->'
        . trim($comment->comment_content) . '</span></p>
        <p >   <Strong>你可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复完整内容</a></Strong></p>
              <p><span class="STYLE2">   <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>   更多内容请:<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');

以上代码加入到functions.php里调用即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: