[Tutor] How to send an email using Python

Samuel Avinash samuelavinash at in.com
Wed Mar 11 17:36:06 CET 2009


 Hey Senthil,Thanks for your response. My script is similar to yours, but as you've pointed that it could be temporary. I've been retrying to connect and send email but in vain.Here's the snippet of my code. Can you debug and help me what could really have gone wrong.import smtplibimport base64smtpserver = 'smtp.gmail.com'AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1smtpuser = 'user at gmail.com'# for SMTP AUTH, set SMTP username heresmtppass = '*****'# for SMTP AUTH, set SMTP password hereRECIPIENTS = ['someuser at gmail.com']SENDER = 'someuser at gmail.com'mssg = open('mssg.txt', 'r').read() # I am reading from this file on the same directorysession = smtplib.SMTP(smtpserver,'465')session.ehlo()#session.esmtpfeatures["auth"] = "LOGIN PLAIN"session.connect(smtpserver,'465')session.ehlo()session.starttls()session.setdebuglevel(1)session.helo()if AUTHREQUIRED:try:session.login(smtpuser, smtppass)except SMTPAuthenticationError, e:# if login fails, try again using a manual pla
 in login methodsmtp.docmd("AUTH LOGIN", base64.b64encode( smtpuser ))smtp.docmd(base64.b64encode( smtppass ), "")smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)if smtpresult:errstr = ""for recip in smtpresult.keys():errstr = """Could not delivery mail to: %sServer said: %s%s%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)raise smtplib.SMTPException, errstr This is the Stack Trace I got when I ran the above script after a very long time(>15min)Traceback (most recent call last): File "C:\Python26\Mail.py", line 13, insession = smtplib.SMTP(smtpserver,'465') File "C:\Python26\lib\smtplib.py", line 239, in init (code, msg) = self.connect(host, port) File "C:\Python26\lib\smtplib.py", line 296, in connect (code, msg) = self.getreply() File "C:\Python26\lib\smtplib.py", line 340, in getreply raise SMTPServerDisconnected("Connection unexpectedly closed") smtplib.SMTPServerDisconnected: Connection unexpectedly closedTool completed with exit code 1ThanksAvi
 nash Original message From:Senthil Kumaran< orsenthil at gmail.com >Date: 11 Mar 09 09:58:53Subject:Re: [Tutor] How to send an email using PythonTo: Samuel Avinash Hello Samuel,When sending through smtp.gmail.com you might need to starttls() and do a login() before sending.>>> import smtplib >>> headers = "From: %s\r
To: %s\r
Subject: %s\r
\r
" %(from,to,subject) >>> message = headers + "Hello, How are you?" >>> mailserver = smtplib.SMTP('smtp.gmail.com') >>> mailserver.ehlo() >>> mailserver.starttls() >>> mailserver.login('username','password') >>> mailserver.sendmail(from,to,message)As I try it now, startls() was giving me a message " reply: '454 TLS not available due to temporary reason\r
'"  As the error message says, it could be temporary.Try it at your end and report if you succeed.Thanks, SenthilOn Tue, Mar 10, 2009 at 4:41 PM, Samuel Avinashwrote: > Hi, > Can anybody tell me how to send an email to a recipient using Python > I am trying to send an email using Gmail > I create an instance ofsmtplib and use : > x=smtplib.SMTP(sever,port) > and then > x.login(user,pwd) > I try to send the email using > x..sendmail(SENDER, RECIPIENTS, msg) > But I get the following error > Traceback (most recent call last): > File "C:UsersAvisDesktopMail.py", line 13, in> session = smtplib.SMTP(smtpserver,port) > File "C:Python26libsmtplib.py", line 239, in init > (code, msg) = self.connect(host, port) > File "C:Python26libsmtplib.py", line 295, in connect > self.sock = self.getsocket(host, port, self.timeout) > File "C:Python26libsmtplib.py", line 273, in getsocket > return socket.createconnection((port, host), timeout) > File "C:Python26libsocket.py", line 498, in createcon
 nection > for res in getaddrinfo(host, port, 0, SOCKSTREAM): > socket.gaierror: [Errno 11001] getaddrinfo failed > > This is in Windows Vista and I am trying to connect to "smtp.gmail.com" with > port 465 >  > Tutor maillistTutor at python.org > http://mail.python.org/mailman/listinfo/tutor > >Senthil 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090311/3f54d205/attachment.htm>


More information about the Tutor mailing list