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

使用PHPMailer通过smtp.mandrill.com发邮件提示成功但未实际发送求助

问题原因及修复方案

存在的问题

  • 变量名不匹配:定义$telefono、$texto两个变量,拼接邮件内容时却使用了不存在的$telephone、$text变量,会导致邮件内容缺失,但不直接触发发送失败异常
  • 发件人地址格式错误:setFrom参数example@example是不合法的邮箱格式,缺少域名后缀,Mandrill会直接拦截此类非法发件人请求
  • SMTP服务器地址错误:Mandrill官方SMTP服务地址为smtp.mandrillapp.com,而非你填写的smtp.mandrill.com,地址错误会导致请求无法到达Mandrill服务端
  • 配置有效性问题:需确保你填写的SMTP用户名是Mandrill后台生成的有效API密钥,且发件人邮箱对应的域名已在Mandrill后台完成SPF、DKIM验证,未验证的域名无法通过Mandrill发信
  • 错误日志未开启:当前SMTPDebug设为0,无法查看服务端返回的具体错误信息,排查问题时可临时改为2打印交互日志

修正后参考代码

<?php

$name = $_POST["first_name"];
$telephone = $_POST["telephone"];
$text = $_POST["text"];

// 统一变量名,避免拼写错误
$body =
    "Name: ".$name."<br>
    Telephone: ".$telephone."<br>
    Message: ".$text;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPmailer/Exception.php';
require 'PHPmailer/PHPMailer.php';
require 'PHPmailer/SMTP.php';

// 开启异常捕获
$mail = new PHPMailer(true);

try {
    // 排查问题时可将SMTPDebug改为2查看详细交互日志,正常运行时改回0
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    // 修正为Mandrill官方SMTP地址
    $mail->Host       = 'smtp.mandrillapp.com';
    $mail->SMTPAuth   = true;
    // 此处填写Mandrill后台生成的有效API密钥作为用户名
    $mail->Username   = '你的Mandrill API密钥';
    // Mandrill SMTP密码可填写任意非空字符串即可
    $mail->Password   = '任意非空值';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
    $mail->Port       = 465;
    // 修正为合法的已验证发件人邮箱
    $mail->setFrom('合法的已验证发件人邮箱', '发件人名称');
    // 修正为合法的收件人邮箱
    $mail->addAddress('合法的收件人邮箱');

    $mail->isHTML(true);
    $mail->Subject = 'Contact';
    $mail->Body    = 'New message from <b>Contact</b><br><br>'.$body;
    $mail->CharSet = 'UTF-8';
    $mail->send();
    echo '<script>
    alert("The data was sent successfully.");
    window.history.go(-1);
    </script>';

} catch (Exception $e) {
    // 排查问题时可把$e->getMessage()输出到alert查看具体错误原因
    echo '<script>
    alert("An error occurred while sending the data.");
    window.history.go(-1);
    </script>';
}
?>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 10:39:02