Can't send mail : smtplib.SMTPAuthenticationError - python

def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = ['dhe*****#***.com']
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
if __name__ == '__main__':
send_email('abc#gmail.com', 'password', '', 'Hello', 'hello')
I am using this script to send email from abc#gmail.com. (hardcoded the recipient for now)
But I am getting
smtplib.SMTPAuthenticationError: (534, '5.7.14 https://accounts.google.com/signin/ .....
Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 dl17sm699785obc.9 - gsmtp')
Access for Less secure apps is already turned on for that account (abc#gmail.com).
There is also NO 2-factor authentication for that account.
I can't login through a web-browser on that machine because it is an EC2 Instance. How do I proceed here?

Related

send email with Gmail Python

I am attempting to send an email, but I run into this error:
smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor d2sm13023190qkl.98 - gsmtp')
In the web URL i dont see anything super useful, would anyone have any tips? For SO purposes I left the email account passwords as test versus sharing my person info..
import smtplib
import ssl
# User configuration
sender_email = 'test#gmail.com'
receiver_email = 'test#gmail.com'
password = 'test'
# Email text
email_body = '''
This is a test email sent by Python. Isn't that cool?
'''
# Creating a SMTP session | use 587 with TLS, 465 SSL and 25
server = smtplib.SMTP('smtp.gmail.com', 587)
# Encrypts the email
context = ssl.create_default_context()
server.starttls(context=context)
# We log in into our Google account
server.login(sender_email, password)
# Sending email from sender, to receiver with the email body
server.sendmail(sender_email, receiver_email, email_body)
print('Email sent!')
print('Closing the server...')
server.quit()
I tried my best... I think this should work!
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
email = "test#gmail.com" # the email where you sent the email
password = "yourPassword"
send_to_email = "yourEmail#gmail.com" # for whom
subject = "Gmail"
message = "This is a test email sent by Python. Isn't that cool?!"
msg = MIMEMultipart()
msg["From"] = email
msg["To"] = send_to_email
msg["Subject"] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()
You must allow the "Less secure apps" in Google configurations.
Here is the link of another thread about it : Link

Gmail SMTP authentication always fail

I created this software in python 3
import smtplib
TO = 'anywhere#mail.com'
SUBJECT = 'Text subject of the mail'
TEXT = 'Text of the mail'
gmail_sender = 'yourMail#gmail.com'
gmail_passwd = 'password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)
BODY = '\r\n'.join(['To: %s' % TO,
'From: %s' % gmail_sender,
'Subject: %s' % SUBJECT,
'', TEXT])
try:
server.sendmail(gmail_sender, [TO], BODY)
print ('email sent')
except:
print ('error sending mail')
server.quit()
The first day the software worked well but now I always have an authentification error from gmail :
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials o81-v6sm9180362wmo.38 - gsmtp')
What I did already is :
I desactivated the unlock captchar from this link :
https://accounts.google.com/DisplayUnlockCaptcha
and I have enabled the less secure apps from this link:
https://www.google.com/settings/security/lesssecureapps
But still the same, I can't send an e-mail anymore. It doesn't seems that there's a Python software problem because I wrote the same software in Go and I still receive the same error.
It’s solved. My login was wrong.

Sending email incl. Subject field ,using Python SMTP

I used server.sendmail(fromaddr, toaddrs, msg) to send automated emails using python.
How can I add an email subject to the mail?
I tried to look in SMTP documentation and didn't find any documentation for that.
From How to send an email with Gmail as provider using Python? :
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"

Gmail SMTP graceful login

I'm using Gmail SMTP to send emails in Python, however sometimes the application may stay idle for an extended period of time.
How do I make sure that the session hasn't expired? Here's my code:
def send_email(user, pwd, recipient, subject, body):
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
Can I always reuse the server session, or can it expire after let's say 5 hours?

Sending email using smtplib doesn't work anymore

So yesterday I had this bit of code written out and it worked perfectly fine, but today it's not sending e-mails anymore. Can someone explain why?
import smtplib
SERVER = 'owa.server.com'
FROM = 'noreply#server.com'
TO = ['person#gmail.com', '1112223344#vtext.com']
name = 'Mr. Man'
SUBJECT = 'Recent Information for: %s' % (name)
TEXT = "Dear " +name+ ",\n\nHello.\n\nSincerely,\nOur Guys Here"
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER, 587)
server.ehlo()
server.starttls()
server.ehlo
server.login('noreply#server.com', 'password')
server.sendmail(FROM, TO, message)
server.quit()
This code is a working snippet. I wasn't getting the e-mails in my personal gmail account because gmail was sending it to the spam folder. I checked to see if it works at my office account, and it did just fine.
import smtplib
# Specifying the from and to addresses
fromaddr = 'fromuser#gmail.com'
toaddrs = 'to#gmail.com'
# Writing the message (this message will appear in the email)
msg = 'Enter you message here'
# Gmail Login
username = 'username'
password = 'password'
# Sending the mail
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Above standard smtp send works with gmail,
thus it must be your server(whatever you're using) configuration that is at fault.

Categories