简体   繁体   English

使用 PHPMailer 从 Gsuite 邮件别名发送邮件

[英]Send mail from Gsuite mail alias with PHPMailer

I'm trying to use PHPMailer on my website to send mail as part of a contact form.我正在尝试在我的网站上使用 PHPMailer 将邮件作为联系表单的一部分发送。 I would like to use contact@example.com instead of standard_email@example.com to send mail, for filtering and security.我想使用contact@example.com而不是standard_email@example.com来发送邮件,以进行过滤和安全。 My mail is handled by GSuite (formerly called Google Apps) and is setup like below:我的邮件由 GSuite(以前称为 Google Apps)处理,设置如下:

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

I have sending working perfectly when using standard_email@example.com in the PHPMailer configuration, but when I try to send using the alias it does not work.在 PHPMailer 配置中使用standard_email@example.com时,我的发送工作正常,但是当我尝试使用别名发送时,它不起作用。 Is this not possible with GSuite aliases?这对 GSuite 别名是不可能的吗?

Contact Controller接触控制器

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);
}

I've also tried using standard_email@example.com as the SMTP_USERNAME and alias@example.com as the MAIL_ADDRESS , but that didn't work either.我还尝试使用standard_email@example.com作为SMTP_USERNAMEalias@example.com作为MAIL_ADDRESS ,但这也不起作用。

Results结果

There are no reported errors, and the page displays the "success" mail message;没有报错,页面显示“成功”邮件; however, no mail is actually sent/received when I visit my user.但是,当我访问我的用户时,实际上没有发送/接收邮件。 Since GSuite apparently routes all of the alias mail to my standard address, I should be seeing it.由于 GSuite 显然将所有别名邮件路由到我的标准地址,我应该会看到它。

Let me know if I'm missing something, and thanks!如果我遗漏了什么,请告诉我,谢谢!

The solution from @Synchro works and for convenience here is a short step by step solution: @Synchro 的解决方案有效,为方便起见,这里是一个简短的分步解决方案:

  1. On your computer, open Gmail.在您的计算机上,打开 Gmail。
  2. In the top right, click Settings Settings and then See all settings.在右上角,单击设置设置,然后单击查看所有设置。
  3. Click the Accounts and import or Accounts tab.单击帐户和导入或帐户选项卡。
  4. In the "Send mail as" section, click Add another email address.在“发送邮件”部分中,单击添加另一个电子邮件地址。
  5. Enter your name and the address you want to send from.输入您的姓名和要发送的地址。
  6. Click Next Step and then Send verification.单击下一步,然后单击发送验证。
  7. Click Add Account单击添加帐户

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

Everything should be working now :)现在一切都应该正常工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM