php - 使用 PHPMailer 从 G Suite 电子邮件别名发送邮件

标签 php gmail phpmailer google-apps

我正在尝试在我的网站上使用 PHPMailer 将邮件作为联系表单的一部分发送。我想使用<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9efdf1f0eafffdeadefbe6fff3eef2fbb0fdf1f3" rel="noreferrer noopener nofollow">[email protected]</a>而不是<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="deadaabfb0babfacba81bbb3bfb7b29ebba6bfb3aeb2bbf0bdb1b3" rel="noreferrer noopener nofollow">[email protected]</a>发送邮件,用于过滤和安全。我的邮件由 GSuite(以前称为 Google Apps)处理,设置如下:

  • 用户 - <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f48780959a90958690ab9199959d98b4918c9599849891da979b99" rel="noreferrer noopener nofollow">[email protected]</a>
  • 别名 - <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32515d5c4653514672574a535f425e571c515d5f" rel="noreferrer noopener nofollow">[email protected]</a> --> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83f0f7e2ede7e2f1e7dce6eee2eaefc3e6fbe2eef3efe6ade0ecee" rel="noreferrer noopener nofollow">[email protected]</a>

使用<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="314245505f555043556e545c50585d715449505c415d541f525e5c" rel="noreferrer noopener nofollow">[email protected]</a>时我的发送工作完美在 PHPMailer 配置中,但是当我尝试使用别名发送时,它不起作用。这对于 GSuite 别名来说不可能吗?

联系 Controller

define("SMTP_HOST", "smtp.gmail.com");
define("MAIL_ADDRESS", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e5f52575f4d7e5b465f534e525b105d5153" rel="noreferrer noopener nofollow">[email protected]</a>");
define("SMTP_USERNAME", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22434e4b435162475a434f524e470c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>");
...

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

我也尝试过使用<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16656277787277647249737b777f7a56736e777b667a733875797b" rel="noreferrer noopener nofollow">[email protected]</a>作为SMTP_USERNAME<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0f02070f1d2e0b160f031e020b400d0103" rel="noreferrer noopener nofollow">[email protected]</a>作为MAIL_ADDRESS ,但这也不起作用。

结果

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

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

最佳答案

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

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

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

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

关于php - 使用 PHPMailer 从 G Suite 电子邮件别名发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42931927/

相关文章:

php - Laravel 获取发送到请求的数据

php - 以 HTML 形式返回 PHP/MySQL 结果以供编辑

php - Mysql查询日期格式、phpmailer

PHPMailer:使用远程 SMTP 服务器,在本地主机下工作,远程服务器上的连接被拒绝 (111)

php - 如何从数据库列中检查 `if` 语句,如 '$a > $b'

PHP 和 curl : is it possible to get a cookie into variable instead of file without parsing headers?

html - 适用于 Android 的 Gmail 客户端不会将我的 css 样式应用于电子邮件

javascript - 将 JavaScript 数组转换为 JSON 对象

php - Gmail API : Insufficient Permission

php - 希望将 phpmailer 库添加到 Zend Framework 设置中