繁体   English   中英

通过sendgrid发送邮件

[英]send mails via sendgrid

我正在尝试通过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 \\类\\雨燕\\传输\\ AbstractSmtpTransport.php(422)

请帮我 !

(完全披露:我作为Web开发人员在SendGrid工作)

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

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

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

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

$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

UPDATE

Sendmail代码:

//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);

两个人:

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

  2. 您的帐户可能仍处于配置状态,因此您必须等待一段时间才能完全激活它

所以

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM