Sending email via gmail via python app
as indicated in the title. I am having problem sending email through my gmail account via python app. I searched the internet for a solution but nothing seems to be resolved and I thought I could ask here. My code is as follows:
FROMADDR = "firstname.lastname@example.org" LOGIN = FROMADDR PASSWORD = "mypass" TOADDRS = "email@example.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 either. I've tried hotmail as well, but it causes the same problem. I'm using Python 2.7 (don't ask :)) on a windows 7 machine developing on Eclipse with PyDev.
Any help would be great! Thank you in advance.
source to share
I am using the same design on one of my servers. My code is below.
The only difference is the extra
after '.starttls ()'. This shouldn't be a problem; from 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 drop the connection if the client doesn't send
, but Google may be more restrictive on their SMTP server. I would check this first. (I've seen ISPs tighten up conditions of this kind to reduce spam, such as Mailinator 2007 write.)
It can also be a filtered port - try running the code in the REPL and confirm which line is the exception, if so
, you will find out on the net. If it does, your use is probably
Note I also experienced random unclean shutdowns, resulting in tried / excluded
import smtplib s = smtplib.SMTP() s.connect("smtp.gmail.com") s.ehlo() s.starttls() s.ehlo() s.login("firstname.lastname@example.org", "frompass") s.sendmail("email@example.com", toAddr, bytes) try: s.close() except: pass
source to share