You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用WordPress的wp_mail函数发送邮件失败问题求助

问题排查与解决方案

你的代码存在几个核心问题,导致邮件无法正常发送,以下是逐一修复步骤:

1. 按钮点击事件的逻辑错误

你在按钮的onclick属性中直接嵌入PHP代码<?php my_function(); ?>,这是完全错误的:

  • PHP是服务器端脚本,页面加载时就会执行这段代码,而非用户点击按钮时触发
  • 客户端的onclick仅支持执行JavaScript,无法直接调用PHP函数

修复方案(改用AJAX实现):

修改index.php中的按钮和添加JS逻辑:

<button class="btn btn-primary px-4 btn-lg" id="btn-s">Angebot anfordern</button>

<script>
document.getElementById('btn-s').addEventListener('click', function() {
    fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: 'action=send_custom_email'
    })
    .then(response => response.text())
    .then(data => alert(data))
    .catch(err => console.error('请求失败:', err));
});
</script>

在functions.php中注册AJAX处理函数:

// 允许登录/未登录用户触发
add_action('wp_ajax_send_custom_email', 'my_function');
add_action('wp_ajax_nopriv_send_custom_email', 'my_function');

function my_function() {
    $to = '...@gmail.com';
    $subject = 'Here is the subject';
    $message = 'This is the HTML message body <b>in bold!</b>';
    // 指定HTML邮件的Content-Type
    $headers = array('Content-Type: text/html; charset=UTF-8');

    // 执行发送并检查结果
    $send_result = wp_mail($to, $subject, $message, $headers);
    
    if ($send_result) {
        echo '邮件发送成功';
    } else {
        echo '邮件发送失败';
        // 记录错误日志用于排查
        error_log('WP Mail发送失败,收件人:' . $to);
    }

    wp_die(); // 必须添加,终止AJAX请求
}

2. SMTP配置不完整

你的wp-config.php仅定义了SMTP_PORT和SMTP_SECURE,但my_phpmailer函数中依赖的其他SMTP变量均未定义,会导致PHPMailer初始化失败。

修复方案:

在wp-config.php中补全所有SMTP配置(以Gmail为例,其他邮箱请对应调整):

define('SMTP_HOST', 'smtp.gmail.com');
define('SMTP_AUTH', true);
define('SMTP_USER', '你的gmail邮箱地址');
define('SMTP_PASS', '你的Gmail应用专用密码'); // Gmail需开启两步验证并创建应用密码,不能用登录密码
define('SMTP_PORT', '587');
define('SMTP_SECURE', 'tls');
define('SMTP_FROM', '你的发件邮箱地址');
define('SMTP_NAME', '你的站点名称');

3. PHPMailer初始化函数的健壮性优化

为my_phpmailer添加配置检查,避免因变量未定义导致的静默失败:

add_action('phpmailer_init', 'my_phpmailer');
function my_phpmailer($phpmailer) {
    // 检查SMTP配置是否完整
    $required_configs = ['SMTP_HOST', 'SMTP_AUTH', 'SMTP_USER', 'SMTP_PASS'];
    foreach ($required_configs as $config) {
        if (!defined($config)) {
            error_log('SMTP配置缺失:' . $config);
            return;
        }
    }

    $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;

    // 开发环境可开启调试(上线后关闭)
    // $phpmailer->SMTPDebug = 2;
    // $phpmailer->Debugoutput = 'error_log';
}

4. 开启调试模式排查隐藏错误

在wp-config.php中开启调试,查看邮件发送过程中的具体错误:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

调试日志会保存在wp-content/debug.log中,可查看详细的错误信息。


内容的提问来源于stack exchange,提问作者Ilian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 02:31:16