在使用wordpress的过程中,常会遇到发送邮件不成功的的问题。以下是我在用的最简洁的方法:

一、使用SMTP方式

在wp-config.php里追加(放在/* That’s all, stop editing! */之前):

/* SMTP 邮件配置 - 开始 */
define('SMTP_HOST', 'smtp.qq.com');
define('SMTP_PORT', 465);
define('SMTP_USER', '4541293@qq.com');      // 你的 QQ 邮箱
define('SMTP_PASS', '123456');      // 到 QQ 邮箱「生成授权码」里获取
define('SMTP_NAME', 'wodepress');            // 发件人昵称
define('SMTP_SECURE', 'ssl');            // 465 填 ssl,587 填 tls
/* SMTP 邮件配置 - 结束 */

在「当前启用主题」或「专用功能插件」里追加:

/**
 * WordPress 6.9+ 兼容 SMTP 发件
 * 仅当常量全部定义后才生效,防止误操作
 */
add_action('phpmailer_init', 'my_smtp_mail_config');
function my_smtp_mail_config(PHPMailer $phpmailer): void {
    // 1. 常量检查
    $required = ['SMTP_HOST', 'SMTP_PORT', 'SMTP_USER', 'SMTP_PASS', 'SMTP_NAME', 'SMTP_SECURE'];
    foreach ($required as $key) {
        if (!defined($key)) {
            // 开发阶段可打开日志:error_log("SMTP 常量 {$key} 未定义");
            return;
        }
    }

    // 2. 基本配置
    $phpmailer->isSMTP();
    $phpmailer->Host       = SMTP_HOST;
    $phpmailer->Port       = SMTP_PORT;
    $phpmailer->SMTPAuth   = true;
    $phpmailer->Username   = SMTP_USER;
    $phpmailer->Password   = SMTP_PASS;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->From       = SMTP_USER;
    $phpmailer->FromName   = SMTP_NAME;

    // 3. 解决 WP 6.9 退信问题:Sender 必须与 From 保持一致
    $phpmailer->Sender = SMTP_USER;
}

使用说明

到QQ邮箱 → 设置 → 账户 → 生成「授权码」,替换SMTP_PASS。

若以后换邮箱,只需改wp-config.php里的6个常量,代码层零改动。

二、插件方式

推荐使用Easy WP SMTP插件

wordpress官网Easy WP SMTP下载地址

https://cn.wordpress.org/plugins/easy-wp-smtp/