Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


python cgi - mail form

Posted on 2004-08-05
Medium Priority
Last Modified: 2008-03-03
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

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)

Thanks for your help,
Question by:bnblazer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

rjkimble earned 2000 total points
ID: 11733386
OOP suggests that you're going to create classes and instantiate objects from those classes. What you have done here is to create a module with a method in it. There is absolutely nothing wrong with that, and I think your approach makes perfect sense, but it doesn't really have much "OOP" to it. Also, your send_cgi() method doesn't have any arguments, which makes me wonder how you're getting the information you need to send the email. It's also missing the colon at the end of the "def" line.

It looks to me that you're using an older version of Python. I say that, because you're using string.join() -- you misspelled string in that expression, BTW -- and your smtplib has a postfix() method. I'm using version 2.3, which seems to have a sendmail() method instead. I'd also use the email module. Here is how I might write your application (using Python 2.3):

#! /usr/bin/python

# this module takes care of sending the mail generated by

import email
import smtplib
import sys

From: %(from)s
To: %(to)s
Subject: %(subject)s

'''.strip( )

def send_cgi( sender, recipient, sbj, content ):
    HOST = "localhost"
    msgdict = dict( )
    msgdict[ 'from' ] = sender
    msgdict[ 'to' ] = recipient
    msgdict[ 'subject' ] = sbj
    msgdict[ 'body' ] = content
    msg = email.message_from_string( TEMPLATE % msgdict )
    print str( msg )

    server = smtplib.SMTP( HOST )  
    server.sendmail( sender, [recipient], str( msg ) )

Note that I used a dictionary to put the text into the email message. I think it makes the script more readable. I added arguments to the send_cgi() method so that the calling routine can pass them in. I use the email.message_from_string() method because it adds some error checking to the email creation process, and you can be sure that if it successfully parses the string that the resulting email will be well formed for transmittal.

In reality, however, there's not a tremendous difference between our scripts.

.... Bob

Author Comment

ID: 11734762
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,

Expert Comment

ID: 11737361
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

Author Comment

ID: 11737554
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.


Expert Comment

ID: 11737785
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question