symfony

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://myadresse@gmail.com: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

  • 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://stackoverflow@gmail.com: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