symfony-docs icon indicating copy to clipboard operation
symfony-docs copied to clipboard

[Mailer] google-mailer DSN configuration not valid anymore

Open jeremyjaouen opened this issue 2 years ago • 8 comments

Symfony version(s) affected

5.4.*

Description

With recents change on GMail, the following configuration for MAILER_DSN: MAILER_DSN=gmail://USERNAME:PASSWORD@default is no more valid, as the connection using username and password are not able anymore.

Is there any existing workaround or another configuration to be able to use SMTP with Google-Mailer ?

How to reproduce

  • Install symfony/google-mailer
  • Setup your .env with MAILER_DSN=gmail://USERNAME:PASSWORD@default (replace your username and password with your own credentials
  • Implement use the MailerInterface to send an email
  • Your Request shouldn't succeed with following error:
message: Failed to authenticate on SMTP server with username "xxxxxxx" using the following authenticators: "LOGIN", "PLAIN", "XOAUTH2". Authenticator "LOGIN" returned "Expected response code "235" but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials y18-20020a05622a121200b002f90a33c78csm17479301qtx.67 - gsmtp".". Authenticator "PLAIN" returned "Expected response code "235" but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials y18-20020a05622a121200b002f90a33c78csm17479301qtx.67 - gsmtp".". Authenticator "XOAUTH2" returned "Expected response code "235" but got code "334", with message "334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==".".

Possible Solution

No response

Additional Context

No response

jeremyjaouen avatar Jul 02 '22 07:07 jeremyjaouen

As stated in the linked documentation you should enable 2-fa and use an App Password (https://support.google.com/accounts/answer/185833) or oauth to sign in. Using the regular password and less secure app option is not supported any more https://support.google.com/accounts/answer/6010255

To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

1ed avatar Jul 04 '22 07:07 1ed

Indeed, I have already read the doc from Google, but it remains that the Symfony Mailer Documentation is no more up to date regarding SMTP and Gmail

jeremyjaouen avatar Jul 13 '22 20:07 jeremyjaouen

@jeremyjaouen Can you submit a PR to update the docs?

fabpot avatar Aug 05 '22 13:08 fabpot

Hello @fabpot, sure I will submit a PR in few days

jeremyjaouen avatar Aug 05 '22 13:08 jeremyjaouen

Hello, I still can't solve the connection problem between Mailer and gmail. did you find the solution

med1995-ops avatar Aug 22 '22 13:08 med1995-ops

I'd got the issue too (symfony/google-mailer": "6.1.*") . Seems GmailTransportFactory ignore dsn paramater "verify_peer" in her method 'create($dsn)', but built-in EsmtpTransportFactory does. So instead of : MAILER_DSN=gmail://USERNAME:PASSWORD@default?verify_peer=0 Use : MAILER_DSN=smtp://USERNAME:[email protected]:465?verify_peer=0

ikonoka avatar Aug 25 '22 11:08 ikonoka

@ikonoka i just tried this but it's not working , i think we should move to another service

mouataz1 avatar Sep 06 '22 11:09 mouataz1

I confirm that using App Password works As @1ed said, you have to enable 2FA then create an App Password (https://support.google.com/accounts/answer/185833)

After creating it you can use MAILER_DSN=gmail://USERNAME:APP-PASSWORD@default

Sevrahk avatar Sep 17 '22 12:09 Sevrahk

@Sevrahk it does work, I hadn't test the mailer functionality in months and had the same issue, after creating the AppPassword and updated the .env MAILER_DSN=gmail://emailAddress:AppPassword@default and it worked. This is my simple code for anyone find it useful. $applicationEmailApr = (new Email()) ->from(new Address('Address', 'Name')) ->to($email) ->subject('subject') ->html("<p>any html within the body</p>"); try { $mailer->send($applicationEmailApr); } catch (TransportExceptionInterface $e) { throw new Exception($e->getMessage()); }

MedDimessi avatar Dec 12 '22 11:12 MedDimessi

2FA is enabled but the problem still persist ... Tried everything:

gmail://[email protected]:myapppass@default
gmail://[email protected]:myapppass@default?verify_peer=0
gmail://[email protected]:myapppass@default?verify_peer=1
gmail+smtp://[email protected]:myapppass@default?verify_peer=0
gmail+smtp://[email protected]:myapppass@default?verify_peer=1
gmail://[email protected]:[email protected]:587?verify_peer=0
gmail://[email protected]:[email protected]:587?verify_peer=1

And every single combination you can think of. Still got the error:

Connection could not be established with host "ssl://smtp.gmail.com:465": stream_socket_client(): Unable to connect to ssl://smtp.gmail.com:465 (Network is unreachable)

Obviously symfony has some problem.

TomaIvanovTomov avatar Dec 17 '22 12:12 TomaIvanovTomov

@TomaIvanovTomov

My configuration is gmail://mygmail:myapppass@default It is close to your first suggestion but without the @gmail.com

Hope it helps ;)

Sevrahk avatar Dec 17 '22 12:12 Sevrahk

@Sevrahk Nope :/ same outcome. I want to switch to tls and 587 port but don't see how to pass the port. Obviously this is the problem. My hosting doesn't allow 465 mails as I am aware. But how to switch to 587 tls ? Literally nothing on that in the documentation.

TomaIvanovTomov avatar Dec 17 '22 12:12 TomaIvanovTomov

@TomaIvanovTomov

In symfony/google-mailer the port 465 is directly set in the constructor so I'm afraid you can't change it in the configuration

Sevrahk avatar Dec 17 '22 13:12 Sevrahk

You can normally set it via DSN and the GoogleTransportFactory should take care of extracting the right port

OskarStark avatar Dec 17 '22 15:12 OskarStark

@OskarStark No, still got it. Connection could not be established with host "ssl://smtp.gmail.com:465": stream_socket_client(): Unable to connect to ssl://smtp.gmail.com:465 (Network is unreachable). Even when the port is set to 587 it still trying to connect through ssl.

$dsn = Transport\Dsn::fromString('gmail://myname:[email protected]:587');
$googleTransport = (new GmailTransportFactory())->create($dsn);
$mailer = new Mailer($googleTransport);

TomaIvanovTomov avatar Dec 17 '22 16:12 TomaIvanovTomov

I'm closing here now. Feel free to contribute some docs about how to configure Gmail properly. On a side note, I would recommend not using Gmail. There are better alternatives in development, and Gmail should never be used in production.

See https://github.com/symfony/symfony-docs/pull/17586

fabpot avatar Dec 18 '22 18:12 fabpot