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="my.user.account@gmail.com" 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 35882577n8BITMIMEnSTARTTLSnENHANCEDSTATUSCODESnPIPELININGnCHUNKINGnSMTPUTF8′) (220, b’2.0.0 Ready to start TLS’) (250, b’smtp.gmail.com at your service, [SERVERIP]nSIZE 35882577n8BITMIMEnAUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTHnENHANCEDSTATUSCODESnPIPELININGnCHUNKINGnSMTPUTF8′) Traceback (most recent call last): File “<pyshell#52>”, line 6, in server.login(username,password) File “C:PythonPython36libsmtplib.py”, line 729, in login raise last_exception File “C:PythonPython36libsmtplib.py”, line 720, in login initial_response_ok=initial_response_ok) File “C:PythonPython36libsmtplib.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=AKgnsbu5n5.7.14 wJL_YP3XLTGPzP_UBwtyCHhzHE1y1G8R0iHaz_pxG9fBH4CePmbXuAE1qH8HvnXTgqcmKin5.7.14 P23_lJoZX_pfbNUOxQp4Fr2VhgbAjnlu3ZL_pcjVBd-TTMUVCAah6Q2-Vq5Dffm9s4UWwln5.7.14 J7V7CjE06eHkw1IlphV4lLbRKVp9Hk7vC92zLQ5zM27cbQiTM8W3lEgm> Please logn5.7.14 in via your web browser and then try again.n5.7.14 Learn more atn5.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.
Advertisement
Answer
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.