Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to MIME-tools by mpolo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2014-10-02 11:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (55 votes), past polls