【发布时间】:2018-12-27 19:37:51
【问题描述】:
当我尝试使用 Sendgrid 的 PHP 库发送电子邮件时,我收到 502 Bad Gateway 错误。我在下面附上了错误日志,
502 Array (
[0] => HTTP/1.1 502 Bad Gateway
[1] => Server: nginx
[2] => Date: Thu, 27 Dec 2018 19:30:14 GMT
[3] => Content-Type: text/html
[4] => Content-Length: 166
[5] => Connection: keep-alive
[6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io
[7] => Access-Control-Allow-Methods: POST
[8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl
[9] => Access-Control-Max-Age: 600
[10] => X-No-CORS-Reason:
https://sendgrid.com/docs/Classroom/Basics/API/cors.html
[11] =>
[12] => )
我不知道我错在哪里,我按照 SendGrid 网站(以及 Github)中的文档进行操作。我已在我使用的代码下方附加了代码。
我已经尝试过在 sendgrid-php 的 GitHub 存储库中的其他问题和问题中发现的可能的故障排除。
<?php
require("sendgrid/sendgrid-php.php");
$email = new \SendGrid\Mail\Mail();
$email->setFrom("noreply@mydomain.com", "Leads");
$email->setSubject($_POST['school'] . " - Appointment");
$tos = [
"support@mydomain.com" => "Support",
$_POST['email'] => $_POST['name']
];
$email->addTos($tos);
$email->addContent("text/html", "<p><strong>Name:</strong> {$_POST['name']}</p>");
$email->addContent("text/html", "<p><strong>Email:</strong> {$_POST['email']}</p>");
$email->addContent("text/html", "<p><strong>Mobile:</strong> {$_POST['mobile']}</p>");
$email->addContent("text/html", "<p><strong>School:</strong> {$_POST['school']}</p>");
$email->addContent("text/html", "<p><strong>Count:</strong> {$_POST['count']}</p>");
$email->addContent("text/html", "<p><strong>Gender:</strong> {$_POST['gender']}</p>");
$sendgrid = new \SendGrid('xxxxxxxxxxxxxxAPIKEYxxxxxxxxxxxxxxxx');
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";
}
?>
执行代码时应该发送电子邮件。
【问题讨论】:
-
您的代码看起来正确。请与 SendGrid 联系,如果您的帐户设置中的国家/地区代码错误,可能会阻止您发送。
-
是的,我看到了,但我不知道如何实现它。你能帮我解决这个问题吗@JeremyHarris