Problem using smtplib.SMTP in a CGI program

David Hughes dfh at
Fri Mar 19 11:58:00 CET 2004

I wonder if anyone can help me resolve this problem. Although my ISP (One 
and One) provides the facility to run Python 2.2 CGI programs, their 
technical support people don't seem to have any Python expertise.

I am trying to run as a CGI program from a web form. It is 
nearly all working, except for the part that I have reduced to the 
following code:

# the line above must contain the path to Python on your web server
import sys, os, smtplib

sys.stderr = sys.stdout

print "Content-Type: Text/plain"
print 'Test SMTP connection'
smtp_host =  ''
print 'SMTPhost = %s' % smtp_host
server = smtplib.SMTP(smtp_host)
print 'server = %s' % repr(server)
## server.login(user, password)
server.sendmail('me at', ['you at'], 'Hello')

which produces the following output:

Test SMTP connection
SMTPhost =
Traceback (most recent call last):
  File "/homepages/40/d69667106/htdocs/cgi-bin/test_smtp.cgi", line 13, in 
    server = smtplib.SMTP(smtp_host)
  File "/usr/lib/python2.2/", line 234, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/lib/python2.2/", line 283, in connect
    raise socket.error, msg
socket.error: (111, 'Connection refused')

The value of SMTPhost is the one my ISP adviced me to use, it is an 
authenticated server requiring a user name and password and I can access 
it using Microsoft Outlook Express without difficulty.

With thanks for any help.

David Hughes
Forestfield Software Ltd

More information about the Python-list mailing list