使用PHP Mailer和GSuite发送邮件时DKIM错误

时间:2019-10-08 01:27:17

标签: php email phpmailer 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无效。

我认为这是因为我正在从外部服务器(而不是Google)发送邮件,并且我的MX记录指向Google,但是我确实需要这些电子邮件才能获得,我应该如何解决此问题?

有没有一种方法可以在PHP Mailer中进行配置?谢谢。

1 个答案:

答案 0 :(得分:0)

抱怨的原因是因为您根本没有在DKIM上签名!

您可以使用PHPMailer进行DKIM签名,但是需要一定数量的设置。 PHPMailer提供了一些代码来帮助您做到这一点。确保您使用的是PHPMailer 6.1.1 or later;较旧的版本存在影响DKIM签名的错误。

首先,您需要create your DKIM keys并将其放入DNS。

现在,您需要更改脚本以使用私钥对邮件签名,如this example所示;您需要添加的部分是:

//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'];