Search code examples
symfonyemailswiftmailer

Sending Email in Symfony 3 in dev mode


In need help learning how to send email in development mode using the Symfony framework with the Swift_mailer library.

Here is my config_dev.yml file:

swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }

And my config.yml file:

swiftmailer:
transport: '%mailer_transport%'
host: '%mailer_host%'
username: '%mailer_user%'
password: '%mailer_password%'
spool: { type: memory }

And my parameters.yml file:

....
mailer_transport: gmail
mailer_host: 127.0.0.1
mailer_user: 'my_gamil_here'
mailer_password: 'my_password_here'

Then in my controller I have the following:

public function indexAction(\Swift_Mailer $mailer)
{
    $message = (new \Swift_Message('Hello Email'))
        ->setFrom('westtexascentral555@gmail.com')
        ->setTo('kaleyaw@yahoo.com')
        ->setBody(
            $this->renderView(
                // app/Resources/views/Emails/registration.html.twig
                'Emails/registration.html.twig',
                array('name' => 'James')
            ),
            'text/html'
        );

    $mailer->send($message);

    return new Response('Email sent');
}

Can Someone tell me what I'm going wrong. Nothing gets sent to the email account.


Solution

    1. Make sure, that mailer_user is set only to your gmail username (without host part). Let's say if I have email myemail@gmail.com, my username would be myemail.

    2. Are you using 2-step verification for your gmail account? If so, additionally you need to generate app password for your account, as specified in docs:

    If your Gmail account uses 2-Step-Verification, you must generate an App password and use it as the value of the mailer_password parameter. You must also ensure that you allow less secure apps to access your Gmail account.

    Shortly, you can follow here, login, select app (in this case Mail) and the device you want to use mailer with, press Generate and you will see modal with your app password, similar to this snub udpz xcvt jrdp. Copy this password, and use it as your gmail password (without spaces).

    If this doesn't help, also check logs, usually if there is a problem with smtp server, swiftmailer prints error to the logs.