2

我正在尝试通过 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)

请帮我 !

4

5 回答 5

5

(全面披露:我在 SendGrid 担任网络开发人员)

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

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

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

于 2012-03-27T03:42:12.617 回答
3

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

$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);
于 2012-01-04T10:42:27.557 回答
2

两个共鸣:

  1. 你可能有错误的身份验证数据

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

所以

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

于 2014-11-28T10:19:21.733 回答
0

我的 Symfony 2 配置为 sendgrid 时出现了相同的错误消息。希望您已经看过本手册:http ://docs.sendgrid.com/documentation/get-started/integrate/examples/symfony-example-using-smtp/

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

于 2012-01-04T14:51:16.517 回答
0

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

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

于 2013-06-20T16:54:37.787 回答