【发布时间】: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_USERNAME 和alias@example.com 作为MAIL_ADDRESS,但这也没有用。
结果
没有报错,页面显示“成功”邮件信息;但是,当我访问我的用户时,实际上没有发送/接收邮件。由于 GSuite 显然将所有别名邮件路由到我的标准地址,我应该会看到它。
如果我遗漏了什么,请告诉我,谢谢!
【问题讨论】:
-
如果您设置了
$mail->SMTPDebug = 2;,您可以观看 SMTP 对话,看看里面是否有什么有趣的东西。单独的事情:你不应该在AltBody或Subject上调用htmlspecialchars- 它不被视为 HTML,所以如果有什么你应该调用strip_tags。 -
啊,谢谢。我一定会更改
htmlspecialchars引用。就SMTPDebug而言,我将其设置为 3 级并且没有发现任何异常,这让我感到困扰。我会再试一次并发布输出(可能两者都有)。 -
Gmail 通常的做法是,如果您尝试使用其他东西,它只会默默地将发件人地址更改为帐户地址。不过,我已经看到别名起作用了。
-
@Synchro 他们是如何让别名起作用的?
-
您必须在您的 gmail 设置中定义别名;完成后,您可以将它们用作发件人地址。
标签: php gmail phpmailer google-apps