send mails via sendgrid

13,080

Solution 1

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.

For more examples see: 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('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => 'A name'))
  ->setBody('Here is the message itself')
  ;

//Send the message
$numSent = $mailer->send($message);

printf("Sent %d messages\n", $numSent);

Solution 2

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

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

Don't hesitate to contact us for help, too!

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

Solution 3

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

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

Share:
13,080
BKF
Author by

BKF

Engineer in computer science (Information System)

Updated on September 07, 2022

Comments

  • BKF
    BKF over 1 year

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

    I got this error : Swift_TransportException

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

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

    please help me !

  • BKF
    BKF over 12 years
    thanks for help, but it doesn't work :( may be some config parameters aren't ok ?
  • mat
    mat over 12 years
    Did you get another error or the same one? Your code looks fine, perhaps you should try the sendmail function first to see if it's a server issue. I'll update my post with the code.
  • BKF
    BKF over 12 years
    It works by this code : $transport = Swift_MailTransport::newInstance(); but I don't receive any mail ! it means ??
  • mat
    mat over 12 years
    It probably means that smtp.sendgrid.net isn't supported on your server (external hosts) and mail() for some reason isn't delivered. Does it work to just send mail like this: mail("[email protected]", "A subject", "A message", "FROM: [email protected]");
  • mat
    mat over 12 years
    You should ask your host why the SMTP is not working. If you found my help useful please accept my answer.
  • BKF
    BKF over 12 years
    It probably means that smtp.sendgrid.net isn't supported on your server (external hosts). thank you mat this is the problem
  • BKF
    BKF over 12 years
    smtp.sendgrid.net isn't supported on my server (external hosts)
  • Dvid Silva
    Dvid Silva almost 11 years
    awesome. I didn't find however here how to see if the email was sent and if it didn't how to retrieve the errors :(
  • iandouglas
    iandouglas almost 11 years
    For that, you'll want to set an endpoint to receive information from SendGrid's "event notification" system. We'll ping that endpoint every step along the way (if you enable "batch" mode, it'll queue up a batch of JSON to send every minute or so, if you send a lot of Email). The JSON data, if it bounces/etc should return the error as well. Check out sendgrid.com/docs/API_Reference/Webhooks/event.html