[Tutor] How to send an email using Python

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

 Hey Stefan,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 smtplib import base64smtpserver = 'smtp.gmail.com' AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1 smtpuser = 'user at gmail.com'# for SMTP AUTH, set SMTP username here smtppass = '*****'# 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 u
 sing a manual plain login method smtp.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, in session = 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 getreplyraise SMTPServerDisconnected("Connection unexpectedly closed")smtplib.SMTPServerDisconnected: Connection unexpectedly closedTool completed with e
 xit code 1 Thanks Avinash Original message From:Stefan Lesicnik< stefan at lsd.co.za >Date: 11 Mar 09 12:12:45Subject:Re: [Tutor] How to send an email using PythonTo: Senthil Kumaran
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090311/28e3d642/attachment.htm>

More information about the Tutor mailing list