symfony2 unable to send email via gmail

3.9k Views Asked by At

I have been trying to send email using swiftmailer, however I am not able to do so, I am getting following exception

request.CRITICAL: Uncaught PHP Exception Swift_TransportException: "Failed to authenticate on SMTP server with username "[email protected]" using 1 possible authenticators" at /home/ubuntu/Symfony/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php line 184 [] []

Can anybody tell what am I missing here? It looks like authentication is failing for some reason.

here is the snippet from my config file:

swiftmailer:    

  transport:  gmail
  encryption: ssl
  auth_mode:  login
  host:       smtp.gmail.com
  username:   [email protected]
  password:   password

My Dev server has ubuntu v13 OS

1

There are 1 best solutions below

1
On

Try removing from your config_dev.php

encryption: ssl
auth_mode:  login

There is no need to specify the encryption: and auth_mode: if you're using transport: gmail See the symfony2.3 cookbook How to use Gmail to send Emails

Quick and dirty way to test the email options. Init a new symfony 2.3 project and in the Default controller in acme /DemoBundle put this action.

/**
 * @Route("/mail")
 * @Template()
 */
public function mailAction() {


    $message = \Swift_Message::newInstance()
            ->setSubject('Hello Email')
            ->setFrom('[email protected]')
            ->setTo('[email protected]')
            ->setBody(
            $this->renderView(
                    'DemoBundle:Default:email.txt.twig', array('name' => 'User1')
            )
            )
    ;
    $this->get('mailer')->send($message);

    return array('name' => 'whatever');
}

Twig templates in Acme/Demo/Bundle/Resources/views/Default Frontend template

mail.html.twig

{% extends '::base.html.twig' %}
  {% block body %}
     This is mail template 
  {% endblock %}

The mailer template. email.txt.twig

This is a test email to {{name }}

Going to the /mail should send the email.