方法二:手动编辑 wp-config.php 文件
如果您不想使用插件,可以添加以下代码到 wp-config.php 文件(位于 WordPress 根目录):
php
复制
下载
define( 'SMTP_USER', 'your@email.com' ); // SMTP 用户名 define( 'SMTP_PASS', 'yourpassword' ); // SMTP 密码 define( 'SMTP_HOST', 'smtp.example.com' ); // SMTP 服务器 define( 'SMTP_FROM', 'your@email.com' ); // 发件人地址 define( 'SMTP_NAME', 'Your Name' ); // 发件人名称 define( 'SMTP_PORT', '587' ); // SMTP 端口 define( 'SMTP_SECURE', 'tls' ); // 加密方式 (ssl 或 tls) define( 'SMTP_AUTH', true ); // 启用 SMTP 认证 define( 'SMTP_DEBUG', 0 ); // 调试模式 (0=关闭, 1=错误信息, 2=全部信息)
然后添加以下代码到主题的 functions.php 文件或创建一个自定义插件:
php
复制
下载
function send_smtp_email( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
add_action( 'phpmailer_init', 'send_smtp_email' );