13

I have a script which sends periodically reports to a list of recipients. Everything worked fine until today 4 am, when I checked my inbox and the reports didn't come.

By debugging the code:

import smtplib
username="[email protected]"
password="my.correct.password"

server=smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls() 
server.ehlo()
server.login(username,password)

#if login worked, it should send a message, but it is not working, so I will suppress this part

server.quit()

I receive the following (old known) result:

(250, b'smtp.gmail.com at your service, [SERVERIP]\nSIZE 35882577\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8') (220, b'2.0.0 Ready to start TLS') (250, b'smtp.gmail.com at your service, [SERVERIP]\nSIZE 35882577\n8BITMIME\nAUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\nENHANCEDSTATUSCODES\nPIPELINING\nCHUNKING\nSMTPUTF8') Traceback (most recent call last): File "<pyshell#52>", line 6, in server.login(username,password) File "C:\Python\Python36\lib\smtplib.py", line 729, in login raise last_exception File "C:\Python\Python36\lib\smtplib.py", line 720, in login initial_response_ok=initial_response_ok) File "C:\Python\Python36\lib\smtplib.py", line 641, in auth raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu5\n5.7.14 wJL_YP3XLTGPzP_UBwtyCHhzHE1y1G8R0iHaz_pxG9fBH4CePmbXuAE1qH8HvnXTgqcmKi\n5.7.14 P23_lJoZX_pfbNUOxQp4Fr2VhgbAjnlu3ZL_pcjVBd-TTMUVCAah6Q2-Vq5Dffm9s4UWwl\n5.7.14 J7V7CjE06eHkw1IlphV4lLbRKVp9Hk7vC92zLQ5zM27cbQiTM8W3lEgm> Please log\n5.7.14 in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 r131sm2964505vkr.40 - gsmtp')

Things I've tried

Allow less secure apps (as indicated in https://www.google.com/settings/security/lesssecureapps): This flag was set to true since I created the account.

Disable Captcha (as indicated in https://accounts.google.com/DisplayUnlockCaptcha): Visited this URL several times. No luck.

Login via browser: It works fine, when logging via any Internet Browser from any computer, although in the first times I had to insert a code sent to the account's recovery phone.

I came across other things I could try, but I am not sure it could work:

  • Specific app passwords
  • Oauth2
  • Change or reset password

I will probably try oauth2, however it would require a change in my code, which I want to avoid due to some restrictions in the server.

3
  • You said "I had to insert a code sent to the account's recovery phone". Does this mean that you have enabled 2FA? You might want to disable 2FA or - even better - use an app password: support.google.com/accounts/answer/185833?hl=en
    – Jonas
    Apr 12, 2020 at 11:22
  • Hello! I remember just confirming the recovery phone. I didn't set 2FA for the concerning account. I also remeber trying signing in and out repeatly countless times, but no success! That's why I decided to use the Gmail API. May 12, 2020 at 15:55
  • changing password fixed it for me. The password was old over 2 years and had been used multiple times before. Sep 10, 2020 at 12:37

10 Answers 10

17

I recently got this problem. I've been using the same script for 3+ years and it suddenly stopped working. The above solutions didn't work for me. After contacting google here's what solved the issue (note it can take a few hours for the changes to propagate - up to 24 hours but usually not that long)

  • ALLOW low secure apps in Google settings (no effect on own but may be related to the full solution)

  • Try the unlock captcha link: https://accounts.google.com/DisplayUnlockCaptcha (no effect on its own but may be related to the full solution)

  • Admin Console > Apps > G Suite > Gmail > User Settings > Confidential Mode --> DISABLE Confidential Mode

  • Admin Console > Apps > G Suite > Gmail > End User Access --> TURN ON "Allow per-user outbound gateways"

2
12

I had this issue as well. My mail forwarder script that has been working fine for 6+ months randomly started throwing this error:

534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbts\n5.7.14 y2ku2-8KrWsn_dAUhnkssuTuESwaeG-DNn9hWBpsSpJCmVL ZhH24jzJfd-5bHUdh993dJ7\n5.7.14 a5LsAOJrheyT0cRXIhMSbycqvMAC7sNrizO5qU_6iDescJcQa-QqUUCFVH7IZoWF9uXyrv\n5.7.14 HC9XzAW-pBwgUjV4i_ts8CNhB_yZf2JHOi-wl6gZ9jwAiYLWx DPe7epI> Please log\n5.7.14 in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 y5sm3059043 9pge.49 - gsmtp

After some research I tried all the following steps:

  • Checking allow low secure apps in Google settings (it was already on, tried turing it off and back on)
  • Trying the unlock captcha link: https://accounts.google.com/DisplayUnlockCaptcha
  • Singing in and out of Google account manually (verifying with phone number)
  • Changing the account password
  • Enabling and disabling 2FA
  • Countless other things...

Nothing seemed to actually work at first. However, after doing all the above steps, the script started working again ~24 hours later without any modifications.

3
  • Hello there! I have tested all these steps, it's been more than a week and the old script still doesn't seem to be working. Anyway, thank you for the answer, I have other accounts working with the same script, I'll be considering making these steps first before changing the script to Gmail API, as any change require a short time stop in production. Feb 21, 2019 at 21:25
  • Signing in, changing the settings, then signing out, then signing back in is what worked for me. That you for the tip to try signing in and out again.
    – Pro Q
    Mar 27, 2020 at 1:02
  • @ProQ I remember trying this same step. I even changed the password, allowed unknown applications, signing back in, verifying my phone number, and so on... Maybe in my case the usage of the account was so often (once every 20 minutes, or 700 times a day) that the login by a python script became blocked permanently. Anyway thanks for your comment Jun 6, 2020 at 16:29
9

I have solved this using the Google Gmail API. It seems Google has either blocked or limited logins for my account through unknown devices, as I could login only via the browser and after I confirmed the telephone number and an code sent by SMS.

So I decided to give up using smtplib and implemented the access using the API Google uses for Gmail: https://developers.google.com/api-client-library/python/ https://developers.google.com/gmail/api/guides/sending

Hope this helps other who had trouble with this issue.

9

The solution to this seems to be:

Doing this should unlock the account, though it can take up to an hour to take effect.

3

In my case, I was using the account from a server in the cloud which I was accessing via SSH. I have to make an SSH tunnel to access from my local browser to the corresponding Google URL as I was accessing it from the SSH terminal:

  1. Create an SSH Tunnel: ssh -D 9090 -C -N user@server-url
  2. Go to settings in Firefox, search proxy option, and set up the SOCK server to 127.0.0.1 and port to 9090:

Setting up the proxy

  1. Now you can check that your current IP is the same as the remote server in myip.com.
  2. Now access the Google URL to log in with the email account.
  3. In my case, it was necessary to access https://www.google.com/accounts/DisplayUnlockCaptcha too
2

If you have signed in with multiple accounts on the same device you would have to sign out from all your accounts and copy and paste this https://accounts.google.com/b/0/DisplayUnlockCaptcha in your url. You will be directed to your gmail sign in page.Choose the account you used in the script, enter your username and password. The link will direct you to the captcha page and click on continue. Make sure you don't close the tab after unlocking captcha and run your script. You don't have to always unlock captcha if this works for the first time.

1

Try changing the password of the email and re-run the script with the new password. This worked for me. You probably have received a Sign-in attempt email and make sure you verify the login your script made.

enter image description here

1
  • Hi! I didn't receive this message after trying to send email. But, yes I tried changing my password, no success, then changed it back. Also unchecked third party unknown applications to log in, no donut for me. In another recent case, when I received this message, answered Yes, it was me, however the application continued to be blocked. Thanks anyway for raising this option. Jun 6, 2020 at 16:25
1

Even i had this issue just try to do these steps because google initially blocks connection without any reason:

  1. try to turn on and off Less secure app access.

  2. log out to your account and login again

  3. Change your password and hit your script and at last print acknowledgment like below:

      print('Your message has been sent!!')
    

if this doesn't work please login after 24 hour this will definitely work!!

0

Just click "Yes, it was me", then turn off "Less secure app accses."

0

If you are signed in with multiple accounts on the same device you do not have to sign out of your accounts for performing these steps.

All you need to do is to open a private window, paste the following URL: https://accounts.google.com/b/0/DisplayUnlockCaptcha into your browser's private window. And you will be redirected to your Gmail sign-in page. put your account email in the given field and then press next and enter the password. The link will direct you to the captcha page and click on continue. Make sure you don't close the tab after unlocking the captcha and run your script. You don't have to always unlock captcha if this works for the first time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.