Link to home
Start Free TrialLog in
Avatar of bnblazer
bnblazer

asked on

python cgi - mail form

I am working on a 'contact form' that like all others will mail its contents using user supplied data from a web page (I know of gypsy mail but I want to do this as a learning process for myself).  I also want to try my hand at OOP for this one (have never done any OOP).  

I am OK with getting the contents from the form, but it is how I handle them once I have them that has me a bit stuck.  I have written what I think to be a 'module' (see code below) that I want to call from my form handling script to take care of the sending of the mail.  I was hoping that:

1) Someone could tell me wether or not I am on the right track with the OOP (ie is the module the way to go or should it be a class).

2) Is the code correct - or have I missed the mark?  Cold it be written better or different?

3) Am I just way off on  the whole idea, and if so, could someone please give an example to get me going in the right direction?

-----  here is the code -----

#! /usr/bin/python

# this module takes care of sending the mail generated by mailform.py

import smtplib
import string,sys

def send_cgi()
    HOST = "localhost"
    FROM = sender
    TO = recipient
    SUBJECT = sbj
    BODY = content
    body = srting.join((
        "From: %s" % FROM,
        "To: %s" % TO,
        "Subject: %s" % SUBJECT,
        "",
        BODY), "\r\n")

    print body
   
    server = smtplib.smtp(HOST)
    server.postfix(FROM, [TO], body)
    server.quit()
# EOF

Thanks for your help,
Brian
ASKER CERTIFIED SOLUTION
Avatar of rjkimble
rjkimble

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bnblazer
bnblazer

ASKER

Thanks for that very good answer.  It cleared up a lot of confusion for me.  Now, if I may, some follow up questions.

1) I used postfix() because that is what my mail server is running.  Sendmail is installed, however if that process is not running, will it still deliver?

2) Why for a cgi is the shebang #! /usr/bin/python and for a non-cgi script it is #! /usr/bin/env python?

Thanks again,
Brian
1) sendmail() is a method on the server object, not the name of a program in this case. It doesn't matter what MTA (mail transfer agent) you're using so long as it's running and listening on the standard SMTP port (25). The "server =" statement creates an instance of the SMTP class that connects to the "HOST." The sendmail method then sends the email using that connection.

2) On most systems they amount to the same thing. The "env" command figures out where the "python" command is and runs it. The first version assumes that python is installed at /usr/bin/python.

Just out of curiosity, how do you intend to call the "send_cgi()" method? If you make that script the target of your form submission, it won't work the way it's written. The way you have written it is as a module that should be imported by the actual cgi script. The cgi script will have to read the parameters submitted by the form. Have you worked out that part yet?

.... Bob
First, thanks again for your help.

I had actually thought I would just import it within the cgi script that handles the form and then (please excuse me if I do not use the correct terms), and pass it the data collected by the form.  However, something is telling me that I might get some problems with t hat method.  If all else fails, I thought I might just include the code above into one script that handles everything.

Brian
No -- you have it exactly right. There's nothing wrong with that approach. I just wanted to make sure that you understood as much. It sounds like you're on top of that.

Good luck. If you have any further problems, just post another question. I'm sure that somebody here will be able to answer it for you.

.... Bob