【问题标题】:Send mail from Gsuite mail alias with PHPMailer使用 PHPMailer 从 G Suite 电子邮件别名发送邮件
【发布时间】:2017-03-21 15:48:21
【问题描述】:

我正在尝试在我的网站上使用 PHPMailer 来发送邮件作为联系表单的一部分。我想使用contact@example.com 而不是standard_email@example.com 来发送邮件,用于过滤和安全。我的邮件由 GSuite(以前称为 Google Apps)处理,设置如下:

  • 用户 - standard_email@example.com
  • 别名 - contact@example.com --> standard_email@example.com

在 PHPMailer 配置中使用 standard_email@example.com 时,我的发送工作正常,但是当我尝试使用别名发送时,它不起作用。这对于 GSuite 别名是不可能的吗?

联系控制器

define("SMTP_HOST", "smtp.gmail.com");
define("MAIL_ADDRESS", "alias@example.com");
define("SMTP_USERNAME", "alias@example.com");
...

//Configure SMTP authentication
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = SMTP_HOST;
$mail->SMTPAuth = true;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
$mail->SMTPSecure = "tls";
$mail->Port = SMTP_PORT;

//Set email headers
$mail->setFrom(MAIL_ADDRESS, "Contact Us Submission");
$mail->addAddress(MAIL_ADDRESS, "Contact Us Submission");
$mail->addReplyTo($form_email, $form_name);
$mail->isHTML(true);

//Set email content
$mail->Subject = htmlspecialchars($form_subject);
$mail->Body = htmlspecialchars($form_comments);
$mail->AltBody = htmlspecialchars($form_comments);

//Attempt to send the message and display the results to the user
if ($mail->send() == false) {
    return new MailResponse(1, "Message could not be sent", $contactSubmission);
} else {
    return new MailResponse(0, "Message sent successfully", $contactSubmission);
}

我也尝试过使用standard_email@example.com 作为SMTP_USERNAMEalias@example.com 作为MAIL_ADDRESS,但这也没有用。

结果

没有报错,页面显示“成功”邮件信息;但是,当我访问我的用户时,实际上没有发送/接收邮件。由于 GSuite 显然将所有别名邮件路由到我的标准地址,我应该会看到它。

如果我遗漏了什么,请告诉我,谢谢!

【问题讨论】:

  • 如果您设置了$mail->SMTPDebug = 2;,您可以观看 SMTP 对话,看看里面是否有什么有趣的东西。单独的事情:你不应该在 AltBodySubject 上调用 htmlspecialchars - 它不被视为 HTML,所以如果有什么你应该调用 strip_tags
  • 啊,谢谢。我一定会更改htmlspecialchars 引用。就SMTPDebug 而言,我将其设置为 3 级并且没有发现任何异常,这让我感到困扰。我会再试一次并发布输出(可能两者都有)。
  • Gmail 通常的做法是,如果您尝试使用其他东西,它只会默默地将发件人地址更改为帐户地址。不过,我已经看到别名起作用了。
  • @Synchro 他们是如何让别名起作用的?
  • 您必须在您的 gmail 设置中定义别名;完成后,您可以将它们用作发件人地址。

标签: php gmail phpmailer google-apps


【解决方案1】:

@Synchro 的解决方案有效,为方便起见,这里是一个简短的逐步解决方案:

  1. 在您的计算机上,打开 Gmail。
  2. 点击右上角的设置设置,然后点击查看所有设置。
  3. 点击“帐户和导入”或“帐户”选项卡。
  4. 在“发送邮件”部分,点击添加另一个电子邮件地址。
  5. 输入您的姓名和您要发送的地址。
  6. 点击下一步,然后发送验证。
  7. 点击添加帐号

了解更多:https://support.google.com/mail/answer/22370?hl=en

现在一切都应该正常了:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-23
    • 2021-02-25
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多