免插件为wordpress配置SMTP服务

  • 时间:2020-05-16 19:39:54
  • 分类:网络文摘
  • 阅读:84 次

由于很多 wordpress 主机禁用了 mail 函数,所以不支持邮件发送,造成用户注册或找回密码时无法收到验证邮件。此问题可通过第三方邮件服务器 SMTP 方式代替 wordpress 默认 mail() 函数来解决。网上介绍的方法大多是通过插件配置 wordpress 的 SMTP ,本文介绍的方法是通过代码来实现。具体操作如下:

将如下代码添加到当前 wordpress主题函数模板 functions.php 文件中,并修改相关信息后保存。

  1. // 配置邮件STMP
  2. add_action('phpmailer_init', 'mail_smtp');
  3. function mail_smtp( $phpmailer ) {
  4.     $phpmailer->FromName = '骤雨打新荷'; // 发件人昵称
  5.     $phpmailer->Host = 'smtp.qq.com'; // 邮箱SMTP服务器
  6.     $phpmailer->Port = 465; // SMTP端口,不需要改
  7.     $phpmailer->Username = '[email protected]'; // 邮箱账户
  8.     $phpmailer->Password = '112211221122'; // 此处填写邮箱生成的授权码,不是邮箱登录密码
  9.     $phpmailer->From = '[email protected]'; // 邮箱账户同上
  10.     $phpmailer->SMTPAuth = true;
  11.     $phpmailer->SMTPSecure = 'ssl'; // 端口25时 留空,465时 ssl,不需要改
  12.     $phpmailer->IsSMTP();
  13. }

说明:发件邮箱不是QQ邮箱或126邮箱的,不需要授权码,请在代码第8行 $phpmailer->Password 配置中填写邮箱登录密码。

下面以QQ邮箱为例,简单介绍一下开启IMAP/SMTP服务和获得第三方授权码的方法。

一、开启IMAP/SMTP服务。

登录你的QQ邮箱,依次点击,设置 → 账户,找到“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”设置选项,开启邮箱“IMAP/SMTP服务”,开启时需要手机发送短信验证。

二、获取第三方授权码

点击下面的“生成授权码 ”,按要求发送短信:“配置邮件客户端”到指定的号码,之后点击“我已发送”,会自动生一个授权码,请记好这个授权码,因为只显示一次,没记住只能再次发送短信了,将这个授权码填写到上面的代码配置信息中即可。

注:下图是QQ邮箱和网易邮箱的IMAP/SMTP信息,貌似目前所有邮箱端口都可以设置为 465 并支持 ssl 加密。

QQ邮箱和网易邮箱的SMTP信息

如果用户注册时点击邮件中的验证链接失效请参考:解决wordpress密码设置链接失效的问题

推荐阅读:
How to Protect Your WordPress Site From Hackers  Top 10 Relationship Blogs With the Best Pieces of Advice in 2020  How to Construct Binary Search Tree from Preorder Traversal in P  Dynamic Programming Algorithm to Compute the Block Sum in a Matr  Smallest Multiple Algorithm using Bruteforce or GCD/LCM  How many different ways can £2 be made using any number of coins  Compute Factorial Digit Sum: Find the sum of the digits in the n  Compute the Maximum Integer Right Triangles Solutions  Power Digit Sum: What is the sum of the digits of the number 2^1  Digit factorials: Find the Sum of All the Curious Numbers 
评论列表
添加评论