Skip to content
This repository was archived by the owner on Feb 6, 2022. It is now read-only.

SwiftMailer not send mail with Google App account #106

Closed
frasodel opened this issue Jun 19, 2015 · 6 comments
Closed

SwiftMailer not send mail with Google App account #106

frasodel opened this issue Jun 19, 2015 · 6 comments

Comments

@frasodel
Copy link

I'm using Symfony and I'm trying to send an email with swiftmailer. I have an account set up with google app ([email protected]) and when I try to send shows me the following error:

Failed to authenticate on SMTP server with username "[email protected]" using 2 possible authenticators

I tried it with my personal gmail account and send the message correctly with the same settings. Just change the username and password.

# Swiftmailer Configuration
swiftmailer:
    transport: smtp
    host:      smtp.gmail.com
    username:  [email protected]   #[email protected]
    password:  google_app_password       #gmail_password
    auth_mode: login
    port:      587
    encryption: tls

I've been searching the internet and I've found this page from Google, where I can change the access to less secure applications. https://www.google.com/settings/security/lesssecureapps

In my gmail account this option is active, but if I turn shows me the same error when sending email. I've been trying to change this setting in google app account but it tells me that this option is not available for google app.

Any idea to send emails from this account by google app? I have to set something else?

@sstok
Copy link

sstok commented Jun 21, 2015

It seems Gmail prefers OAuth, Swiftmailer supports this authentication mech but its not really documented.

https://developers.google.com/gmail/xoauth2_protocol
https://github.com/swiftmailer/swiftmailer/blob/5.x/lib/classes/Swift/Transport/Esmtp/Auth/XOAuth2Authenticator.php

@frasodel
Copy link
Author

Hello @sstok ,

I have modified the parameters of symfony swiftmailer to change the auth_mode. I have changed the following code, but I get an error in the kernel application. What auth_mode should I use ?

# Swiftmailer Configuration
swiftmailer:
    transport: smtp
    host:      smtp.gmail.com
    username:  [email protected]   #[email protected]
    password:  google_app_password       #gmail_password
    auth_mode: oauth
    port:      587
    encryption: tls

This is the error message:

InvalidConfigurationException in BaseNode.php line 313:
Invalid configuration for path "swiftmailer.mailers.default.auth_mode": The "OAuth" authentication mode is not supported

@frasodel
Copy link
Author

I have also tried to change it using this code but it tells me that this method is not available

// switch to new settings
        $transport      = $this->get('mailer')->getTransport();
        $ext            = $transport->getExtensionHandlers();
        $auth_handler   = $ext[0];
        $auth_handler->setUserName(EMAIL);
        $auth_handler->setPassword(PASSWORD);
        $auth_handler->setAuthMode('XOAUTH2');

wouterj added a commit to symfony/symfony-docs that referenced this issue Feb 6, 2016
…reguiluz)

This PR was squashed before being merged into the 2.3 branch (closes #5856).

Discussion
----------

Reworded the "How to use Gmail" cookbook article

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets | -

In StackOverflow some people are [having issues](http://stackoverflow.com/questions/33226583/swiftmailer-doesnt-work/33240538#33240538) with the `encryption` parameter of Gmail. And others are [having issues](symfony/swiftmailer-bundle#106) with Gmail + SwiftMailerBundle.

That's why I propose to reword this article and explain some things with more detail.

Commits
-------

b113f72 Reworded the "How to use Gmail" cookbook article
@Aerendir
Copy link

I'm having the same exact problem. Reading this I don't understand how to solve the problem and also the reworded documentation is not much clear. There is told

If your application uses tls encryption or oauth authentication, you must override the default options by defining the encryption and auth_mode parameters.

but using as settings

disable_delivery:  false
transport: gmail
username:  "%mailer_user%"
password:  "%mailer_password%"
encryption: ssl
auth_mode: oauth

I receive the following exception:

Invalid configuration for path "swiftmailer.mailers.default.auth_mode": The "oauth" authentication mode is not supported

So, how can I use SwiftMailer with a Google Apps set email?

@frasodel
Copy link
Author

frasodel commented Jul 25, 2016

I could make it work with the following code:

swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    auth_mode: "%mailer_auth_mode%"
    port:      "%mailer_port%"
    encryption: "%mailer_encryption%"

and the following parameters:

    mailer_transport: smtp
    mailer_host: smtp.gmail.com
    mailer_user: ******@*******
    mailer_password: ********
    mailer_auth_mode: login
    mailer_port: 587
    mailer_encryption: tls

I do not remember if I had to touch something in connection permissions of the account. But as I have a business account would not let me set some parameters.

@Aerendir
Copy link

Thankyou for the reply @frasodel ... Anyway in the meantime I've moved to AWS SES to send e-mails, so I solved the problem at root :D

@fabpot fabpot closed this as completed Sep 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants