首页 > 技术 > 正文

#教程# WordPress 无插件邮件发送及规避SMTP泄漏网站主机真实IP

云主机一直以来都没有开启邮件回复功能,主要是怕泄漏网站主机真实IP,因为现在腾讯云主机在被攻击宕机后会自动切换到备用服务器,虽然套了CDN但是发送邮件就会暴露服务器IP导致直接绕过CDN被攻击。

云主机一直以来都没有开启邮件回复功能,主要是怕泄漏网站主机真实 IP,因为现在腾讯云主机在被攻击宕机后会自动切换到备用服务器,虽然套了 CDN 但是发送邮件就会暴露服务器 IP 导致直接绕过 CDN 被攻击。

如何泄漏 IP 的

当我们的网站通过 SMTP 代发或者 php 调用 SendMail 来发邮件通知评论者时,我们发送过去的邮件原文中将带有我们主机的真实 IP!!如果被人恶意利用,我们的 CDN 防护都将变得毫无意义。


解决方案

自建 API: 用第三方 API 代发邮件即可,就算暴露 IP 也是暴露了邮件服务器的 IP,这个复杂性比较高,不适合大众;

调用第三方企业邮局 API: 这个也设置复杂,我们就不在这里详细说明了;

使用支持隐藏 IP 的第三方企业邮局: 这里是我们说的重点,目前云线路测试成功的有 Yandex、阿里企业邮局都可以隐藏发件服务器的 IP,而且都是免费的,经过一番测试我个人觉得还是 GOOGLE 的企业邮局好用(日前已无法免费申请了);

下面我把云线路的免插件发邮件的配置列出来

使用前,请确定你的主机是否支持 mail() 函数。在你的主题/functions.php 里添加:

//使用 smtp 发送邮件(请根据自己使用的邮箱设置 SMTP)

add_action('phpmailer_init','mail_smtp');

function mail_smtp( $phpmailer ) {

$phpmailer->FromName = '云线路'; //发件人名称

$phpmailer->Host = 'smtp.gmail.com'; //修改为你使用的邮箱 SMTP 服务器

$phpmailer->Port = 465; //SMTP 端口

$phpmailer->Username = 'yunloc.com.no-reply@xxx.com'; //邮箱账户

$phpmailer->Password = 'XXXXXX'; //邮箱密码

$phpmailer->From = 'yunloc.com.no-reply@xxx.com'; //邮箱账户

$phpmailer->SMTPAuth = true;

$phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25 时->留空,465 时->ssl)

$phpmailer->IsSMTP();

}

//评论回复邮件

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 = '[email protected]' . 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 "border-right:#666666 1px solid;border-radius:8px;color:#111;font-size:12px;width:95%;border-bottom:#666666 1px solid;font-family:微软雅黑,arial;margin:10px auto 0px;border-top:#666666 1px solid;border-left:#666666 1px solid"><div class="adM">

</div><div "width:100%;background:#666666;min-height:60px;color:white;border-radius:6px 6px 0 0"><span "line-height:60px;min-height:60px;margin-left:30px;font-size:12px">您在<a "color:#00bbff;font-weight:600;text-decoration:none" href="' . get_option('home') . '" target="_blank" rel="noopener noreferrer">' . get_option('blogname') . '</a> 上的留言有回复啦!</span> </div>

<div "margin:0px auto;width:90%">

<p><span "font-weight:bold;">' . trim(get_comment($parent_id)->comment_author) . '</span>,您好!</p>

<p>您于' . trim(get_comment($parent_id)->comment_date) . ' 在文章《' . get_the_title($comment->comment_post_ID) . '》上发表留言: </p>

<p "border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">' . nl2br(get_comment($parent_id)->comment_content) . '</p>

<p><span "font-weight:bold;">' . trim($comment->comment_author) . '</span> 于' . trim($comment->comment_date) . ' 给您的回复如下: </p>

<p "border-bottom:#ddd 1px solid;border-left:#ddd 1px solid;padding-bottom:20px;background-color:#eee;margin:15px 0px;padding-left:20px;padding-right:20px;border-top:#ddd 1px solid;border-right:#ddd 1px solid;padding-top:20px">' . nl2br($comment->comment_content) . '</p>

<p>您可以点击 <a "color:#00bbff;text-decoration:none" href="' . htmlspecialchars(get_comment_link($parent_id)) . '" target="_blank" rel="noopener noreferrer">查看回复的完整内容</a></p>

<p>感谢你对 <a "color:#00bbff;text-decoration:none" href="' . get_option('home') . '" target="_blank" rel="noopener noreferrer">' . get_option('blogname') . '</a> 的关注,如您有任何疑问,请联系 [email protected]</p><p "color:#A8979A;">(此邮件由系统自动发出,请勿回复.)</p></div></div>';

$from = "From: "" . get_option('blogname') . "" <$wp_email>";

$headers = "$fromnContent-Type: text/html; charset=" . get_option('blog_charset') . "n";

wp_mail( $to,$subject,$message,$headers );

//echo 'mail to ',$to,'<br/> ',$message; // for testing

}

}

add_action('comment_post','comment_mail_notify');

邮件的具体样式和内容可自行修改,支持 HTML。

网友评论

验证码 换一张
取 消
暂无评论...
为您推荐
  • 相关阅读
  • 业界资讯
  • 手机通讯
  • 电脑办公
  • 新奇数码
  • 软件游戏
  • 科学探索