Solved

python cgi - mail form

Posted on 2004-08-05
5
891 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 …
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now