尝试使用 SwiftMailer 和 Sendgrid 发送电子邮件时出现致命错误 [英] Fatal error when trying to send email using SwiftMailer and Sendgrid

查看:50
本文介绍了尝试使用 SwiftMailer 和 Sendgrid 发送电子邮件时出现致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 swiftmailer 和 sendgrid smtp 发送电子邮件时遇到此错误

I'm getting this error when trying to send an email using swiftmailer and the sendgrid smtp

致命错误: *未捕获的异常Swift_TransportException",消息预期响应代码 250,但得到代码",消息"'*

Fatal error: *Uncaught exception 'Swift_TransportException' with message 'Expected response code 250 but got code "", with message ""'*

这是我的代码:

$hdr = new SmtpApiHeader();


// Set all of the above variables
$hdr->addTo($toList);
$hdr->addSubVal('-name-', $nameList);
$hdr->addSubVal('-time-', $timeList);

// Specify that this is an initial contact message
$hdr->setCategory("initial");

// The subject of your email
$subject = 'Example SendGrid Email';

// Where is this message coming from. For example, this message can be from 
// support@yourcompany.com, info@yourcompany.com
$from = array('no-reply@mupiz.com' => 'Mupiz');
$to = array('antonin@noos.fr'=>'AN',"antonin@mupiz.com"=>"AN2s");

$text="Hello -name-
       Thank you for your interest in our products. We have set up an appointment
             to call you at -time- EST to discuss your needs in more detail.

                Regards,

                Fred, How are you?
      ";
$html = "
<html>
  <head></head>
  <body>
    <p>Hello -name-,<br>
       Thank you for your interest in our products. We have set up an appointment
             to call you at -time- EST to discuss your needs in more detail.

                Regards,

                Fred, How are you?<br>
    </p>
  </body>
</html>
";

// Your SendGrid account credentials
$username = 'XXXX';
$password = 'XXXX';

// Create new swift connection and authenticate
$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);

// add SMTPAPI header to the message
// *****IMPORTANT NOTE*****
// SendGrid's asJSON function escapes characters. If you are using Swift Mailer's
// PHP Mailer functions, the getTextHeader function will also escape characters.
// This can cause the filter to be dropped.
$headers = $message->getHeaders();
$headers->addTextHeader('X-SMTPAPI', $hdr->asJSON());

// 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
// If we specify the names in the X-SMTPAPI header, then this will always be 1.
echo 'Message sent out to '.$recipients.' users';
}
// something went wrong =(
else
{
echo "Something went wrong - ";
print_r($failures);
}

一个想法?

谢谢

推荐答案

导致这种情况的原因可能有很多.最有可能的情况是您的服务器已关闭与外部主机的连接.其他可能性是您使用的旧版 PHP 存在 openSSL 错误,或者您的速率受到某些限制.

There could be a number of things causing this. The most likely one is that your server has connecting to external hosts turned off. Other possibilities are that you're using an old version of PHP that has an openSSL error or that you're being rate limited by something.

您应该查看此问题以了解有关外部主机问题的详细信息:通过 sendgrid 发送邮件

You should take a look at this question for details on the external host issue: send mails via sendgrid

另外,如果您想使用 SendGrid 发送电子邮件,您应该使用 SendGrid PHP 库.它解决了 Swift 和发送的一大堆微妙的细微差别.此外,它还允许您访问 HTTP API,以防您因任何原因无法使用 SMTP.

On a separate note, you should use the SendGrid PHP library if you want to send emails using SendGrid. It addresses a whole bunch of subtle nuances with Swift and sending in general. Also, it gives you access to the HTTP API in case you can't use SMTP for whatever reason.

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

完全披露:我是 SendGrid 的开发人员布道者,不时研究 PHP 库.

Full Disclosure: I work as a developer evangelist for SendGrid and work on the PHP library from time to time.

这篇关于尝试使用 SwiftMailer 和 Sendgrid 发送电子邮件时出现致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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