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