Getting the attachment from a mime message

Oleg Broytmann phd at phd.russ.ru
Thu Sep 7 04:22:59 EDT 2000


On Wed, 6 Sep 2000, Thomas Gagne wrote:
> I'm not having much luck figuring out how Mime messages are disected.  Given:
> m = mimetools.Message(fp)
> 
> m.items() has all the header values, but what message returns a decoded
> attachment?  How do you handle a message with multiiple attachments?  How do
> you know if you're pointing to an attachment and not just some random place in
> the message body?

                       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.

WHERE TO GET
   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

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