[Tutor] Sending e-mail msg

Python python at venix.com
Wed May 17 18:24:16 CEST 2006


On Wed, 2006-05-17 at 10:03 -0500, URBAN LANDREMAN wrote:
> I'm trying to set up a routine to send customized e-mail messages from 
> records in a database.
> My first step is trying to send one simple e-mail message.  After I get that 
> figured out, I can move on to step 2.
> 
> Unfortunately, when I try the example code in the tutorials for sending 
> e-mail, I get error messages which I am unsure how to interpret.
> 
> The code I use is:
> import smtplib
> 
> fromaddr = "urban.landreman at co.hennepin.mn.us"
> toaddrs  = "ulandreman at msn.com"
> # Add the From: and To: headers at the start!
> msg = ("From: %s\r\nTo: %s\r\n\r\n"
>        % (fromaddr,  toaddrs))
> msg = msg + "This is a test message"
> 
> print "Message = " + msg
> print "Message length is " + repr(len(msg))
> 
> server = smtplib.SMTP('mail.hennepinPublicHealth.org')
> server.set_debuglevel(1)
> server.sendmail(fromaddr, toaddrs, msg)
> server.quit()
> ***
> 
> The output I get is:
> 
> Message = From: urban.landreman at co.hennepin.mn.us
> 
> To: ulandreman at msn.com
> 
> 
> 
> This is a test message
> Message length is 89
> 
> Traceback (most recent call last):
>   File "J:/SPSS/Python/EMailExample.py", line 14, in -toplevel-
>     server = smtplib.SMTP('mail.hennepinPublicHealth.org')
>   File "C:\Program Files\Python\lib\smtplib.py", line 244, in __init__
>     (code, msg) = self.connect(host, port)
>   File "C:\Program Files\Python\lib\smtplib.py", line 307, in connect
>     (code, msg) = self.getreply()
>   File "C:\Program Files\Python\lib\smtplib.py", line 348, in getreply
>     line = self.file.readline()
>   File "C:\Program Files\Python\lib\socket.py", line 340, in readline
>     data = self._sock.recv(self._rbufsize)
> error: (10054, 'Connection reset by peer')

'Connection reset by peer'

This occurs when a connection is terminated without going through the
normal TCP close procedure.  If localhost is giving you the same
behavior, you should be able to learn why from your logs.

Your code looks OK.  I would expect that the issue lies with your
network config or firewall(s).

That mail server was happy to accept a connection from me, so I doubt if
the problem is at the server.

>>> import smtplib
>>> c = smtplib.SMTP('mail.hennepinPublicHealth.org')
>>> dir(c)
['__doc__', '__init__', '__module__', 'close', 'connect', 'data', 'debuglevel', 'docmd', 'does_esmtp', 'ehlo', 'ehlo_resp', 'esmtp_features', 'expn', 'file', 'getreply', 'has_extn', 'helo', 'helo_resp', 'help', 'local_hostname', 'login', 'mail', 'noop', 'putcmd', 'quit', 'rcpt', 'rset', 'send', 'sendmail', 'set_debuglevel', 'sock', 'starttls', 'verify', 'vrfy']
>>> c.close()

> 
> ***
> I also tried:
> server = smtplib.SMTP('localhost')
> 
> with the same result.
> 
> I have checked several of the tutorials and FAQ sites and the impression 
> that's given is that this functionality is quite simple, so I assume that 
> I'm just missing one small step.
> 
> Any suggestion would be much appreciated.
> 
> Thank you.
> Urban Landreman
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list