symfonysymfony5phpdotenvsymfony-mailer

Symfony 5.3 Mailer setup env vars not reading correctly


In a Symfony 5.3 project I am using the Mailer (..\Symfony\Component\Mailer\MailerInterface) to send mails. For devolopment I required "symfony/google-mailer" with composer and set it up in .env.local. Say username is "example@gmail.com" and password is "0000".

In .env.local I specified

MAILER_USER='example@gmail.com'
MAILER_PASSWORD='0000'

MAILER_DSN=gmail://MAILER_USER:MAILER_PASSWORD@default

which results in error

Failed to authenticate on SMTP server with username "MAILER_USER" using the following authenticators: "LOGIN", "PLAIN", "XOAUTH2". Authenticator "LOGIN" returned "Symfony\Component\Mailer\Exception\TransportException: Expected response code "235" but got code "535", with message "535-5.7.8 Username and Password not accepted.

As stated in the docs I changed the one special character in user name ("@") to it's URL-encoded form like

MAILER_USER='example%40gmail.com'

which then results in the error

Email "example%40gmail.com" does not comply with addr-spec of RFC 2822.

(Obviously the URL-encoding didn't work like expected (because it wasn't reversed?)) I tried to load the env vars in paramaters in services.yaml and use the parameters instead - but that lead to the first error too.

If I write the auth infos into the MAILER_DSN env var directly it just works fine without problems, like

MAILER_DSN=gmail://example@gmail.com:0000@default

So this seems to be a syntax problem which I can't figure out from the docs. Any hints?


Solution

  • You should remove the single quotes and you need to wrap the env variables used in other env variables with ${} sic

    MAILER_USER=example@gmail.com
    MAILER_PASSWORD=0000
    
    MAILER_DSN=gmail://${MAILER_USER}:${MAILER_PASSWORD}@default
    

    Result:

    $_SERVER['MAILER_DSN'] = "gmail://example@gmail.com:0000@default"