【问题标题】:502 Bad Gateway - Sendgrid PHP Mail Issue502 Bad Gateway - Sendgrid PHP 邮件问题
【发布时间】: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

标签: php email nginx sendgrid


【解决方案1】:

SendGrid 存在一些奇怪的问题。有时,API 会在后端自动软删除。为了使代码正常工作,我必须创建一个新的 API 密钥并重试。解决方案奏效了。

注意 - GitHub 中的一些问题指示使用发件人地址初始化构造函数,但这不是实际问题。 解决方法是在仪表板中创建一个新的 API 密钥,直到 SendGrid 调查 API 被软删除的原因,以便 API 调用 返回错误。

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 2019-03-07
    • 1970-01-01
    • 2016-09-30
    • 2020-04-05
    • 2020-07-12
    • 2023-03-20
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多