php - 使用 PHP Mailer 通过 GSuite 发送邮件时出现 DKIM 错误
问题描述
我在 A2 托管,但我使用 GSuite 来处理我的所有邮件。
当我从 Gmail 向 mail-tester.com 发送测试消息时,我得到了一个很棒的评价。
但是,当我使用我的 PHP 脚本发送消息时:
$mail = new PHPMailer(true);
ob_start();
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $pickuploc . '@xxxx.com'; // SMTP username
$mail->Password = 'xxx';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom($pickuploc . '@xxx.com', 'xxx xxxx');
$mail->addAddress($email, $fname . " " . $lname); // Add a recipient
$mail->addBCC($pickuploc . '@xxx.com');
// Content
$mail->isHTML(true); // Set email format to HTML
include 'email-confirmed.html';
$mail->Subject = 'Your Reservation Has Been Confirmed!';
$mail->Body = ob_get_clean();
$mail->AltBody = 'Your reservation has been confirmed.';
$mail->send();
我从 mail-tester.com 收到一条错误消息,显示我的 DKIM 无效。
我认为这是因为我从一个外国服务器(不是谷歌)发送,我的 MX 记录指向谷歌,但我真的需要这些电子邮件才能得到,我应该如何解决这个问题?
有没有办法在 PHP Mailer 中配置它?谢谢。
解决方案
它抱怨的原因是因为您根本没有与 DKIM 签约!
您可以使用 PHPMailer 进行 DKIM 签名,但需要进行一定的设置。PHPMailer 提供了一些代码来帮助您做到这一点。确保您使用的是PHPMailer 6.1.1 或更高版本;旧版本存在影响 DKIM 签名的错误。
首先,您需要创建您的 DKIM 密钥并将它们放入您的 DNS 中。
现在您需要更改脚本以使用您的私钥对消息进行签名,如本例所示;您需要添加的部分是:
//This should be the same as the domain of your From address
$mail->DKIM_domain = 'example.com';
//See the DKIM_gen_keys.phps script for making a key pair -
//here we assume you've already done that.
//Path to your private key:
$mail->DKIM_private = 'dkim_private.pem';
//Set this to your own selector
$mail->DKIM_selector = 'phpmailer';
//Put your private key's passphrase in here if it has one
$mail->DKIM_passphrase = '';
//The identity you're signing as - usually your From address
$mail->DKIM_identity = $mail->From;
//Suppress listing signed header fields in signature, defaults to true for debugging purpose
$mail->DKIM_copyHeaderFields = false;
//Optionally you can add extra headers for signing to meet special requirements
$mail->DKIM_extraHeaders = ['List-Unsubscribe', 'List-Help'];
推荐阅读
- python - Wake Up Agent Python 2.7 script
- vba - 我正在尝试删除 excel 中的单元格,因为它们的值从另一个函数呈现为空白
- javascript - 反应路由器和 webpack 似乎产生了错误的输出,在某些 URL 中给出了 404
- typescript - TypeScript 中带有推导和 `keyof` 的细粒度类型
- python - 什么命令会在 bash 脚本中引发错误?
- ios - 如何删除特定单元格的模糊视图?
- powershell - 在 Powershell 中为任务创建进度条
- c++ - 如何将模板类 X 的模板成员函数声明为嵌套类 X::Y 的朋友
- drools - Drools 决策表 - 如何在同一条件列中使用多个对象
- javascript - 每次我使用路由导航到组件时都会获取角度 http