Newbie trying to use Python with Sendmail and MySQL

Oleg Broytmann phd at phd.russ.ru
Thu Aug 24 03:49:24 EDT 2000


Hello.

On Wed, 23 Aug 2000, John Tulabing wrote:
> I am attempting to receive a mail message, and then push all the data
> within the message to a MySQL database.  Is there already a tool or app
> that already does this?  If not, is Python the best way to go?

   This is just an example of using python - and yes, python is best for
the task!

   Master site: http://sun.med.ru/~phd/Software/Python/#extract_mime

   Faster mirrors: http://skyscraper.fortunecity.com/unix/797/Software/Python/#extract_mime
   http://members.xoom.com/_XMCM/phd2.1/Software/Python/index.html#extract_mime

                       Extract MIME from incoming mail

extract_mime - shell wrapper
extract_mime.py - python script

   The extract_mime.py program uses mimetools and multifile lib modules,
which require that input file supports random access (seek). Usually MTA
(sendmail) passes mail to stdin (which is not seekable). To work around
this
I wrote a shell wrapper that chdir to a work directory, removes temp file
(if there are any), saves stdin to a real file and runs python script.
   Python script tests whether the message is really MIME message (if not
it aborts - you need some tweaking here if you want to get simple mail),
splits incoming message into parts and saves is as files.
   The first two parts of any MIME message are MIME warning (for
MIME-incapable MUAs) and message text. My script skips both - I don't need
it. Again, it is easy to teach my program to caught message text.
   After these two parts there are attachments. Python script extracts
these attachments and saves it under original names (if mail message
provides ones; if there are no filenames my scripts generates ones in form
of "outfile%d", where %d is ordinal number of attachment. This lead to a
problem if you pass many mail messages to the program at once - some
outfiles could overwrite others).

   To use these programs you need to teach your MTA to "send" mail to the
shell wrapper. For sendmail and compatibles you need to edit /etc/aliases
(or /etc/mail/aliase or such) and run newaliases.
   Add the following line to /etc/aliases:
robot: "| /path/to/extract_mime"
   Now all messages sent to robot at your.host.domain will be passed to
extract_mime!
   Usually MTA is running under user root (I am talking about UNIX systems.
If you are using other system as mail server - you are in big trouble :).
To
make your program more secure, run them under different user:
robot: "| su umail -c /path/to/extract_mime". Make sure the directory where
the program writes files is readable and writeable.
   For MTAs that are not running as root you either agree to run these
program under mail user (whatever) or you may change the user. Just compile
setuid wrapper (there is nice setuid-prog.c in python distribution), make
it setuid (setgid) and run via /etc/aliases:
robot: "| /path/to/setuid_wrapper"
   Don't forget to run newaliases after changing the aliases file.

Oleg.
---- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list