http://qs1969.pair.com?node_id=78360

Item Description: Parse and create MIME messages

Review Synopsis:

While Mail::Internet will permit you to manipulate ordinary emails, if you want to deal with attachments or foreign characters, you will eventually have to descend into the world of MIME.

CPAN provides two basic suites for handling such messages: MIME::Lite, which permits generation, but not parsing, and the MIME-tools package, which both generates and parses MIME messages.

Due to the complexity of MIME messages, the MIME-tools package can get confusing. You will likely find yourself bouncing between three or four different man pages/PODs to get where you want to go.

Creating a MIME message is quite easy, and involves the MIME::Entity module (included). You simply call the build method to create the base message and then repeatedly call the attach method to add on parts.

Decoding an incoming message is more complicated. In Mail::Internet, every message has a header and a body. This is no longer true in the world of MIME. If the message is single-part, MIME-tools will return a header and a body handle (the change allowing easier access to binary files). However, in a multi-part message (one that has attachments), the body does not exist as far as the package is concerned. Instead, you have to look at the ->parts of the message, each of which has a head and a body (unless the part itself is multipart, in which case you'll have to recurse...).

There are a couple of convenience features that make life a bit easier. For instance, you can call the make_multipart method on all your incoming messages so that singlepart messages cease being a special case -- now you can use the parts method on it (iterating over the number of parts) to get the one or more parts that exist. If you have a multipart message that only has one part, there is a companion method to make_singlepart. Both of these methods behave gracefully when given inappropriate input.

Good: The modules provided manage to handle just about everything you could want with MIME messages, and since HTMail and attachments are ubiquitous, few people can afford to assume that their mail can be handled by Mail::Internet.

Bad: The moduleset is definitely "heavyweight". If you can get away with MIME::Lite (receiving only), you will save yourself some development time, and of course, your code will be "lighter", as should be obvious from the name. You will also need another way to send the mail you have produced (either pipe it to /usr/lib/sendmail -t -i on a *NIX or use Mail::Sendmail or Net::SMTP). As mentioned in a comment below, the encoding/decoding is done in memory, which can be a limitation. MIME-tools does, however, save parts out to disk if they are long, so you only have to have one part in memory at a time. A caveat is in order here: if you're creating a MIME message and use the purge method to get rid of disk files, the module will (try to) unlink the original attachment. If it fails due to file/directory permissions, it doesn't complain, though.

Replies are listed 'Best First'.
Re: MIME-tools
by kschwab (Vicar) on May 07, 2001 at 04:30 UTC
    My biggest nit with MIME-tools is that it encodes and decodes entirely in memory. MIME attachments can and do get very large, and you can easily run your box into the ground.

    I've resorted to using system() calls to mutt to get the job done. ( well, for sending mime attached emails...I understand that MIME-Tools covers more ground.)

Security concerns
by ngaur (Initiate) on Jun 02, 2004 at 15:25 UTC
    MIME::Tools has become an important component in a number of antivirus scanners and the like in mail gateways. In this sort of usage, you can't assume well formed MIME input. Some viruses deliberately mess with the MIME formatting in order to evade detection. IF you're dealing with messages you can reasonably expect not to be maliciously constructed, there should be no problem.

    For more information on the problems, see

    http://www.securiteam.com/unixfocus/5JP041F7FA.html

    The Current release version of MIME::Tools (5.411a) hasn't been updated since 2001, and the author is advertising that he's looking for someone else to take over MIME::Tools and MIME::Lite. The makers of MIME::Defang have been releasing their own version. You can get it at

    http://www.mimedefang.org/node.php?id=1

Re: MIME-tools
by Jenda (Abbot) on Feb 10, 2003 at 22:13 UTC
    While Mail::Internet will permit you to manipulate ordinary emails, if you want to deal with attachments or foreign characters, you will eventually have to descend into the world of MIME.
    CPAN provides two basic suites for handling such messages: MIME::Lite, which permits generation, but not parsing, and the MIME-tools package, which both generates and parses MIME messages.

    Wow, that sounds is if you wanted to say Mail::Sender is advanced ;-)

    I guess you just did not notice it there. For others like that ... Mail::Sender is also just for the creation of MIME messages, like MIME::Lite, and to some people it may look more understandable.

    The main difference between MIME::* and Mail::Sender is that with MIME::* you first build up a structure of message parts and then do something with the object, while Mail::Sender is like "open the connection, send the headers, start such and such part, send this data, start some other part, send that data and close the connection". Or "open the connection, send this body, attach this file and close".

    It wount give you the power of MIME::*, it looks messy if the MIME structure gets more complex, but for simple things it's just fine (I have to know, I'm the author ;-)

    As far as the "encoding/deconding" in memory versus on disk goes ... Mail::Sender doesn't do either. It sends everything to the SMTP server immediately.

    Jenda

Re: MIME-tools
by sweetblood (Prior) on Aug 08, 2003 at 02:49 UTC
    It's true that MIME::Tools is a bit heady, but it's damn near impossible to do without when dealing with attachments and nested messages. Combine it with Net::POP3 and build your own pop3 email client or automate email tasks. Once you get the hang of it, you can really do some cool stuff.