当我尝试使用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";
}
?>
执行代码后应发送电子邮件。
答案 0 :(得分:0)
SendGrid存在一些奇怪的问题。有时,API会在后端自动被软删除。为了使代码正常工作,我必须创建一个新的API密钥并重试。解决方案成功了。
注意 -GitHub中的一些问题指示使用发件人地址初始化构造函数,但这不是实际问题。 解决方法是在仪表板上创建一个新的API密钥,直到 SendGrid调查了为什么将API进行软删除以便对API进行调用 返回错误。