Send emails via sendgrid

I am trying to send mail through sendgrid, here is 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; } 

I got this error: Swift_TransportException

The expected response code is 250, but received the code ", with the message" "

... \ SwiftMailer \ Lib \ Classes \ Swift \ Transport \ AbstractSmtpTransport.php (422)

Please help me!

+2
source share
5 answers

Try sending a message on one line to see if this works or returns a different error. If it returns an error, it may be a problem with the server or authentication.

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

If this works, you can try using the code below, and if it works with it, it will show the recipients instead of crashes.

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

In addition, check if all the variables are empty.

Additional examples: http://swiftmailer.org/docs/sending.html#quick-reference-for-sending-a-message

UPDATE

Sendmail Code:

 //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); 
+3
source

(full disclosure: I work in SendGrid as a web developer)

We recently released a new PHP library that can solve some of these problems. We still use Swift, but if I remember, the library sets it up for you now, which makes it a little easier to get.

Feel free to contact us for help!

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

+5
source

two resonances:

  • 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.

+2
source

I have the same error message with my symfony 2 configuration for sendgrid. I hope you saw this guide: http://docs.sendgrid.com/documentation/get-started/integrate/examples/symfony-example-using-smtp/

Try changing the port to 587. Let me know that this works.

0
source

Maybe you where too fast? I encountered the same problem after kickstart using sendgrid and now using web-api, I got a real "account disconnected" error ...

I did not read the full text after the first login - they will first look at the account until it is activated ... OK, so I'm waiting ^^

0
source

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


All Articles