4

I know this question has a bunch of answers like this on SO already but none of the solutions seem to work for me.

I am trying to send an email from my Google Apps Domain. Emails are being sent most of the time but sometimes this exception gets thrown and the email doesn't get sent.

Uncaught PHP Exception Swift_TransportException: "Expected response code 220 but got code "", with message """ at C:\HostingSpaces\abc\domain.com\wwwroot\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\AbstractSmtpTransport.php line 383 {"exception":"[object] (Swift_TransportException(code: 0): Expected response code 220 but got code \"\", with message \"\" at C:\\HostingSpaces\\abc\\domain.com\\wwwroot\\vendor\\swiftmailer\\swiftmailer\\lib\\classes\\Swift\\Transport\\AbstractSmtpTransport.php:383)"} []

Yes, I use SSL, yes, I have given access to less secure apps and yes I use port 465. I have enabled spooling so that the app doesn't crash because the exception doesn't get caught.

My config.yml is below

swiftmailer:
    transport:  smtp
    host:       smtp.gmail.com
    username:   no-reply@googledomain.com
    password:   password
    port:       465
    encryption: ssl
    spool: { type: memory }

And my email class is below

public function send_new_order_email($entity) {
    try{
        $message = $this->getEmailObj("toaddress@gmail.com");
        $message->setBody(
                $this->renderView(
                    'APIBundle:Emails:ops_neworder.html.twig',
                    array('entity' => $entity)
                ),
                'text/html'
            )->setSubject('New Order : '.$entity->getId());

        $this->container->get('mailer')->send($message);
    }
    catch(Exception $e){}
}

private function getEmailObj($email){
    $message = \Swift_Message::newInstance()
            ->setFrom('no-reply@googledomain.com')
            ->setTo($email);
    return $message;
}

Do you have any suggestions to make this work?

Community
  • 1
  • 1
user972616
  • 1,324
  • 3
  • 18
  • 38

1 Answers1

0

There is in Symfony with Swiftmailer an specialized configuration for Gmail, it handle the email communication forgetting the rest of the configuration. This work for me in many ocassions:

swiftmailer:
    transport: gmail
    username:  your_gmail_username
    password:  your_gmail_password

I hope this make sense for you

You have more information here

Jose M. González
  • 12,590
  • 1
  • 13
  • 9