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
Solved

python cgi - mail form

Posted on 2004-08-05
5
905 Views
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 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
0
Comment
Question by:bnblazer
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
rjkimble earned 500 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 mailform.py

import email
import smtplib
import sys

TEMPLATE = '''
From: %(from)s
To: %(to)s
Subject: %(subject)s

%(body)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 ) )
    server.quit()
# EOF

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
0
 
LVL 1

Author Comment

by:bnblazer
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,
Brian
0
 
LVL 9

Expert Comment

by:rjkimble
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
0
 
LVL 1

Author Comment

by:bnblazer
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.

Brian
0
 
LVL 9

Expert Comment

by:rjkimble
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

829 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