Fatal error while trying to send email using SwiftMailer and Sendgrid

I get this error when trying to send an email using swiftmailer and sendgrid smtp

Fatal error: * Failed to throw "Swift_TransportException" with the message "Expected response code 250, but received the code", with the message "" * *

Here is 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 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); } 

Idea?

thanks

+3
source share
3 answers

This can lead to a number of reasons. It is most likely that your server is connecting to external hosts. Other possibilities are that you are using an old version of PHP that has an openSSL error or that your speed is limited by something.

You should take a look at this question for detailed information on the external host problem: send mail via sendgrid


In a separate note, you should use the SendGrid PHP library if you want to send emails using SendGrid. It affects a whole bunch of subtle nuances with Swift and shipping in general. In addition, it gives you access to the HTTP API if you cannot use SMTP for any reason.

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

Full disclosure . I work as an evangelist developer for SendGrid and from time to time I work with the PHP library.

+3
source

there can be two options for sendgrid:

  • you may have invalid authentication data

  • your account can still be provided, so you need to wait a bit for it to be fully activated.

So

Your account is still provided, you will not be able to send emails.

+3
source

You will also receive this error if your account is banned (for example, credit card expiration date).

Note. SendGrid does not discard messages, they hold them until your billing problem is resolved, and then process them even when this error is returned.

0
source

Source: https://habr.com/ru/post/1204431/


All Articles