'Try to use Symfony mailer with Gmail

Try to use Symfony mailer with Gmail on my Symfony 5.1 app.

mailer.yaml:

framework:
    mailer:
        dsn: '%env(MAILER_DSN)%'

.env:

MAILER_DSN=gmail+smtp://[email protected]:mypassword@localhost

But each time I send a mail, I got:

Connection could not be established with host "ssl://smtp.gmail.com:465": stream_socket_client():
SSL operation failed with code 1. OpenSSL Error messages:
error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

Don't understand why... According to the https://symfony.com/doc/current/mailer.html it should work.

I activated low security app on my Gmail account.

Thanks for help



Solution 1:[1]

To use Gmail, you need the package symfony/google-mailer (composer require symfony/google-mailer)

And this configuration in your .env file:

###> symfony/google-mailer ###
# Gmail SHOULD NOT be used on production, use it in development only.
MAILER_DSN=gmail://YourEmailAddress:YourPassword@default?verify_peer=0
###< symfony/google-mailer ###

An example:

MAILER_DSN=gmail://[email protected]:admin123@default?verify_peer=0

Warning:

Although it’s not recommended to disable this verification for security reasons, it can be useful while developing the application or when using a self-signed certificate

Solution 2:[2]

I know you've said you've done it, but I usually get this error because I need to allow "less secure apps" on my Gmail account.

  1. Connect to Gmail
  2. Go to https://www.google.com/settings/security/lesssecureapps and click Authorize
  3. Go to https://accounts.google.com/DisplayUnlockCaptcha and click Continue
  4. Retry sending an email from your website, and if it still doesn't work repeat points 2 & 3. Sometimes I have to try a few times...

Solution 3:[3]

You need to make sure you are using an SSL certificate when sending emails through Gmail.

See:

https://symfony.com/doc/3.0//email/gmail.html

Solution 4:[4]

That can come from an async property that is defined by config/packages/messenger.yaml see https://github.com/symfony/symfony/issues/45811

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Victor Weiss
Solution 3 William Ward
Solution 4 Paul Gilbert