【问题标题】:send mails via sendgrid通过 sendgrid 发送邮件
【发布时间】:2023-03-16 04:11:02
【问题描述】:

我正在尝试通过 sendgrid 发送邮件,这里是我的代码:

// Setup Swift mailer parameters
        $transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 25);
        $transport->setUsername($username);
        $transport->setPassword($password);
        $swift = Swift_Mailer::newInstance($transport);

        // Create a message (subject)
        $message = new Swift_Message($subject);

        // attach the body of the email
        $message->setFrom($from);
        $message->setBody($html, 'text/html');
        $message->setTo($to);
        $message->addPart($text, 'text/plain');

        // send message
        if ($recipients = $swift->send($message, $failures))
        {              
          // This will let us know how many users received this message
          echo 'Message sent out to '.$recipients.' users';exit;
        }

我收到了这个错误: Swift_TransportException

预期的响应代码为 250,但得到代码“”,带有消息“”

...\swiftMailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php(422)

请帮帮我!

【问题讨论】:

  • 有关支持问题请联系库供应商。

标签: php swiftmailer sendgrid


【解决方案1】:

(完全披露:我在 SendGrid 工作是一名网络开发人员)

我们最近发布了一个新的 PHP 库,可以解决其中的一些问题。我们仍然使用 Swift,但如果我记得的话,这个库现在已经为你设置好了这些东西,让它更容易上手。

也不要犹豫与我们联系以寻求帮助!

http://github.com/sendgrid/sendgrid-php

【讨论】:

  • 太棒了。但是我在这里没有找到如何查看电子邮件是否已发送以及如果没有如何检索错误:(
  • 为此,您需要设置一个端点来接收来自 SendGrid 的“事件通知”系统的信息。我们将在此过程中的每一步 ping 该端点(如果您启用“批处理”模式,如果您发送大量电子邮件,它将每分钟左右排队发送一批 JSON)。 JSON数据,如果它反弹/等也应该返回错误。查看sendgrid.com/docs/API_Reference/Webhooks/event.html
【解决方案2】:

尝试在一行中发送消息,看看是否有效或返回另一个错误。如果它返回错误,则可能是服务器或身份验证问题。

$result = $swift->send($message);

如果可行,您可以尝试下面的代码,如果可行,请对其进行调整,以便显示您的收件人而不是失败。

if (!$swift->send($message, $failures))
{
  echo "Failures:";
  print_r($failures);
}

除此之外,检查所有变量是否不为空。

更多示例见:http://swiftmailer.org/docs/sending.html#quick-reference-for-sending-a-message

更新

发送邮件代码:

//Create the Transport
$transport = Swift_MailTransport::newInstance();

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

//Create a message
$message = Swift_Message::newInstance('Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

//Send the message
$numSent = $mailer->send($message);

printf("Sent %d messages\n", $numSent);

【讨论】:

  • 感谢您的帮助,但它不起作用:(可能是某些配置参数不正确?
  • 您遇到了另一个错误还是同样的错误?您的代码看起来不错,也许您应该先尝试 sendmail 功能,看看它是否是服务器问题。我会用代码更新我的帖子。
  • 它通过以下代码工作:$transport = Swift_MailTransport::newInstance();但我没有收到任何邮件!这意味着??
  • 这可能意味着您的服务器(外部主机)不支持 smtp.sendgrid.net 并且由于某种原因未发送 mail()。像这样发送邮件是否有效:mail("yourmail@mail.com", "A subject", "A message", "FROM: noreply@mail.com");
  • 您应该询问您的主机为什么 SMTP 不工作。如果您觉得我的帮助有用,请接受我的回答。
【解决方案3】:

两个原因:

  1. 您的验证数据可能有误

  2. 您的帐户可能仍处于预配状态,因此您必须稍等片刻才能完全激活它

所以

您的帐户仍在配置中,您可能无法发送 电子邮件。

【讨论】:

    【解决方案4】:

    我在 Symfony 2 的 sendgrid 配置中出现了同样的错误消息。希望你看过这本手册:http://docs.sendgrid.com/documentation/get-started/integrate/examples/symfony-example-using-smtp/

    尝试将端口更改为 587。让我知道它有效。

    【讨论】:

    • 我的服务器(外部主机)不支持 smtp.sendgrid.net
    【解决方案5】:

    也许你的速度太快了?在使用 sendgrid 和现在使用 web-api 后,我遇到了同样的问题,我得到了真正的错误“帐户已禁用”...

    第一次登录后我没有阅读全文 - 他们会先查看帐户,直到它被激活...好的,所以我等待^^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多