几行搞定 WordPress 邮件发送

要通过代码方式在 WordPress 中配置 SMTP,而无需使用插件,您可以通过修改 wp-config.php 文件并添加一些代码来实现。以下是详细步骤:

1. 打开 wp-config.php 文件

在您的 WordPress 根目录下找到 wp-config.php 文件,使用文本编辑器打开。

2. 添加 SMTP 配置代码

wp-config.php 文件中,添加以下代码来配置 SMTP 设置:

// SMTP Configuration
define('WP_MAIL_SMTP_HOST', 'smtp.example.com');  // SMTP server address
define('WP_MAIL_SMTP_PORT', 587);                 // SMTP port number
define('WP_MAIL_SMTP_USERNAME', '[email protected]');  // SMTP username
define('WP_MAIL_SMTP_PASSWORD', 'your-email-password');     // SMTP password
define('WP_MAIL_SMTP_FROM', '[email protected]');      // Sender email
define('WP_MAIL_SMTP_FROM_NAME', 'Your Name');              // Sender name
define('WP_MAIL_SMTP_SECURE', 'tls');                       // Encryption method, use 'tls' or 'ssl'
define('WP_MAIL_SMTP_AUTH', true);                          // Whether to enable authentication

3. 使用 phpmailer 配置 SMTP

在主题的 functions.php 文件中添加以下代码来启用 SMTP 配置:

// SMTP Configuration
add_action( 'phpmailer_init', 'custom_phpmailer_init' );

function custom_phpmailer_init( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host = WP_MAIL_SMTP_HOST;
    $phpmailer->SMTPAuth = WP_MAIL_SMTP_AUTH;
    $phpmailer->Port = WP_MAIL_SMTP_PORT;
    $phpmailer->Username = WP_MAIL_SMTP_USERNAME;
    $phpmailer->Password = WP_MAIL_SMTP_PASSWORD;
    $phpmailer->From = WP_MAIL_SMTP_FROM;
    $phpmailer->FromName = WP_MAIL_SMTP_FROM_NAME;
    $phpmailer->SMTPSecure = WP_MAIL_SMTP_SECURE;
}

4. 保存文件并测试

  • 保存 wp-config.phpfunctions.php 文件。

这将配置 WordPress 使用指定的 SMTP 服务器发送电子邮件,而不需要使用插件。


评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注