26

I'm seeking some help logging into a gmail account and downloading some emails using a python script. I'm trying to adapt an approach found here, but I'm running into a problem with step 1, accessing the account via imap.

here is the code I'm starting with:

import email
import imaplib

m = imaplib.IMAP4_SSL("imap.gmail.com",993)
rc, resp = m.login('myemailaddress','mypassword')

I get the following error:

Traceback (most recent call last):
  File "email.py", line 1, in <module>
    import email, imaplib
  File "/home/will/wd/email.py", line 14, in <module>
     m.login('myemailaddress','mypassword')
  File "/usr/lib/python3.4/imaplib.py", line 538, in login
    raise self.error(dat[-1])
 imaplib.error: b'[ALERT] Please log in via your web browser: http://support.google.com/mail/accounts/bin/answer.py?answer=78754 (Failure)'

Imap is indeed enabled in gmail settings. I have looked at the instruction on the google support link and questions regarding this error in similar situations, such as here and here, but my situation is different from the first because
1) it never worked to start with, and
2) I'm not running it so frequently to get blocked. Its also different from the second example because this is a normal gmail account, not a custom domain with a google apps account.
Using the https://accounts.google.com/DisplayUnlockCaptcha to try and allow access doesn't work for me either.

The only thing that allows login to work is to change my google accounts security settings to "allow access for less secure apps".

My question is: how can I fix my code (or my setup more generally) to allow me to login, without relaxing my accounts security settings? Is there some way to meet security requirements using libimap?

5
  • 2
    Did you enable IMAP in your Gmail settings? Commented Aug 20, 2014 at 20:17
  • I did indeed, I'll edit to say so Commented Aug 20, 2014 at 20:23
  • 1
    You need to use "less secure setting", or integrate support for googles oauth2 authentication system. Commented Sep 30, 2014 at 23:22
  • I ran the same code and it executed without error. I suspect this has something to do with your settings or credentials Commented Sep 30, 2014 at 23:27
  • 1
    Possible duplicate of How can I avoid google mail server asking me to log in via browser? Commented Apr 15, 2016 at 10:54

5 Answers 5

23

You can try to turn on this: https://www.google.com/settings/security/lesssecureapps This action solved the same problem for me.

Sign up to request clarification or add additional context in comments.

3 Comments

Try to include pertinent details from the links you provide in case the link dies in the future.
@AlexOls This does work for me. But, I noted in my question that I did this, and specifically asked if there was a way to get imaplib working without changing my account security settings, so I can't mark this as correct.
This does not work for me if I run my tests using Docker with Ubuntu Linux. But locally it's working. Any other thoughts?
17

If you want to avoid this error without compromising your account's security, use OAuth to authenticate. The protocol is documented here, and there is Python sample code that shows the use of XOAUTH2 with imaplib.

Independent of this, you should consider enabling two-step verification on your account to make it more secure. If you do, you can use an App Password to connect to IMAP, which might also avoid the above warning.

1 Comment

Long overdue acceptance - barely any time to work on this! I can succesfully log in using OAUTH2 via rauth while still disallowing 'less secure apps'. Haven't worked out the code to actually read the emails using this access method, but the log-in works.
3

With the new gmail´s update, some mail servr or apps get blocked due to the new gmails security settings. To solve this, I went to https://www.google.com/settings/security page and 'enabled' Access for less secure apps.

Comments

2

There are mainly 3 reasons why this error occurs:

  1. Internet Connection Issue
  2. Incorrect Login Credentials
  3. myaccount.google.com/lesssecureapps is turned off

Comments

0

I was able to solve this by using a text-mode browser (elinks) to verify my login from the remote server. I had already struggled a while, so I had already tried enabling unsafe apps and various other incantations.

After logging in to gmail.com in elinks (using the html-only interface and having a security code sent to my phone) I could use imaplib to access the gmail account. Presumably one must do web-authentication from the same IP one tries to use Python/imaplib from.

It's probably Better™ to use the OAuth protocol, but this way I got my script running again without rewriting it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.