Re: CGI email script

From: Steve Holden (steve_at_holdenweb.com)
Date: 11/21/04

  • Next message: Andrew James: "Re: Small Database Needed"
    Date: Sun, 21 Nov 2004 15:17:17 -0500
    
    

    bojanraic@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@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@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
    

  • Next message: Andrew James: "Re: Small Database Needed"

    Relevant Pages

    • Re: How do I insert a cgi script into Publisher page?
      ... If your ISP supports cgi and has a form handling program then a form created ... You must tell the server what e mail address you want the form results sent ... any where else you so desire or an auto redirect script with a delay. ... link to your host and specifically where on the host website it gives you ...
      (microsoft.public.publisher.webdesign)
    • Re: How do I insert a cgi script into Publisher page?
      ... If your ISP supports cgi and has a form handling program then a form created in publisher will function using FTP upload rather than front page server extensions to upload the pages. ... The thank you page may have a link back to the page where you came from or any where else you so desire or an auto redirect script with a delay. ... Also provide us a link to your host and specifically where on the host website it gives you the instructions for using the forms program in the cgi folder. ...
      (microsoft.public.publisher.webdesign)
    • Re: Apache: limiting the execution place
      ... They want it so users can't use FTP, shell, or a CGI or PHP script to view, ... other users via shell, FTP, web server processes (such as PHP or CGI ...
      (Security-Basics)
    • Re: How do I insert a cgi script into Publisher page?
      ... is not where I host my website. ... If your ISP supports cgi and has a form handling program then a form ... You must tell the server what e mail address you want the form results ... any where else you so desire or an auto redirect script with a delay. ...
      (microsoft.public.publisher.webdesign)

    • ... I am using python in an openlayers application to access another ... server on my network via javascript. ... The proxy script works fine ... fine with a "hello world" python script. ...
      (comp.lang.python)