Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMTPAuthenticationError when sending mail using gmail and python [duplicate]

Tags:

python

smtplib

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 = "[email protected]" gmail_pwd = "password" TO = '[email protected]' 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')     
like image 769
mans Avatar asked Nov 10 '14 20:11

mans


People also ask

How do I fix SMTP authentication error in Python?

Ensure that you're trying to connect to the correct SMTP host on the correct SSL or TLS port (as per the host's guidelines). Verify that you've provided the correct username/password. In rare cases, if you've not installed Python correctly, the mail classes may not get initialized correctly.

How do you send an email to multiple recipients in Python?

To send email to multiple recipients using Python smtplib, we can use the sendmail method. We create the SMTP instance by using the SMTP server address as the argument. Then we create the message with the MIMEText class. We combine the recipients into a string with join .


2 Answers

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

like image 196
aaveg Avatar answered Sep 24 '22 21:09

aaveg


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

like image 29
taz Avatar answered Sep 20 '22 21:09

taz