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

咨询:PHPMailer中发件人与收件人邮箱地址相同的原因

问题:PHPMailer发送邮件时发件人与收件人显示相同的原因及解决办法

我在使用PHPMailer开发邮件发送功能时遇到问题:发件人与收件人邮箱地址显示相同。我已在代码中通过setFrom($email, $name)设置发件人(其中$email是表单输入框提交的邮箱地址),并用addAddress指定了固定的收件邮箱,请问为何会出现这种情况?

相关代码如下:

PHP代码

<?php
require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';
use PHPMailer\PHPMailer\PHPMailer;

if (isset($_POST['send'])) {
    $name = htmlentities($_POST['name']);
    $email = htmlentities($_POST['email']);
    $subject = htmlentities($_POST['subject']);
    $message = htmlentities($_POST['message']);

    $mail = new PHPMailer(true);
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'YOUR_EMAIL';
    $mail->Password = 'YOUR_PASSWORD';
    $mail->Port = 465;
    $mail->SMTPSecure = 'ssl';
    $mail->isHTML(true);
    $mail->setFrom($email, $name);
    $mail->addAddress('YOUR_RECEIVE_EMAIL');
    $mail->Subject = ($subject);
    $mail->Body = $message ;
    $mail->send();

    header("Location: ./response.html");
}

HTML代码

<form class="center_absolute display_grid" action="email.php" method="post">
    <input class="w_25_rem margin_bottom_1_rem" type="text" name="name" placeholder="Name" autocomplete="off">
    <input class="w_25_rem margin_bottom_1_rem" type="email" name="email" placeholder="Email" autocomplete="off">
    <input class="w_25_rem margin_bottom_1_rem" type="text" name="subject" placeholder="Subject" autocomplete="off">
    <textarea name="message" class="w_25_rem margin_bottom_1_rem" placeholder="Message..."></textarea>
    <div class="primary-checkbox-container margin_bottom_2_rem">
      <input class="primary-checkbox" type="checkbox" required>
      <div class="primary-checkbox-description color_black font_size_small">I agree to the <a class="font_size_small color_primary" href="https://rizalcss.com/privacy-policy" target="_blank">Privacy Policy</a>.</div>
    </div>
    <button class="primary-button background_color_primary font_size_medium color_white border_radius_secondary" type="submit" name="send">
      Send <i class="fa-solid fa-paper-plane primary-button-icon color_white"></i>
    </button>
  </form>

核心原因:SMTP服务器的发件人限制(以Gmail为例)

你使用的是Gmail的SMTP服务器,而Gmail的SMTP服务不允许随意将第三方邮箱设为发件人——因为你是通过YOUR_EMAIL和YOUR_PASSWORD完成SMTP认证的,Gmail会强制将邮件的实际发件人替换为你用于认证的邮箱地址。如果YOUR_RECEIVE_EMAIL刚好和YOUR_EMAIL是同一个邮箱,就会出现发件人与收件人显示相同的情况。

另外,即使收件邮箱不是认证邮箱,邮件客户端也可能因为Gmail的DKIM/SPF验证规则,优先显示认证邮箱作为发件人,而非你通过setFrom设置的第三方邮箱。

解决办法

  1. 设置回复地址(推荐方案)
    不要直接用用户提交的邮箱作为发件人,而是将其设为回复地址,发件人保持为你的SMTP认证邮箱。这样既符合SMTP服务器规则,用户回复邮件时也会自动发到提交的邮箱:

    // 发件人设为你的SMTP认证邮箱,可自定义显示名称
    $mail->setFrom('YOUR_EMAIL', '你的网站名称');
    // 设置回复地址为用户提交的邮箱
    $mail->addReplyTo($email, $name);
    
  2. 更换支持自定义发件人的SMTP服务
    如果必须显示用户提交的邮箱为发件人,可以选择SendGrid、Mailgun这类专业邮件服务,它们允许你在验证域名后自定义发件人地址(需配置SPF/DKIM记录确保邮件送达率)。

  3. 检查收件邮箱与认证邮箱是否重复
    确认YOUR_RECEIVE_EMAIL和YOUR_EMAIL不是同一个邮箱,避免出现发件人收件人地址重合的显示问题。

额外注意事项

  • Gmail开启两步验证后,必须使用应用专用密码而非普通密码进行SMTP认证,否则会登录失败。
  • Gmail的SMTP配置也可尝试使用TLS端口587,对应将SMTPSecure设为tls,部分网络环境下兼容性更好。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 04:45:36