CGI email script

bojanraic at gmail.com bojanraic at gmail.com
Sat Nov 20 17:02:07 EST 2004


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"

def main():
    form = cgi.FieldStorage()
    if form.has_key("name") and form["name"].value != "":
        fromaddress = form["email"].value
        toaddress = my at email.com
        message = 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?
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...
3. What am I doing wrong here? How can I fix the errors and make it
work?

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.



More information about the Python-list mailing list