Requests using smtplib module denied - python

I have been using the smtplib module in python to send text messages from a gmail account and my requests to connect to the server recently started being denied. My code isn't anything special:
import smtplib
server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( '<gmail_address>', '<gmail_password>’ )
server.sendmail( '<from>', '<number>#mms.att.net', '<msg>’ )
An error is thrown at the 4th line
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534,
and gmail notifies me that they have blocked a sign-in attempt from an app, because it didn't meet modern security requirements.
I'm hoping to find a work around that allows me to continue sending SMS messages using the smtplib module.

Google explain that you can lower your security settings but they don't rcommend it, and give precious little in the way of advice apart from that.

Related

Why is gmail rejecting my SMTP connection?

I set my gmail account to allow "Less secure apps access" and I use server.starttls() in smtplib in python to make sure the session is encrypted so why is google still periodically rejecting my login with "Please log in via your web browser and then try again. Learn more at https://support.google.com/mail/answer/78754 "
server = smtplib.SMTP(GMAIL_SERVER, GMAIL_PORT)
server.ehlo()
server.starttls()
server.login(GMAIL_USER, GMAIL_PASS)
server.sendmail(GMAIL_USER, [to_address], mime_msg.as_string())
server.close()
logger.info("Email successfully sent")
When using the Gmail SMTP from a programming a lot of times you will get an error with it rejecting your login. It wants you to use the browser using Xoauth2.
A alternative would be to try and use an apps password infested of your true password. In my experience this tends to fix the issue, its often the case when the user has 2fa enabled.

SMTP Authentication Error had occured. How to resolve that?

Code is provided below:
import smtplib
s=smtplib.SMTP('127.0.0.1',587)
s.starttls()
s.login("EMAIL_ADDRESS#gmail.com","PASSWORD")
messsage="congrats"
s.sendmail('EMAIL_ADDRESS#gmail.com','RECIPIENT_ADDRESS#gmail.com',message)
s.quit()
Error:
SMTPAuthenticationError:
(535, b'5.7.8 Username and Password not accepted.
The SMTP server should be smtp.gmail.com, not 127.0.0.1. 127.0.0.1 refers to localhost or the current server you are running on. So unless you happen to be running on Gmail's SMTP server, this will not work as you currently have it. Of course, you must also use a correct email/password combination.

Sending an email using Python

I'm trying to send myself an email using Python's smtplib module. Here's the code I'm trying to execute:
import smtplib
sender = 'manas.oid#gmail.com'
receivers = ['manas.oid#gmail.com']
message = """From: From Person <manas.oid#gmail.com>
To: To Person <manas.oid#gmail.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except smtplib.SMTPException:
print "Error: unable to send email"
However, I get a 'Error:unable to send email' message when I try to execute this script. What seems to be wrong with my script?
You did not login and you did not start the connection with smtpObj.starttls().
To add on to what #Malik says, below are the steps you need to perform before you'll be able to do anything with GMail (provided less secure apps can access your account, see below).
conn = SMTP('smtp.gmail.com',587)
conn.ehlo()
conn.starttls()
conn.ehlo()
conn.login(username,pwd)
conn.sendmail(username, emailID, message)
Note that after recent changes to GMail, you'll need to explicitly allow less secure apps to access your account. GMail would block your request to login until you enable it. Link to enable less secure apps to access: https://support.google.com/accounts/answer/6010255?hl=en
Gmail wouldn't let you send an unauthenticated email from an account.
Instead, use the Gmail API to send emails. Some useful links below:
Authentication: https://developers.google.com/gmail/api/auth/web-server
Email: https://developers.google.com/gmail/api/guides/sending

Sending an email via gmail through a python application

as indicated by the title I am having trouble sending an email via my gmail account through a python application.I have searched online for a solution but nothing seems to solve it and I thought I might ask here.
My code is the following:
FROMADDR = "myemail#gmail.com"
LOGIN = FROMADDR
PASSWORD = "mypass"
TOADDRS = "varis81#hotmail.com"
msg = "Test message"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
print "E-mail succesfully sent"
I get the message:
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I tried different ports but it doesn't work also.I also tried hotmail but it causes the same problem.I am using Python 2.7 (don't ask :) ) on a Windows 7 machine developing on Eclipse using PyDev.
Any help would be great!
Thank you in advance.
I'm using the same construct on one of my servers. My code is below.
The only difference is the extra .ehlo() after '.starttls()`. This should not be the issue; from the RFC:
5.2 Result of the STARTTLS Command
The client SHOULD send an EHLO command as the first
command after a successful TLS negotiation.
According to the RFC, the server should not sever a connection if the client does not send ehlo after starttls, but Google could be more restrictive on their SMTP server. I'd check that first. (I've seen providers tighten down on these kinds of conditions to reduce spam, see Mailinator's 2007 writeup for instance.)
It could also be filtered ports - try running the code in the REPL and confirm which line is exceptioning, if it's the connect() you'll know it's network. If it's after, it's likely your usage of smtplib.
Of note, I also experienced occasional unclean shutdowns, resulting in the try/except around .close().
import smtplib
s = smtplib.SMTP()
s.connect("smtp.gmail.com")
s.ehlo()
s.starttls()
s.ehlo()
s.login("from#gmail.com", "frompass")
s.sendmail("fromname#gmail.com", toAddr, bytes)
try:
s.close()
except: pass
Well, since I cant post comments yet I'll have to attempt an answer..
Judging by this: Python SMTP Errno 10060
Perhaps a timeout would help?

How to handle leaving message on website in python

I am developing my first website. I have page where people can leave message to me. What I am doing is sending a AJAX call to the server, and the server gets the content of the message, then login in an gmail account and send the message to another gmail account using python's email libraries smtplib and email.
It works ok, but the problem is it takes more than one minute on my laptop, the loading image on the client side would keep spinning during the time. I think the bottleneck is login().
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
# login, send email, logout
mailServer.login(conf.user, conf.pw)
mailServer.sendmail(conf.user, to, msg.as_string())
mailServer.close()
So is there any way to speed this up, or other ways to do this.
Thanks
So the delay is probably caused by Google's SMTP server as they do a reverse lookup of the IP address of your webserver - mail systems often do this as part of sending e-mail.
What you have coded is only needed if you intended to send mail out from your account - that is when you need to perform authenticated SMTP. If you want to only mail your own account then you can remove the login step. Note, that you will have to change the mail server from smtp.gmail.com to one of Google's inbound mail (MX) servers. Also, if you are performing a simple forward you will have to handle e-mail rejection or other connection problems so having your script dump the mail to a simple mail server running on the mail server would be helpful.

Categories