86

when i try to send mail using gmail and python error occurred this type of question are already in this site but doesn't help to me

gmail_user = "me@gmail.com"
gmail_pwd = "password"
TO = 'friend@gmail.com'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
        'From: %s' % gmail_user,
        'Subject: %s' % SUBJECT,
        '', TEXT])

server.sendmail(gmail_user, [TO], BODY)
print ('email sent')

error:

    server.login(gmail_user, gmail_pwd)
    File "/usr/lib/python3.4/smtplib.py", line 639, in login
   raise SMTPAuthenticationError(code, resp)
   smtplib.SMTPAuthenticationError: (534, b'5.7.14   
   <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtl1\n5.7.14       Li2yir27TqbRfvc02CzPqZoCqope_OQbulDzFqL-msIfsxObCTQ7TpWnbxIoAaQoPuL9ge\n5.7.14 BUgbiOqhTEPqJfb02d_L6rrdduHSxv26s_Ztg_JYYavkrqgs85IT1xZYwtbWIRE8OIvQKf\n5.7.14 xxtT7ENlZTS0Xyqnc1u4_MOrBVW8pgyNyeEgKKnKNyxce76JrsdnE1JgSQzr3pr47bL-kC\n5.7.14 XifnWXg> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fl15sm17237099pdb.92 - gsmtp')    
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
mans
  • 1,043
  • 1
  • 8
  • 7
  • 1
    Look like I can't add a new answer to this question, none of the below worked for me. After contacting google this was the solution https://stackoverflow.com/a/56809076/1225755 – Ryan King Jun 28 '19 at 14:56

3 Answers3

147

Your code looks correct. Try logging in through your browser and if you are able to access your account come back and try your code again. Just make sure that you have typed your username and password correct

EDIT: Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:

Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps

aaveg
  • 1,864
  • 2
  • 16
  • 15
  • 1
    username and password are working fine in my browser – mans Nov 10 '14 at 20:57
  • Since its an authentication error. possible reasons could be: 1) Incorrect username password(capital letters should be taken care of) 2)Too many unsuccessful attempts therefore, you need to fill the captcha. That can be done by by the browser 3) Not too sure about this but in some cases, slow internet connection – aaveg Nov 10 '14 at 21:01
  • 1
    if you have some other gmail id, try that – aaveg Nov 10 '14 at 21:02
  • tried another id but same error – mans Nov 10 '14 at 21:04
  • 15
    Make sure you click turn on, and not turn off! I lost two hours of time because of this simple error :) – Kehlin Swain Jul 12 '15 at 06:12
  • 3
    Hi! I tried going to this setting in my google business account, but it says the permission lies with administrator, even though I'm the administrator! – Anant Gupta Jan 01 '16 at 14:03
  • 5
    @AnantGupta `Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.` you have to enable this in the admin console first and after a while you will be abler to allow `lesssecureapps` – psychok7 May 01 '16 at 15:45
  • 4
    Turn on this solve the prob. Thanks. But is there any change in the code to make it securer so google can accept? – muyong Mar 31 '17 at 16:51
  • 4
    @aaveg Is there a way to check in the Python console whether it is invalid credentials or a need to enable less secure apps? – anonymous Dec 11 '17 at 22:56
  • 2
    If it helps anyone else, I got auth error even after turning on less secure apps and displayunlockcaptcha. After a while of figuring out, I saw that less secure apps settings in google was turning off on its own! I tried turning it on a couple of times and it worked then. – Anupam Dec 18 '17 at 14:50
  • @KehlinSwain I cant believe I made that mistake. Thanks so much! – Anupam Apr 27 '18 at 06:44
  • @psychok7 where do I enable "you have to enable this in the admin console first and after a while you will be abler to allow lesssecureapps" where is that admin console switch? – sogu Mar 17 '20 at 21:14
  • 4
    Google has deprecated this feature from 30th May, 2022 – Aman Sharma Jun 12 '22 at 07:26
52

Your code looks correct but sometimes google blocks an IP when you try to send a email from an unusual location. You can try to unblock it by visiting https://accounts.google.com/DisplayUnlockCaptcha from the IP and following the prompts.

Reference: https://support.google.com/accounts/answer/6009563

Hamish
  • 2,763
  • 1
  • 17
  • 21
taz
  • 529
  • 4
  • 2
  • Please describe some points of the solution, since the link is valid now but could be unreachable later – abarisone Mar 18 '15 at 15:15
  • 1
    If it helps anyone else, I got auth error even after turning on less secure apps and displayunlockcaptcha. After a while of figuring out, I saw that less secure apps settings in google was turning off on its own! I tried turning it on a couple of times and it worked then. – Anupam Dec 18 '17 at 14:51
  • 1
    When you turn on this feature, generally, it reflects in an hour but sometimes it takes 24 hrs to get reflected. Its mentioned in their console. – Nilesh Jan 03 '18 at 10:02
  • Thanks, I try to run my script on my vps(a ip I never logged in with before) and this link works for me to sign in on new ip. – hsc Feb 04 '18 at 05:17
  • 1
    Even after I turned on lesssecureapps, it still complained error. Your sulution comes my rescue, thanks very much. – Bicheng Apr 13 '18 at 13:44
  • thankyou so much, this was not shown in any guide or anything anywhere ty sm – steff_bdh Jun 08 '19 at 19:21
  • It worked for me, since I was trying to use my account from the cloud. – Cainã Max Couto-Silva Nov 17 '20 at 03:25
5

I have just sent an email with gmail through Python. Try to use smtplib.SMTP_SSL to make the connection. Also, you may try to change the gmail domain and port.

So, you may get a chance with:

server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(gmail_user, password)
server.sendmail(gmail_user, TO, BODY)

As a plus, you could check the email builtin module. In this way, you can improve the readability of you your code and handle emails headers easily.

Pablo
  • 1,311
  • 9
  • 20
  • 4
    This failed to solve the problem. – ABCD Jan 12 '18 at 16:29
  • 1
    @SmallChess this is a pretty old answer. Certainly google already change its auth method and maybe Python already has something done to deal with it. Check aaveg answer which looks to work. – Pablo Jan 12 '18 at 16:56
  • 1
    2020 and works very well. As server you could also use smtp.gmail.com – Davide Mar 16 '20 at 00:20