通过 sendgrid 发送邮件 [英] send mails via sendgrid

查看:36
本文介绍了通过 sendgrid 发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to send mails via sendgrid, here my code :

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

I got this error : Swift_TransportException

预期响应代码为 250,但得到代码",消息为"

Expected response code 250 but got code "", with message ""

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

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

请帮帮我!

推荐答案

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

Try to send the message in one line to see if that works or returns another error. If it returns an error, it's probably a server or authentication issue.

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

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

If that works, you could try the code below and if it works tweak it so it shows your recipients instead of failures.

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

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

Other than that, check if all variables are not empty.

更多例子见: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);

这篇关于通过 sendgrid 发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆