Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 942
  • Last Modified:

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
0
bnblazer
Asked:
bnblazer
  • 3
  • 2
1 Solution
 
rjkimbleCommented:
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
 
bnblazerAuthor Commented:
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
 
rjkimbleCommented:
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
 
bnblazerAuthor Commented:
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
 
rjkimbleCommented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now