Fatal error when trying to send email using SwiftMailer and Sendgrid

蹲街弑〆低调 提交于 2020-01-24 10:31:06

问题


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

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

Here's my code :

$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);
}

An idea ?

Thanks


回答1:


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.

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


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

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




回答2:


for sendgrid might be two resons:

  1. you may have the authentification data wrong

  2. your account might still be provisioned, so you must wait a bit for it to be fully activated

so

  • check all the autentification data from app/config/mail.php to match with the data from https://sendgrid.com/docs/Integrate/index.html

  • check also not the have a top message/notification on your sendgrid account like:

Your account is still being provisioned, you may not be able to send emails.




回答3:


You will also receive this error if your account is frozen for billing reasons (e.g. lapsed credit card expiry date).

Note: SendGrid doesn't drop the message(s), they hold them until your billing issue is resolved then process them even when this error is returned.



来源:https://stackoverflow.com/questions/11158845/fatal-error-when-trying-to-send-email-using-swiftmailer-and-sendgrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!