NAME

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

DESCRIPTION

MIME-tools is a collection of Perl5 MIME:: modules for parsing and decoding single- or multipart (even nested multipart) MIME messages.

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. Most of the MIME:: modules will automatically wrap raw (unblessed) filehandles inside MIME::IO objects, so that they conform to this 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.

The MIME::Decoder can be used to encode as well. When encoding an 8-bit document as a 7-bit mail message, the 8-bit characters are escaped for you, by the MIME::Latin1 module.

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

CPAN SPECIFICATIONS

Module       DSLI   Description                                 Info
----------   ----   ----------------------------------------    ----
MIME::
::Decoder    adpO   OO interface for decoding MIME messages     ERYQ
::Entity     adpO   An extracted and decoded MIME entity        ERYQ
::Head       adpO   A parsed MIME header                        ERYQ
::IO         adpO   Simple I/O handles for filehandles/scalars  ERYQ
::Latin1     adpO   Encoding 8-bit Latin-1 as 7-bit ASCII       ERYQ
::Parser     adpO   Parses streams to create MIME entities      ERYQ
::ParserBase adpO   For building your own MIME parser           ERYQ

KIT CONTENTS

./MIME/*.pm         the MIME-tools classes
./Makefile.PL       the input to MakeMaker
./COPYING           terms and conditions for copying/using the software
./README            this file
./docs/             HTMLized documentation
./etc/              convenient copies of other modules you may need
./testin/           files you can use for testing (as in "make test")
./testout/          the output of "make test"

REQUIREMENTS

You'll need Perl5.001m or better... aw, heck: let's call it 5.002.

Obtain and install the following kits from the CPAN:

MIME::QuotedPrint 

MIME::Base64

MailTools:             (1.06 or higher)  
    Mail::Header
    Mail::Internet
    etc...

For your convenience, possibly-old copies are provided in the ./etc directory, of the distribution, but they are NOT installed for you during the installation procedure.

INSTALLATION

Pretty simple:

1. Gunzip and de-tar the distribution, and cd to the top level.
2. Type:      perl Makefile.PL
3. Type:      make                    # this step is optional
4. Type:      make test               # this step is optional
5. Type:      make install

Other interesting targets in the Makefile are:

make config     # to check if the Makefile is up-to-date
make clean      # delete local temp files (Makefile gets renamed)
make realclean  # delete derived files (including ./blib)

COMPATIBILITY

If you're installing this as a replacement for the MIME-parser 1.x release, and you really don't want to break existing code, you should do this at any point before the code is invoked:

use MIME::ParserUtil;

MIME::ParserUtil->emulate_version(1.0);

Try not to get too attached to this, though. Instead, plan on upgrading your code ASAP to the 2.0 style.

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.

In my mind's eye, I see an abstract class, call it MIME::Attrs, 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 of MIME::Head: that way, MIME::Head would have to implement its interface, and allow itself to be initiallized from a MIME::Attrs object.

However, when you read RFC-1521, you begin to see how much MIME information is organized by its presence in particular fields. I imagine that we'd begin to mirror the structure of RFC-1521 fields and subfields to such a degree that this might not give us a tremendous gain over just having MIME::Head.

To subclass or not to subclass?

When I originally wrote this module for the CPAN, I agonized for a long time about whether or not it really should just be a subclass of Mail::Internet (then at version 1.17). There were plusses:

  • Software reuse.

  • Inheritance of the mail-sending utilities.

And, unfortunately, minuses:

  • The Mail::Internet 1.17 model of messages as being short enough to fit into in-core arrays is excellent for most email applications; however, it seemed ill-suited for generic MIME applications, where MIME streams could be megabytes long.

  • The implementation of Mail::Internet 1.17 was excellent for certain kinds of header manipulation, but the implementation of get() was less-efficient than I would have liked for MIME applications.

  • In my heart of hearts, I honestly felt that the head should be encapsulated as a first-class object, and in Mail::Internet 1.17 it was not.

So I chose to make MIME::Head and MIME::Entity their own standalone modules.

Since that time, I worked with Graham Barr (author of most of the MailTools package, and a darn nice guy to "work" with over email), and he has graciously evolved the MailTools modules into a direction that addressed a lot of these issues.

With MailTools now its 1.06 release, it was finally time to finish what I started, and release MIME-tools 2.0.

CHANGE LOG

Ancient history

Version 2.0

Modules now inherit from the new Mail:: modules! This means big changes in behavior.

Version 1.13

MIME::Head now no longer requires space after ":", although either a space or a tab after the ":" will be swallowed if there. Thanks to Igor Starovoitov for pointing out this shortcoming.

Version 1.12

Fixed bugs in parser where CRLF-terminated lines were blowing out the handling of preambles/epilogues. Thanks to Russell Sutherland for reporting this bug.

Fixed idiotic is_multipart() bug. Thanks to Andreas Koenig for noticing it.

Added untested binmode() calls to parser for DOS, etc. systems. No idea if this will work...

Reorganized the output_path() methods to allow easy use of inheritance, as per Achim Bohnet's suggestion.

Changed MIME::Head to report mime_type more accurately.

POSIX module no longer loaded by Parser if perl >= 5.002. Hey, 5.001'ers: let me know if this breaks stuff, okay?

Added unsupported ./examples directory.

Version 1.11

Converted over to using Makefile.PL. Thanks to Andreas Koenig for the much-needed kick in the pants...

Added t/*.t files for testing. Eeeeeeeeeeeh...it's a start.

Fixed bug in default parsing routine for generating output paths; it was warning about evil filenames if there simply *were* no recommended filenames. D'oh!

Fixed redefined parts() method in Entity.

Fixed bugs in Head where field name wasn't being case folded.

Version 1.10

A typo was causing the epilogue of an inner multipart message to be swallowed to the end of the OUTER multipart message; this has now been fixed. Thanks to Igor Starovoitov for reporting this bug.

A bad regexp for parameter names was causing some parameters to be parsed incorrectly; this has also been fixed. Thanks again to Igor Starovoitov for reporting this bug.

It is now possible to get full control of the filenaming algorithm before output files are generated, and the default algorithm is safer. Thanks to Laurent Amon for pointing out the problems, and suggesting some solutions.

Fixed illegal "simple" multipart test file. D'OH!

Version 1.9

No changes: 1.8 failed CPAN registration

Version 1.8.

Fixed incompatibility with 5.001 and FileHandle::new_tmpfile Added COPYING file, and improved README.

Future plans

  • Add option to parse message/rfc822 as a pseduo multipart document.

  • Dress up mimedump and mimeexplode utilities to take cmd line options for directory, environment vars (MIMEDUMP_OUTPUT, etc.).

  • Allow you to specify a cutoff() method that will determine whether decoded message is stored on disk or in a scalar. Default will probably keep messages <10-20K in core. In such a case, the body() method will return undef; we'll need a new way of accessing the data transparently. Too bad setvbuf() isn't guaranteed... we could just "open" a filehandle on the scalar... hmmm... maybe I'll provide an open_body() for those lucky few that have it... :-)

TERMS AND CONDITIONS

Copyright (c) 1996 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.

SEE ALSO

The MIME format is documented in RFC 1521.

The MIME header format is documented in RFC 822.

AUTHOR

Eryq, eryq@rhine.gsfc.nasa.gov.

Initial release (1.0): 28 April 1996. Re-release (2.0): Halloween 1996.