Home | MIME::Body | MIME::Decoder | MIME::Entity | MIME::Head |
MIME::IO | MIME::Latin1 | MIME::Parser | MIME::ParserBase | |
MIME::ToolUtils |
MIME-tools![]() It's MIME time! * |
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...
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;
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.
If you want to tweak the way this toolkit works (for example, to turn on debugging), use the routines in the MIME::ToolUtils module.
Here's an example, which composes and sends a MIME message containing two parts: a text file, and an attached GIF:
use MIME::Entity;
# Create the top-level, and set up the mail headers: $top = build MIME::Entity Type=>"multipart/mixed"; $top->head->add('from', "me\@myhost.com"); $top->head->add('to', "you\@yourhost.com"); $top->head->add('subject', "Hello, nurse!"); # Attachment #1: a simple text document: attach $top Path=>"./testin/short.txt"; # Attachment #2: a GIF file: attach $top Path => "./docs/mime-sm.gif", Type => "image/gif", Encoding => "base64"; # Send it: open MAIL, "| /usr/lib/sendmail -t -i" or die "open: $!"; $top->print(\*MAIL); close MAIL;
I'm working on making this even easier (in particular, to make it easier for you to set up the data for each attachment, and to test the interface with Mail::Send). I'd also like to make it so that the content-type and encoding can be automatically inferred from the file's path.
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
./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"
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.
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)
use MIME::ToolUtils; MIME::ToolUtils->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.
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.
However, 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.
get()
was less-efficient than I would have liked for MIME applications.
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.
"\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.
"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
.
"\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.
Fixed bug in MIME::Entity::body() where it was using the bodyhandle completely incorrectly. Thanks to Joel Noble for bringing this to my attention.
Fixed MIME::Head::VERSION so CPAN:: is happier. Thanks to Larry Virden for bringing this to my attention.
Fixed undefined-variable warnings when dumping skeleton (happened when there was no Subject: line) Thanks to Joel Noble for bringing this to my attention.
Added option to parse "message/rfc822"
as a pseduo-multipart document. Thanks to Andreas Koenig for suggesting
this.
MIME::Parser can now store message data in-core. There were a lot of requestes for this feature.
MIME::Entity can now compose messages. There were a lot of requestes for this feature.
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.
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.
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!
See the COPYING file in the distribution for details.
The MIME header format is documented in RFC 822.
Eryq, eryq@rhine.gsfc.nasa.gov
Initial release (1.0): 28 April 1996. Re-release (2.0): Halloween 1996.
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 Andreas Koenig Numerous good ideas, tons of beta testing, and help with CPAN-friendly packaging Igor Starovoitov Bug reports and suggestions
Not to mention the Accidental Beta Test Team, whose bug reports have been invaluable in improving the whole:
Phil Abercrombie Andrew Pimlott Joel Noble Russell Sutherland Larry Virden
Please forgive me if I've left you out. Or email me.