connect in SMTPlib

Rajkumar S. raj2569 at yahoo.com
Sun Jul 15 15:32:12 EDT 2001


Hi all,

I am writing cgi program to send a mail of all the selection made by
the user to admin. The from addr and to addr are fixed in the script.
One of the probable error that I see is that of the SMTP server being
down. Their are no exceptions defined for the connect(). So the script
bombs when the connect fails and gives the 500 error.

Can the script be made more robust esp when faced by a down SMTP
server?

raj

#!/usr/local/bin/python
import smtplib
import string
import cgi

fromaddr = "xxx <xx at xx.com>"
toaddrs  = "xxx <xx.com>"

thankyou = '''Content-Type: text/html\n\n
<html>
<head> <title> Thank You!</title> </head>
<body>
Thank you !!
</body>
</html>'''

error = '''Content-Type: text/html\n\n
    <html>
    <head> <title> We are sorry</title> </head>
    <body>
    We are extremely sorry that an error had occured while sending mail.
    </body>'''

form = cgi.FieldStorage()
formkeys = form.keys()

<snip>
</snip>

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, toaddrs, order))
line = msg + line

server = smtplib.SMTP('localhost')

try:
    server.sendmail(fromaddr, toaddrs, line)
except smtplib.SMTPException:
    print error
    server.quit()
server.quit()
print thankyou





More information about the Python-list mailing list