45

I am trying to send emails from Django using an email configured by Google Apps, my configuration at the settings.py file looks something like this:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'contact@mydomain.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

When I try to send an email using:

from django.core.mail import send_mail

send_mail("Happy new year", "We wish you the best for 3001",
    "contact@mydomain.com", ["someuser@gmail.com"])

I get the following error:

SMTPAuthenticationError: 
(535, '5.7.1 Please log in with your web browser and then try again. 
 Learn more at
 5.7.1 https://support.google.com/mail/bin/answer.py?answer=78754 k2sm758604obl.14')

Since I'm working on a remote server with no graphical user interface, I cannot even try to login from the browser.

ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 1
    I'm using exactly the same settings (except for the user/password of course) and it works for me. Are you using a standard Gmail address, or is it a Google apps account with your own domain name? That's the only thing that comes to my mind that could cause this problem. And also make sure that your credentials are correct. That's what the Google help page behind the link tells you to do. – Danilo Bargen Apr 04 '12 at 15:35
  • I'm using a Google apps account with my own domain name. It actually worked on my home computer after a few attempts (and login in using the browser). But I can't make it work from the remote server. – Juan Enrique Muñoz Zolotoochin Apr 04 '12 at 15:51
  • Have you follow the three google checks posted in your google support link? (UnlockCaptcha) – dani herrera Apr 04 '12 at 16:58
  • 1
    When I visit those links from my home computer, they just redirect me to the email account. It seems that I need to visit them from the machine that is having the issue, but I don't have access to a graphical interface on that machine. – Juan Enrique Muñoz Zolotoochin Apr 04 '12 at 17:08
  • For anyone encountering problems configuring the required HTTP/S tunnel: http://superuser.com/questions/490632/tunneling-https-traffic-via-a-putty-ssl-tunnel-with-socks – ripper234 Oct 21 '12 at 14:13
  • Actual solution http://stackoverflow.com/a/43465118/742173 – Abhishek Gupta Apr 18 '17 at 06:31

16 Answers16

63

Just go to https://accounts.google.com/DisplayUnlockCaptcha and click "continue". This is going to allow access from other servers.

ballade4op52
  • 2,142
  • 5
  • 27
  • 42
Hasan Veli Soyalan
  • 2,428
  • 20
  • 24
32

I've been messing with this for a couple of hours within a cucumber/capybara/selenium test - discovered something stupid which will fix this error for good, guaranteed

The all too familiar error:

Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) (Net::IMAP::NoResponseError)

As it turns out, there are TWO "Allow Less Secure Apps" toggles which need to be changed to allow logins from unknown devices/IMAP.

One here: https://myaccount.google.com/security?pli=1#connectedapps (bottom of the page)

And one here: https://www.google.com/settings/security/lesssecureapps

BOTH OF THESE GODFORSAKEN TOGGLES need to be changed to get rid of this error message.

edit: from user Milothicus (https://stackoverflow.com/users/3538026/milothicus): in myaccount.google.com, under 'Sign-in & Security', select 'Connected apps & sites'. this also has an option to 'Allow less secure apps'. after turning this one on, my server could now send me an automated email.

Community
  • 1
  • 1
etusm
  • 4,082
  • 1
  • 30
  • 26
  • This was the only one that worked for me when trying to send email from a Perl script on Ubuntu 16.04. – Håkon Hægland Sep 02 '16 at 22:00
  • Wow! Usually Google does good UI, but two identically named toggles... what a screw up! Thanks for the answer. – mpb Dec 25 '17 at 03:18
14

When I tried to access my account I was sent this email consisting of this link. https://www.google.com/settings/security/lesssecureapps

You can turn-on then possibly turn-off after you've done testing.

jasxir
  • 808
  • 9
  • 18
7

I got the following response from Google Apps support:

You need to turn on your Outbound relay. To do this:

  1. Log into your account at google.com/a/yourdomain.com
  2. Click the Settings tab and then select Email in the left column.
  3. In the Outbound relay section, select Allow users to send mail through an external SMTP when configuring a "from" address hosted outside your domain.
  4. Click Save changes.

They also provided a help link: http://support.google.com/a/bin/answer.py?hl=en&answer=176054

After turning on Outbound relay and using the proxy to login to the webmail one more time (thanks to @DaniloBargen and @joshcartme) the issue was resolved. I've read the link explaining what the Outbound relay is and I'm not really sure why would I need it (I don't believe I'm using an external SMTP server).

Since I'm not really sure this is what solved the issue I won't mark the response as accepted until I get some confirmation.

  • This issue started happening to us today, and we have the correct setting checked in Outbound Relay. – ripper234 Jun 14 '12 at 07:54
  • 5
    Since you last updated your answer I think the process changed. I had the same error and what I did was log in to my account via the browser, then go to https://accounts.google.com/DisplayUnlockCaptcha (which I accessed from [here](https://support.google.com/mail/answer/14257?hl=en)) and then send an email via Django. Now the client is recognised and I can continue sending mails via that address in Django. – Bentley4 Dec 05 '13 at 15:08
5

Set up an ssh tunnel to the server in question so that you could, from your home computer, log in to the gmail web client using the server's IP. You probably need to tunnel port 80 and port 443, maybe just 443. After logging in through the web client the problem should go away according to knowledge base article listed in the SMTPAuthenticationError.

Here's an example of how to set up the tunnel: http://www.noah.org/wiki/SSH_tunnel#simple_port_forwarding_.28SSH_tunneling.29

joshcartme
  • 2,717
  • 1
  • 22
  • 34
  • 7
    An easier way to connect to the other network using tunnels is to use a dynamic tunnel (`ssh -D 6789 remotehost`) and then to set `localhost:6789` as SOCKS5-Proxy in Firefox. Then you are basically in the remote network with your browser and localhost is the remote host. – Danilo Bargen Apr 04 '12 at 20:43
  • I managed to set up the tunnel using @DaniloBargen's method (thanks!). I can actually log in to the email account using the browser but the problem persists. What's really weird is that I tried one of the suggestions of the above link, went to https://www.google.com/a/yourdomain.com/UnlockCaptcha (changing the domain, of course). But none of the username/password combinations worked. I've sent an email to Google Apps support about this. – Juan Enrique Muñoz Zolotoochin Apr 05 '12 at 15:15
  • I had marked this answer as accepted because, while I'm not exactly sure how I resolved the issue, this answer was a definitely helpful and necessary. It just got unmarked, so I guess someone else knows better. Maybe I should have marked my own response where I explained the situation as accepted? – Juan Enrique Muñoz Zolotoochin Apr 20 '12 at 02:30
  • The steps are: setup proxy using @DaniloBargen method, clear browser cache or open another browser, login in gmail. – Cesar Canassa Jul 27 '12 at 05:11
5

Option #1 (this worked for me):

After getting the error Please log in with your web browser and then try again. Learn more etc. when trying to send email from my web application, I logged in to the email via browser from my local computer.

After I logged in, there was a yellow notification bar on top which asking me if I want to allow external application access my mail. I confirmed this and Google asked me to log in to the account from the application within the next 10 mins. This will white-list the application.

Option #2:

If Option #1doesn't work for you, try this: http://www.rocketideas.com/2012/05/gmail-error-password-not-accepted-from-server-solved/

finspin
  • 4,021
  • 6
  • 38
  • 66
  • Option 1 worked for me, thanks. In my case it was a WordPress site using 'Configure SMTP' to send contact emails. Google flagged up the 'suspicious activity' when logging in via the browser, and offered me the opportunity to whitelist the ip/site. Thanks for posting! :) – crdunst Jun 05 '13 at 12:14
4

etusm provided two locations to turn on less secure apps:

One here: https://myaccount.google.com/security?pli=1#connectedapps (bottom of the page)

And one here: https://www.google.com/settings/security/lesssecureapps

both were turned on, but my headless server still couldn't send me an email. based on JohnPang's google+ recommendation, i found a third location where i had to allow access to less secure apps:

in myaccount.google.com, under 'Sign-in & Security', select 'Connected apps & sites'. this also has an option to 'Allow less secure apps'. after turning this one on, my server could now send me an automated email.

Milothicus
  • 123
  • 2
  • 7
2

I found the solution at: https://support.google.com/accounts/answer/185833?hl=en and finally https://security.google.com/settings/security/apppasswords

If you are testing your project on a local machine, you should go to the latter link, and enable "Access for less secure apps".

1man
  • 5,216
  • 7
  • 42
  • 56
1

Do you have two factor authentication enabled for the apps account ? Then you might need to use an application specific password for that application.

Christer B
  • 155
  • 1
  • 1
  • 7
1

All of the above doesn't help in my case (weird). But this link might help you:

https://security.google.com/settings/security/activity

You can access it via Google Plus

  1. Open Google+
  2. Select "Security" from the top
  3. Under "Recent activity" click "View all events"
  4. You will see a list of "Unusual Activity"
  5. It shows "Application / device sign-in attempt (prevented) Singapore" as I'm using AWS from Singapore
  6. Click on "Change" > "Yes, that was me!"
  7. Retry again. Done!
John Pang
  • 2,403
  • 25
  • 25
0

As of now (look at my post date) there is only one "Allow less secure apps" toggle in the Gmail account admin UI: https://myaccount.google.com/u/0/security#connectedapps

It'll work from your local computer (Mac or PC) after that.

To allow access from Amazon EC2 (and I suspect other Cloud-located hosts), there is yet another flag to set in Google's never ending battle with spammers: https://accounts.google.com/b/0/DisplayUnlockCaptcha

Alex Rogachevsky
  • 514
  • 1
  • 3
  • 14
0

Recently, I have found that this issue can be resolved by confirming that the activity has originated from a request I initiated, by visiting Google Account

I had to confirm, under Security Events, that the suspicious activity was in-fact me, even though the originating server from where the request came from was cloud hosted, and therefore over 1000 km away. After clicking this step, and setting less secure apps, I was able to use getmail to retrieve my mail, over ssl using either imap or pop.

Victor Romeo
  • 396
  • 3
  • 11
0

Just want to highlight Danilo Bargen's comment:

An easier way to connect to the other network using tunnels is to use a dynamic tunnel (ssh -D 6789 remotehost) and then to set localhost:6789 as SOCKS5-Proxy in Firefox. Then you are basically in the remote network with your browser and localhost is the remote host

Also I want to add that SOCKS Proxy method also works with Chrome. As a result you can log in with your local web browser as if you are on a server.

TitanFighter
  • 4,582
  • 3
  • 45
  • 73
-1

change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.

steineron
  • 389
  • 3
  • 6
-1

This is occuring due to some apps are marked as less secure apps by google. So to use those apps, you need to give access for those apps. to do that follow http://www.codematrics.com/your-imap-server-wants-to-alert-you-to-the-following-please-login-via-your-web-browser-gmail/

Hope this will solve your issue.

Miral Viroja
  • 333
  • 2
  • 10
-3

In my case, when i tried to login to the google account via web, it asked me for a captcha. I entered the captcha and then the automation worked.

Pratik Khadloya
  • 12,509
  • 11
  • 81
  • 106