sending emails using python

Steve Holden steve at holdenweb.com
Thu Sep 7 04:50:23 EDT 2006


Sybren Stuvel wrote:
> sridhar enlightened us with:
> 
>>iam having user account on an exchangeserver.
>>with that can i send an email using python?
>>
>>if iam using the following code iam getting error
>>
>>
>>fromAddress = 'sridhar_kasturi at satyam.com'
>>toAddress = 'sridhar_kasturi at satyam.com'
>>msg = "Subject: Hello\n\nThis is the body of the message."
> 
>                        ^^^^
> 
> You need \r\n\r\n there. Line-ends in email messages should be DOS
> line-ends.
> 
This is untrue for the Python smtplib, though correct according to the 
RFCs. The SMTP.data() method uses a locally-declared function called 
quotedata() to ensure the correct line endings, so using "\n" will 
result in the same message as using "\r\n".
> 
>>Traceback (most recent call last):
>>  File
>>"C:\sridhar\Beginning_Python\Beginning_Python\Chapter16\tryitout\InitialMailExample.py",
>>line 5, in ?
>>    server = smtplib.SMTP("hstmsg002",25)
>>  File "C:\Python24\lib\smtplib.py", line 244, in __init__
>>    (code, msg) = self.connect(host, port)
>>  File "C:\Python24\lib\smtplib.py", line 307, in connect
>>    (code, msg) = self.getreply()
>>  File "C:\Python24\lib\smtplib.py", line 351, in getreply
>>    raise SMTPServerDisconnected("Connection unexpectedly closed")
>>SMTPServerDisconnected: Connection unexpectedly closed
> 
> 
> Well that's useless. You could install a network sniffer
> (http://www.ethereal.com/) and find out what's actually sent over the
> network connection, and where things go wrong.
> 
Useless it might be, but it's a lot more clueful than some questions we 
see, so it seems a little dismissive just to say "it's useless" - the OP 
has provided the traceback, which is so often missing in questions from 
newbies, and has correctly localized the error to a small chunk of code.

True, the error message isn't particularly helpful in these 
circumstances, but it probably represents an accurate description (from 
the client's point of view) about what's happening, in which case an 
Ethereal trace will provide little more data than the traceback. I 
suppose it'll tell you whether the server is sending RST or FIN to 
terminate the connection, but it won't give much insight into why.

We might hazard a guess that the connection is being closed because 
there is no SMTP server running on the named host, or (less likely) it 
requires authentication. Unfortunately when I try to connect to a 
non-existent SMTP server on an existent host I see

socket.error: (113, 'Software caused connection abort')

under both 2.4.3 and 2.5b2. Alas I don't have ready access to a server 
that requires authentication (my servers authenticate senders by POP 
access IIRC). So I can't reproduce this error.

Perhaps the SMTP server is strapped down to accepting connections from 
specific IP addresses by some firewall? It would probably be a good idea 
to have a chat with the server's administrator to see if they can 
suggest what might be going wrong.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list