CGI email script
Steve Holden
steve at holdenweb.com
Sun Nov 21 15:17:17 EST 2004
bojanraic at gmail.com wrote:
> Hi!
>
> I recently started playing with Python CGI, and I was happy that my
> basic input-name--print-hello-name CGI form example worked, so I
> thought I should advance to somew\hat more complicated scripts.
>
> I'm trying to write a basic Python email CGI script to send email
> through HTML email form.
> I've seen a lot of examples out there, but nothing I tried so far
> seemed to work. So I would like to know what I am doing wrong.
>
> Here's the HTML form file:
>
> <html>
>
> <head>
> <title>Email Form</title>
> </head>
>
> <body>
>
> <FORM METHOD="POST" ACTION="sendMail.cgi">
> <INPUT TYPE=HIDDEN NAME="key" VALUE="process">
> Your name:<BR>
> <INPUT TYPE=TEXT NAME="name" size=60>
> <BR>
> Email:<BR>
> <INPUT TYPE=TEXT NAME="email" size=60>
> <BR>
> <P>
> Comments:<BR>
> <TEXTAREA NAME="comment" ROWS=8 COLS=60>
> </TEXTAREA>
> <P>
> <INPUT TYPE="SUBMIT" VALUE="Send">
> </FORM>
>
> </body>
>
> </html>
>
> Here's the CGI script:
>
> #!/usr/bin/python
>
> import cgitb
> cgitb.enable()
> import cgi
> import smtplib
>
> print "Content-type: text/html\n"
>
It's conventional, though I believe strictly unnecessary, to include a
carriage return in the terminator sequence "Content ... \r\n". Of course
your Python system will take platform specific action with the line
ending *it* inserts, of course :-)
> def main():
> form = cgi.FieldStorage()
> if form.has_key("name") and form["name"].value != "":
> fromaddress = form["email"].value
> toaddress = my at email.com
Well, this is certainly an error, because "toaddress" should be a list
of addresses (allowing you to send the same message to more than one
person - usually you'd include everyone in the "From:", "Cc:" and "Bcc:"
headers). I'm presuming you just forgot the quotes when you erased your
own email address. What you probably want here is
toaddress = ["my at email.invalid"]
or whatever. The "invalid" top-level domain is always the best one to
use when you want to be sure you aren't using a real address.
> message = form["comment"].value
>
here you really do need to be including some SMTP headers in your mail,
though a lot of mailers will be quite forgiving about this. I'd probably
use something like
message = """\
From: %s
To: %s
Subject: A message from your web site
%s
""" % (fromaddress, toaadress[0], form["comment"].value
> server = smtplib.SMTP('localhost')
> server.set_debuglevel(1)
> server.sendmail(fromaddress, toaddress, message)
> server.quit()
>
> print "<html><head><title>Sent</title></head>"
> print "<body><h1 align=center>",
> print "<p>Your message has been sent!</body>"
> print "</html>"
>
> if __name__ == '__main__': main()
>
>
>
> Of course, I change the email address to my own.
> I keep getting the premature end of script headers error.
>
> Several questions:
>
> 1. server = smtplib.SMTP('localhost') should return the name of the
> server, right? I don't need to explicitly name it, or do I?
Most systems will resolve localhosts to 127.0.0.1, so as long as your
web server runs an SMTp server as well this will work.
> 2. cgitb.enable() - shouldn't that give me a trace back if something
> goes wrong so I can debug the code? It doesn't seem to give me
> anything...
cgitb.enable() does try to produce output, but in this case you will
probably only be able to see it in your server's error log. That's a
sign that the error's occurring very early on int he sequence, which is
puzzling. Of course, if your web server's Python is old enough not to
*implement* cgitb then this would account for the error you are actually
seeing.
> 3. What am I doing wrong here? How can I fix the errors and make it
> work?
>
Well, there are a few things to try here.
> Please understand that I have read all the previous questions on this
> matter, and when I tried to follow the instructions, none of them
> seemed to work for me.
>
> Any help will be greatly appreciated.
done-my-best-ly y'rs - steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
More information about the Python-list
mailing list