WordPress 使用自定义邮件API接口发送评论邮件

发表时间:2019-09-02 15:51 | 分类:建站经验 | 浏览:1,385 次

使用wordpress搭建的博客用户在评论时默认会通过sendmail发送邮件,这种方式有明显的两个缺点。一可能服务器或者虚拟主机不能访问25端口(国内和国外很多主机都是封禁的,为了防止垃圾邮件),另外一个就是会暴露服务器的IP地址,加了cdn也无法隐藏ip。搁置很久的问题,现在还是打算来解决下。这里博主打算用自建的邮件API,而不是搜狐的sendcloud服务。

API

地址:https://api.nbhao.org/sendmail.php

参数:token、subject、message、fromname、email,分别表示认证信息,主题、邮件内容、发信人名字和收信人。

每个人自建的api参数可能会有区别。

代码

把一下代码添加到wordpress博客主题的functions.php文件中。

/**
** WordPress自定义API邮件代发函数
** 参考地址:https://zhang.ge/5045.html
**/
function CustomMailApi($fromname,$to,$subject,$message) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_URL, 'https://api.nbhao.org/sendmail.php');

    $data = array(
        'token' => '123456',
        'fromname' => $fromname,
        'message'=> $message,
        'subject' => $subject,
        'email'=> $to
        );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    if($result === false) {
        echo curl_error($ch);
    }
    curl_close($ch);
    return $result;
}

function zhangnq_send_email($parent_id,$comment){//发送邮件的函数 by Qiqiboy.com
    $admin_email = get_bloginfo ('admin_email');//管理员邮箱
    $parent_comment=get_comment($parent_id);//获取被回复人(或叫父级评论)相关信息
    $author_email=$comment->comment_author_email;//评论人邮箱
    $to = trim($parent_comment->comment_author_email);//被回复人邮箱
    $spam_confirmed = $comment->comment_approved;
    if ($spam_confirmed != 'spam' && $to != $admin_email && $to != $author_email) {
        $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
        $subject = '您在 [' . get_option("blogname") . '] 的留言有了新的回复!';
        $fromname = get_option('blogname');
	$message = '
<div style="background-color:#fff; border:1px solid #666666; color:#111;
-moz-border-radius:8px; -webkit-border-radius:8px; -khtml-border-radius:8px;
border-radius:8px; font-size:12px; width:702px; margin:0 auto; margin-top:10px;
font-family:微软雅黑, Arial;">
<div style="background:#666666; width:100%; height:60px; color:white;
-moz-border-radius:6px 6px 0 0; -webkit-border-radius:6px 6px 0 0;
-khtml-border-radius:6px 6px 0 0; border-radius:6px 6px 0 0; ">
<span style="height:60px; line-height:60px; margin-left:30px; font-size:12px;">
您在<a style="text-decoration:none; color:#00bbff;font-weight:600;"
href="' . get_option('home') . '">' . get_option("blogname") . '
</a>博客上的留言有回复啦!</span></div>
<div style="width:90%; margin:0 auto">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在 [' . get_option("blogname") . '] 的文章
《' . get_the_title($comment->comment_post_ID) . '》 上发表评论:
<p style="background-color: #EEE;border: 1px solid #DDD;
padding: 20px;margin: 15px 0;">' . nl2br(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复如下:
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;
margin: 15px 0;">' . nl2br($comment->comment_content) . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#00bbff"
href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复的完整內容</a></p>
<p>欢迎再次光临 <a style="text-decoration:none; color:#00bbff"
href="' . get_option('home') . '">' . get_option("blogname") . '</a></p>
<p>(此邮件由系统自动发出, 请勿回复.)</p>
</div>
</div>';
        $message = convert_smilies($message);
        if( $to != '' && is_email($to)){
	   CustomMailApi( $fromname, $to, $subject, $message );
        }
    }
}

//下面是修改版的WordPress发邮件代码(支持嵌套评论邮件)
/* 邮件通知 by Qiqiboy */
function comment_CustomMailApi_notify($comment_id) {
    $comment = get_comment($comment_id);//根据id获取这条评论相关数据
    $comment_approved=$comment->comment_approved;
    if ($comment_approved != 1) { return; }
    $content=$comment->comment_content;
    //对评论内容进行匹配
    $match_count=preg_match_all('/<a href="#comment-([0-9]+)?" rel="nofollow">/si',$content,$matchs);
    if($match_count>0){ //如果匹配到了
        foreach($matchs[1] as $parent_id){//对每个子匹配都进行邮件发送操作
        zhangnq_send_email($parent_id,$comment);
        }
    } elseif ($comment->comment_parent!='0'){//以防万一,有人故意删了@回复,还可以通过查找父级评论id来确定邮件发送对象
        $parent_id=$comment->comment_parent;
        zhangnq_send_email($parent_id,$comment);
    } else return;
}
add_action('comment_post', 'comment_CustomMailApi_notify');

测试

正常情况管理员回复邮件后通知类似如下:

20190902153853

如果有多封邮件,说明之前已经添加过评论回复邮件通知代码,可以去自己的主题目录下找下,然后注释掉。实在不行添加如下代码到functions.php,一般都是用的comment_mail_notify这个函数。

remove_action('comment_post', 'comment_mail_notify');

SMTP发送

默认使用smtp发送邮件

//使用smtp发送邮件,代码中使用的是QQ邮箱,你可以参照你使用的邮箱具体设置SMTP   
add_action('phpmailer_init', 'mail_smtp');   
function mail_smtp( $phpmailer ) {   
    $phpmailer->FromName = 'XX博客'; //发件人   
    $phpmailer->Host = 'smtp.qq.com'; //修改为你使用的SMTP服务器   
    $phpmailer->Port = 25; //SMTP端口   
    $phpmailer->Username = 'user@domain.com'; //邮箱账户      
    $phpmailer->Password = 'password'; //邮箱密码   
    $phpmailer->From = 'user@domain.com'; //你的邮箱      
    $phpmailer->SMTPAuth = true;      
    $phpmailer->SMTPSecure = ''; //tls or ssl (port=25留空,465为ssl)   
    $phpmailer->IsSMTP();   
}

本文参考张戈的一篇文章,原文地址:https://zhang.ge/5045.html。

本文标签:

本文链接:https://www.sijitao.net/3101.html

欢迎您在本博客中留下评论,如需转载原创文章请注明出处,谢谢!

现在只有1个回复
Comment (1)
Trackbacks (0)
  1. 今日头条新闻  ( 2019.09.16 00:26 ) : #-9

    文章不错支持一下吧

  1. 还没有Trackbacks
一键脚本 博客历程 留言联系 文章归档 网站地图 谷歌地图
Copyright © 2010-2024 章郎虫博客 All Rights Reserved.