The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MIME-tools - modules for parsing (and creating!) MIME entities

SYNOPSIS

Here's some pretty basic code for parsing a MIME message, and outputting its decoded components to a given directory:

    use MIME::Parser;
     
    # Create parser, and set the output directory:
    my $parser = new MIME::Parser;
    $parser->output_dir("$ENV{HOME}/mimemail");
     
    # Parse input:
    $entity = $parser->read(\*STDIN) or die "couldn't parse MIME stream";
    
    # Take a look at the top-level entity (and any parts it has):
    $entity->dump_skeleton; 

Here's some code which composes and sends a MIME message containing three parts: a text file, an attached GIF, and some more text:

    use MIME::Entity;

    # Create the top-level, and set up the mail headers:
    $top = build MIME::Entity Type    =>"multipart/mixed",
                              From    => "me\@myhost.com",
                              To      => "you\@yourhost.com",
                              Subject => "Hello, nurse!";
    
    # Part #1: a simple text document: 
    attach $top  Path=>"./testin/short.txt";
    
    # Part #2: a GIF file:
    attach $top  Path        => "./docs/mime-sm.gif",
                 Type        => "image/gif",
                 Encoding    => "base64";
        
    # Part #3: some literal text:
    attach $top  Data=>$message;
    
    # Send it:
    open MAIL, "| /usr/lib/sendmail -t -i" or die "open: $!";
    $top->print(\*MAIL);
    close MAIL;

DESCRIPTION

MIME-tools is a collection of Perl5 MIME:: modules for parsing, decoding, and generating single- or multipart (even nested multipart) MIME messages. (Yes, kids, that means you can send messages with attached GIF files).

A QUICK TOUR

Overview of the classes

Here are the classes you'll generally be dealing with directly:

           .------------.       .------------.           
           | MIME::     |------>| MIME::     |
           | Parser     |  isa  | ParserBase |   
           `------------'       `------------'
              | parse()
              | returns a...
              |
              |
              |
              |    head()       .--------.
              |    returns...   | MIME:: | get()
              V       .-------->| Head   | etc... 
           .--------./          `--------'      
     .---> | MIME:: | 
     `-----| Entity |           .--------. 
   parts() `--------'\          | MIME:: | 
   returns            `-------->| Body   |
   sub-entities    bodyhandle() `--------'
   (if any)        returns...       | open() 
                                    | returns...
                                    | 
                                    V  
                                .--------. read()    
                                | IO::   | getline()  
                                | Handle | print()          
                                `--------' etc...    

To illustrate, parsing works this way:

  • The "parser" parses the MIME stream. Every "parser" inherits from the "parser base" class, which does the real work. When a message is parsed, the result is an "entity".

  • An "entity" has a "head" and a "body". Entities are MIME message parts.

  • A "body" knows where the data is. You can ask to "open" this data source for reading or writing, and you will get back an "I/O handle".

  • An "I/O handle" knows how to read/write the data. It is an object that is basically like an IO::Handle or a FileHandle... it can be any class, so long as it supports a small, standard set of methods for reading from or writing to the underlying data source.

A typical multipart message containing two parts -- a textual greeting and an "attached" GIF file -- would be a tree of MIME::Entity objects, each of which would have its own MIME::Head. Like this:

    .--------.
    | MIME:: | Content-type: multipart/mixed 
    | Entity | Subject: Happy Samhaine!
    `--------'
         `----.
        parts |
              |   .--------.   
              |---| MIME:: | Content-type: text/plain; charset=us-ascii
              |   | Entity | Content-transfer-encoding: 7bit
              |   `--------' 
              |   .--------.   
              |---| MIME:: | Content-type: image/gif
                  | Entity | Content-transfer-encoding: base64
                  `--------' Content-disposition: inline; filename="hs.gif"

Parsing, in a nutshell

You usually start by creating an instance of MIME::Parser (a subclass of the abstract MIME::ParserBase), and setting up certain parsing parameters: what directory to save extracted files to, how to name the files, etc.

You then give that instance a readable filehandle on which waits a MIME message. If all goes well, you will get back a MIME::Entity object (a subclass of Mail::Internet), which consists of...

  • A MIME::Head (a subclass of Mail::Header) which holds the MIME header data.

  • A MIME::Body, which is a object that knows where the body data is. You ask this object to "open" itself for reading, and it will hand you back an "I/O handle" for reading the data: this is a FileHandle-like object, and could be of any class, so long as it conforms to a subset of the IO::Handle interface.

If the original message was a multipart document, the MIME::Entity object will have a non-empty list of "parts", each of which is in turn a MIME::Entity (which might also be a multipart entity, etc, etc...).

Internally, the parser (in MIME::ParserBase) asks for instances of MIME::Decoder whenever it needs to decode an encoded file. MIME::Decoder has a mapping from supported encodings (e.g., 'base64') to classes whose instances can decode them. You can add to this mapping to try out new/experiment encodings. You can also use MIME::Decoder by itself.

Composing, in a nutshell

All message composition is done via the MIME::Entity class. For single-part messages, you can use the "build" in MIME::Entity constructor to create MIME entities very easily.

For multipart messages, you can start by creating a top-level multipart entity with "build" in MIME::Entity, and then use the similar "attach" in MIME::Entity method to attach parts to that message. Please note: what most people think of as "a text message with an attached GIF file" is really a multipart message with 2 parts: the first being the text message, and the second being the GIF file.

When building MIME a entity, you'll have to provide two very important pieces of information: the content type and the content transfer encoding. The type is usually easy, as it is directly determined by the file format; e.g., an HTML file is text/html. The encoding, however, is trickier... for example, some HTML files are 7bit-compliant, but others might have very long lines and would need to be sent quoted-printable for reliability.

See the section on encoding/decoding for more details, as well as "A MIME PRIMER".

Encoding/decoding, in a nutshell

The MIME::Decoder class can be used to encode as well; this is done when printing MIME entities. All the standard encodings are supported (see "A MIME PRIMER" for details):

    Encoding...       Normally used when message contents are...
    -------------------------------------------------------------------
    7bit              7-bit data with under 1000 chars/line, or multipart.
    8bit              8-bit data with under 1000 chars/line.
    binary            8-bit data with possibly long lines (or no line breaks).
    quoted-printable  Text files with some 8-bit chars (e.g., Latin-1 text).
    base64            Binary files.

Which encoding you choose for a given document depends largely on (1) what you know about the document's contents (text vs binary), and (2) whether you need the resulting message to have a reliable encoding for 7-bit Internet email transport.

In general, only quoted-printable and base64 guarantee reliable transport of all data; the other three "no-encoding" encodings simply pass the data through, and are only reliable if that data is 7bit ASCII with under 1000 characters per line, and has no conflicts with the multipart boundaries.

I've considered making it so that the content-type and encoding can be automatically inferred from the file's path, but that seems to be asking for trouble... or at least, for Mail::Cap...

Other stuff you can do

If you want to tweak the way this toolkit works (for example, to turn on debugging), use the routines in the MIME::ToolUtils module.

Good advice

  • Run with -w. If you see a warning about a deprecated method, change your code ASAP. This will ease upgrades tremendously.

  • Don't try to MIME-encode using the non-standard MIME encodings. It's just not a good practice if you want people to be able to read your messages.

  • Be aware of possible thrown exceptions. For example, if your mail-handling code absolutely must not die, then perform mail parsing like this:

        $entity = eval { $parser->parse(\*INPUT) };
        

    Parsing is a complex process, and some components may throw exceptions if seriously-bad things happen. Since "seriously-bad" is in the eye of the beholder, you're better off catching possible exceptions instead of asking me to propagate undef up the stack. Use of exceptions in reusable modules is one of those religious issues we're never all going to agree upon; thankfully, that's what eval{} is good for.

NOTES

Terminology

Here are some excerpts from RFC-1521 explaining the terminology we use; each is accompanied by the equivalent in MIME:: module terms...

Message

From RFC-1521:

    The term "message", when not further qualified, means either the
    (complete or "top-level") message being transferred on a network, or
    a message encapsulated in a body of type "message".

There currently is no explicit package for messages; under MIME::, messages are streams of data which may be read in from files or filehandles.

Body part

From RFC-1521:

    The term "body part", in this document, means one of the parts of the
    body of a multipart entity. A body part has a header and a body, so
    it makes sense to speak about the body of a body part.

Since a body part is just a kind of entity (see below), a body part is represented by an instance of MIME::Entity.

Entity

From RFC-1521:

    The term "entity", in this document, means either a message or a body
    part.  All kinds of entities share the property that they have a
    header and a body.

An entity is represented by an instance of MIME::Entity. There are instance methods for recovering the header (a MIME::Head) and the body (a MIME::Body).

This is the top portion of the MIME message, which contains the Content-type, Content-transfer-encoding, etc. Every MIME entity has a header, represented by an instance of MIME::Head. You get the header of an entity by sending it a head() message.

Body

From RFC-1521:

    The term "body", when not further qualified, means the body of an
    entity, that is the body of either a message or of a body part.

A body is represented by an instance of MIME::Body. You get the body of an entity by sending it a bodyhandle() message.

Compatibility

As of 4.x, MIME-tools can no longer emulate the old MIME-parser distribution. If you're installing this as a replacement for the MIME-parser 1.x release, you'll have to do a little tinkering with your code.

Design issues

Why assume that MIME objects are email objects?

I quote from Achim Bohnet, who gave feedback on v.1.9 (I think he's using the word header where I would use field; e.g., to refer to "Subject:", "Content-type:", etc.):

    There is also IMHO no requirement [for] MIME::Heads to look 
    like [email] headers; so to speak, the MIME::Head [simply stores] 
    the attributes of a complex object, e.g.:

        new MIME::Head type => "text/plain",
                       charset => ...,
                       disposition => ..., ... ;

I agree in principle, but (alas and dammit) RFC-1521 says otherwise. RFC-1521 [MIME] headers are a syntactic subset of RFC-822 [email] headers. Perhaps a better name for these modules would be RFC1521:: instead of MIME::, but we're a little beyond that stage now. (Note: RFC-1521 has recently been obsoleted by RFCs 2045-2049, so it's just as well we didn't go that route...)

However, in my mind's eye, I see a mythical abstract class which does what Achim suggests... so you could say:

     my $attrs = new MIME::Attrs type => "text/plain",
                                 charset => ...,
                                 disposition => ..., ... ;

We could even make it a superclass or companion class of MIME::Head, such that MIME::Head would allow itself to be initiallized from a MIME::Attrs object.

In the meanwhile, look at the build() and attach() methods of MIME::Entity: they follow the spirit of this mythical class.

To subclass or not to subclass?

When I originally wrote these modules for the CPAN, I agonized for a long time about whether or not they really should subclass from Mail::Internet (then at version 1.17). Thanks to Graham Barr, who graciously evolved MailTools 1.06 to be more MIME-friendly, unification was achieved at MIME-tools release 2.0. The benefits in reuse alone have been substantial.

Questionable practices

Fuzzing of CRLF and newline on input

RFC-1521 dictates that MIME streams have lines terminated by CRLF ("\r\n"). However, it is extremely likely that folks will want to parse MIME streams where each line ends in the local newline character "\n" instead.

An attempt has been made to allow the parser to handle both CRLF and newline-terminated input.

See MIME::ParserBase for further details.

Fuzzing of CRLF and newline when decoding

The "7bit" and "8bit" decoders will decode both a "\n" and a "\r\n" end-of-line sequence into a "\n".

The "binary" decoder (default if no encoding specified) still outputs stuff verbatim... so a MIME message with CRLFs and no explicit encoding will be output as a text file that, on many systems, will have an annoying ^M at the end of each line... but this is as it should be.

See MIME::ParserBase for further details.

Fuzzing of CRLF and newline when encoding/composing

All encoders currently output the end-of-line sequence as a "\n", with the assumption that the local mail agent will perform the conversion from newline to CRLF when sending the mail.

However, there probably should be an option to output CRLF as per RFC-1521. I'm currently working on a good mechanism for this.

See MIME::ParserBase for further details.

Inability to handle multipart boundaries with embedded newlines

First, let's get something straight: this is an evil, EVIL practice. If your mailer creates multipart boundary strings that contain newlines, give it two weeks notice and find another one. If your mail robot receives MIME mail like this, regard it as syntactically incorrect, which it is.

See MIME::ParserBase for further details.

A MIME PRIMER

So you need to parse (or create) MIME, but you're not quite up on the specifics? No problem...

Content types

This indicates what kind of data is in the MIME message, usually as majortype/minortype. The standard major types are shown below. A more-comprehensive listing may be found in RFC-2046.

application

Data which does not fit in any of the other categories, particularly data to be processed by some type of application program. application/octet-stream, application/gzip, application/postscript...

audio

Audio data. audio/basic...

image

Graphics data. image/gif, image/jpeg...

message

A message, usually another mail or MIME message. message/rfc822...

multipart

A message containing other messages. multipart/mixed, multipart/alternative...

text

Textual data, meant for humans to read. text/plain, text/html...

video

Video or video+audio data. video/mpeg...

Content transfer encodings

This is how the message body is packaged up for safe transit. There are the 5 major MIME encodings. A more-comprehensive listing may be found in RFC-2045.

7bit

No encoding is done at all. This label simply asserts that no 8-bit characters are present, and that lines do not exceed 1000 characters in length (including the CRLF).

8bit

No encoding is done at all. This label simply asserts that the message might contain 8-bit characters, and that lines do not exceed 1000 characters in length (including the CRLF).

binary

No encoding is done at all. This label simply asserts that the message might contain 8-bit characters, and that lines may exceed 1000 characters in length. Such messages are the least likely to get through mail gateways.

base64

A standard encoding, which maps arbitrary binary data to the 7bit domain. Like "uuencode", but very well-defined. This is how you should send essentially binary information (tar files, GIFs, JPEGs, etc.).

quoted-printable

A standard encoding, which maps arbitrary line-oriented data to the 7bit domain. Useful for encoding messages which are textual in nature, yet which contain non-ASCII characters (e.g., Latin-1, Latin-2, or any other 8-bit alphabet).

TERMS AND CONDITIONS

Copyright (c) 1996, 1997 by Eryq. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See the COPYING file in the distribution for details.

SUPPORT

Please email me directly with questions/problems (see AUTHOR below).

If you want to be placed on an email distribution list (not a mailing list!) for MIME-tools, and receive bug reports, patches, and updates as to when new MIME-tools releases are planned, just email me and say so. If your project is using MIME-tools, it might not be a bad idea to find out about those bugs before they become problems...

CHANGE LOG

See the README file in the distribution for the most-recent changes. For a full history, see the ./docs/MIME-tools.pod file in the distribution.

AUTHOR

MIME-tools was created by:

    ___  _ _ _   _  ___ _     
   / _ \| '_| | | |/ _ ' /    Eryq (President, Zero G Inc.)
  |  __/| | | |_| | |_| |     http://www.zeegee.com/
   \___||_|  \__, |\__, |__   eryq@zeegee.com
             |___/    |___/

Release as MIME-parser (1.0): 28 April 1996. Release as MIME-tools (2.0): Halloween 1996. Release of 4.0: Christmas 1997.

VERSION

$Revision: 4.112 $

ACKNOWLEDGMENTS

This kit would not have been possible but for the direct contributions of the following:

    Gisle Aas             The MIME encoding/decoding modules.
    Laurent Amon          Bug reports and suggestions.
    Graham Barr           The new MailTools.
    Achim Bohnet          Numerous good suggestions, including the I/O model.
    Kent Boortz           Initial code for RFC-1522-decoding of MIME headers.
    Andreas Koenig        Numerous good ideas, tons of beta testing,
                            and help with CPAN-friendly packaging.
    Igor Starovoitov      Bug reports and suggestions.
    Jason L Tibbitts III  Bug reports, suggestions, patches.
 

Not to mention the Accidental Beta Test Team, whose bug reports (and comments) have been invaluable in improving the whole:

    Phil Abercrombie
    Brandon Browning
    Kurt Freytag
    Steve Kilbane
    Jake Morrison
    Rolf Nelson
    Joel Noble    
    Michael W. Normandin 
    Tim Pierce
    Andrew Pimlott
    Dragomir R. Radev
    Nickolay Saukh
    Russell Sutherland
    Larry Virden
    Zyx

Please forgive me if I've accidentally left you out. Better yet, email me, and I'll put you in.

SEE ALSO

Users of this toolkit may wish to read the documentation of Mail::Header and Mail::Internet.

The MIME format is documented in RFCs 1521-1522, and more recently in RFCs 2045-2049.

The MIME header format is an outgrowth of the mail header format documented in RFC 822.