WordPress纯代码免插件配置SMTP邮件功能教程

WordPress 使用过多的插件导致wordpress变慢及安全性降低,能用代码实现的坚决不使用插件。WordPress纯代码免插件配置SMTP邮件功能教程来了!

WordPress纯代码实现SMTP邮件发送功能

其实这个标题我写的有很大夸大成分,因为 WordPress 本身是有集成 phpmailer 邮件的,我只是把这个调用了一下,没有什么很高的技术含量,废话不多说,直放代码。

SMTP邮件功能在WordPress中是很常用的功能, 例如:网站注册登录、评论回复等风,虽然WordPress自带了mail函数,但是使用自带mail函数发送邮件很容易失败或者被拒收。

而且目前很多虚拟主机基本都是禁用了mail函数的,所以我们只能使用SMTP服务发送邮件,网上关于STMP的WordPress插件有很多,今天本站分享一种不需要插件纯代码实现STMP邮件发送的功能。

首先将下面代码修改后复制到functions.php文件,再测试发送邮件功能。

<span class="com">//WordPress免插件&lt;a href="http://www.laoking.net" target="_blank" rel="noopener noreferrer"&gt;配置SMTP邮件&lt;/a&gt;</span>
<span class="kwd">function</span><span class="pln"> mail_smtp</span><span class="pun">(</span><span class="pln"> $phpmailer </span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">FromName</span> <span class="pun">=</span> <span class="str">'『老K博客』'</span><span class="pun">;</span> <span class="com">//发件人名称</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">Host</span> <span class="pun">=</span> <span class="str">'smtp.qq.com'</span><span class="pun">;</span> <span class="com">//修改为你使用的邮箱SMTP服务器</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">Port</span> <span class="pun">=</span> <span class="lit">465</span><span class="pun">;</span> <span class="com">//SMTP端口</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">Username</span> <span class="pun">=</span> <span class="str">'*****@qq.com'</span><span class="pun">;</span> <span class="com">//邮箱账户</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">Password</span> <span class="pun">=</span> <span class="str">'*****'</span><span class="pun">;</span> <span class="com">//邮箱授权码(此处填写QQ邮箱生成的授权码)</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">From</span> <span class="pun">=</span> <span class="str">'*****@qq.com'</span><span class="pun">;</span> <span class="com">//邮箱账户</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">SMTPAuth</span> <span class="pun">=</span> <span class="kwd">true</span><span class="pun">;</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">SMTPSecure</span> <span class="pun">=</span> <span class="str">'ssl'</span><span class="pun">;</span> <span class="com">//tls or ssl (port=25时-&gt;留空,465时-&gt;ssl)</span><span class="pln">
$phpmailer</span><span class="pun">-&gt;</span><span class="typ">IsSMTP</span><span class="pun">();</span>
<span class="pun">}</span><span class="pln">
add_action</span><span class="pun">(</span><span class="str">'phpmailer_init'</span><span class="pun">,</span> <span class="str">'mail_smtp'</span><span class="pun">);</span>

这里注意一些就是 SMTP 地址的填写,下面是常用邮箱的 POP3 与 SMTP 服务器如下:

<span class="pun">阿里企业云邮箱</span><span class="pln">POP3</span><span class="pun">/</span><span class="pln">SMTP </span><span class="pun">协议收发邮件服务器地址分别如下:</span>
<span class="pun">收件服务器地址:</span><span class="pln">
POP </span><span class="pun">服务器地址:</span><span class="pln">pop3</span><span class="pun">.</span><span class="pln">mxhichina</span><span class="pun">.</span><span class="pln">com </span><span class="pun">端口</span><span class="lit">110</span><span class="pun">,</span><span class="pln">SSL </span><span class="pun">加密端口</span><span class="lit">995</span>
<span class="pun">发件服务器地址:</span><span class="pln">
SMTP </span><span class="pun">服务器地址:</span><span class="pln">smtp</span><span class="pun">.</span><span class="pln">mxhichina</span><span class="pun">.</span><span class="pln">com </span><span class="pun">端口</span><span class="lit">25</span><span class="pun">,</span><span class="pln"> SSL </span><span class="pun">加密端口</span><span class="lit">465</span><span class="pln">
  
</span><span class="pun">腾讯企业邮箱</span><span class="pln"> POP3</span><span class="pun">/</span><span class="pln">SMTP </span><span class="pun">协议收发邮件服务器地址分别如下:</span>
<span class="pun">接收邮件服务器:</span><span class="pln">
POP </span><span class="pun">服务器地址:</span><span class="pln">pop</span><span class="pun">.</span><span class="pln">exmail</span><span class="pun">.</span><span class="pln">qq</span><span class="pun">.</span><span class="pln">com </span><span class="pun">(端口</span> <span class="lit">110</span><span class="pun">),使用</span><span class="pln">SSL</span><span class="pun">,端口号</span><span class="lit">995</span> 
<span class="pun">发送邮件服务器:</span><span class="pln">
SMTP </span><span class="pun">服务器地址:</span><span class="pln">smtp</span><span class="pun">.</span><span class="pln">exmail</span><span class="pun">.</span><span class="pln">qq</span><span class="pun">.</span><span class="pln">com </span><span class="pun">(端口</span> <span class="lit">25</span><span class="pun">),使用</span><span class="pln">SSL</span><span class="pun">,端口号</span><span class="lit">465</span>

这里博主用的是阿里云企业邮箱,所以 SMTP 服务器地址为 smtp.mxhichina.com 端口:465 。如果是腾讯企业邮箱的话,就填上面的 SMTP 邮件服务器地址,其他的邮件服务器的话,大家可以在网上查一下,配置非常简单!

上面的代码已经是非常的简单粗暴了,但是插件有些时候更深入人心,尤其是对一些懒人站长来说简直是不要太简单。

这里推荐的是一款叫做WP SMTP的WordPress插件,该插件数年未更新但是功能依旧强劲,也体验过其他SMTP插件都是花里胡哨还占用较大的资源,WP SMTP这匹老马老夜还是非常推荐的。
WP SMTP官方下载地址:https://wordpress.org/plugins/wp-smtp/

WordPress教程

WordPress加速优化教程提升网站速度

2021-4-21 13:14:42

WordPress教程

WordPress给个人博客增加总访问量教程

2021-4-21 13:20:14

无法充值请加客服QQ3635339338人工充值
24小时自助充值教程QQ3635339338人工充值
版权声明 本站所发布的一切与破解相关的文章仅限用于学习和研究目的,请在下载后的24个小时之内,从您的电脑中彻底删除。所有资源均来自于网络,不得用于商业或者非法用途,否则一切后果请用户自负。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索