SendGrid 仅向 Gmail 发送电子邮件

SendGrid only sends emails to Gmail

我正在尝试通过 phpMailer 集成 SendGrid API(也尝试使用 php API 库)。在本地主机和实时服务器中,phpmailer 获得 250 OK,API 库响应获得 202 OK。

提交后(4 位收件人),我只收到 1 封发到 gmail 的电子邮件。另一个域电子邮件地址在 SendGrid 活动页面中显示已发送,但我没有收到这些电子邮件,除了 Gmail。

我的发件人电子邮件地址和域地址已经过验证。我的代码有什么问题?

php邮件程序

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSMTP();
$mail->Host          = 'smtp.sendgrid.net';
$mail->Port          = 587;
$mail->Username      = 'apikey';
$mail->Password      = 'API_KEY';
$mail->SMTPAuth      = true;
$mail->SMTPDebug     = true;
$mail->Debugoutput   = 'html';
$mail->SMTPKeepAlive = true;
$mail->SMTPSecure = 'TLS';
$mail->Priority = 3;
$mail->Encoding = 'base64';
$mail->CharSet = "utf-8";
$mail->SetFrom('sender@mail.com', $name = 'Sender Title');
$mail->AddAddress('receiver@mail.com', 'Receiver Title');
$mail->Subject  =  'SendGrid Test';
$mail->MsgHTML('Mail body');
$sending = $mail->Send();
$mail->SmtpClose();

有API图书馆

$email = new \SendGrid\Mail\Mail();
$email->setFrom("sender@mail.com", "Example User");
$email->setSubject("Sending with Twilio SendGrid is Fun");
$email->addTo("receiver@mail.com", "Example User");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid('API_KEY');
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

如果您的电子邮件发送成功并被 Gmail 接收,那么您的代码没有任何问题(除了 Synchro 在评论中指出的小问题)。

Email deliverability isn't an exact science, so there could be something in the contents of your email that triggers mail blocking for some inboxes. I'd follow the guidelines in this article on email deliverability 以确保您设置自己以将电子邮件发送到收件箱。

除此之外,我还会设置SendGrid Event Webhook。这将根据您的电子邮件正在执行的操作向您发送事件,包括电子邮件被丢弃、延迟、退回、阻止或发送时的发送事件。这可能有助于您了解您的电子邮件发生了什么。