Entry
Taking input from HTML form (CGI)
Jul 5th, 2000 10:00
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 123, Paul Winkler
"""
Packages: networking.internet.cgi
"""
"""
I wanted to do a very basic CGI script that takes input from an html
form and sends a message. A bit of deja-newsing turned up several
suggestions in comp.lang.python. I hacked a couple of them together and
came up with this:
"""
#!/usr/bin/python
import cgi, os
print "content-type: text/html"
print
print"<HEAD><TITLE>Message sent.</TITLE></HEAD>"
results=cgi.FieldStorage() #Always do this ONCE to get input from form.
address = results["address"].value
return_address=results["return_addr"].value
subject = results["subject"].value
text = results["text"].value
print"<p>Your message has been sent to", address
# Use sendmail to do the actual sending.
mailpipe = "/usr/lib/sendmail " + address
# os.popen means "pipe open". So we are
# opening a pipe to sendmail, then printing to it.
m = os.popen(mailpipe, "w")
m.write("To: " + address + "\n")
m.write("From: " + return_address + "\n")
m.write("Subject: " + subject + "\n\n")
m.write(text)
m.close()
# End of script