0
votes
import smtplib

fromaddress = '[email protected]'
toaddress = [fromaddress]
message = """
From: %s
To: %s
Subject: test

Hello

Goodbye
""" % ( fromaddress, toaddress)

server = smtplib.SMTP('mydomain.com') 
server.set_debuglevel(1) 
server.sendmail(fromaddress, toaddress, message) 

This code opens a socket ok but then fails on sendmail with a ' Connection unexpectedly closed ' error. From looking at the smtplib source this occurs when an empty response is returned from the smtp server.

What am I missing from the above code? I'm running this on a cheapy webhost as a cgi script and as part of their service they have sendmail and python services installed. In their docs they have the standard 'send an email in php' code example with no authentication needed so I'm supposing the same should work for python.

Anyone have any insight? The host is hostgator if it helps.

EDIT:

The underlying error is the smtp server issuing a hard reset ie TCP 'Connection reset by peer'. In what circumstances would an email server do this?

2
It might close the connection if it decides that the message is spam. For example, if the from address really is "[email protected]". Or you might be connecting to the wrong smtp server. Check the HELO exchange and confirm that you've got the right server. Does "mydomain.com" resolve to an A record that points at hostgator's smtp server? Finally, try using telnet and running the SMTP protocol by hand. Maybe you'll notice an error message that makes sense.Robᵩ
You don't need telnet access to your server, you just need shell access of any sort. Once you are logged into your server, then you run "telnet mydomain.com 25".Robᵩ
sry, that's what I meant, no shell access on this server (which is why i'm running cgi instead of wsgi)user1561108

2 Answers

2
votes

I believe toaddress should be a list.

fromaddress = '[email protected]'
toaddress = [fromaddress]
2
votes

You're trying to send via SMTP , which is usually implemented with some sort of authentication ( username + password ). Most SMTP servers will immediately kill the connection if you don't authenticate ( or your IP Address fails authentication too many times within a given interval ).

By calling smtplib.SMTP([host[, port[, local_hostname[, timeout]]]]) , you're trying to open up a connection to mydomain.com. Most webhosts will have a centralized mail system ( SMTP / IMAP ) that is separate from the shell systems too.

I haven't debugged SMTP stuff in a while ( or used SMTP under Python ) but you an poke around by jumping into the webhost and doing telnet mydomain.com 25 which should let you see if the SMTP server is willing to accept connections from you... or is just leaving.

You can also try doing a phpinfo() on your webhost and seeing if they have any special SMTP authentication stuff built in.

Other than that, I would suggest you try piping messages into the sendmail(compatible) binary, and let the server handle SMTP.

it would look something like this...

p= os.popen( "/usr/sbin/sendmail -t -i -f" , "w" )
p.write(message)
status = p.close()
if status :
    raise RuntimeError("Could not excecute sendmail properly")