.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "MIME::Tools 3"
.TH MIME::Tools 3 "2017-04-05" "perl v5.26.3" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
MIME\-tools \- modules for parsing (and creating!) MIME entities
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
Here's some pretty basic code for \fBparsing a \s-1MIME\s0 message,\fR and outputting
its decoded components to a given directory:
.PP
.Vb 1
\& use MIME::Parser;
\&
\& ### Create parser, and set some parsing options:
\& my $parser = new MIME::Parser;
\& $parser\->output_under("$ENV{HOME}/mimemail");
\&
\& ### Parse input:
\& $entity = $parser\->parse(\e*STDIN) or die "parse failed\en";
\&
\& ### Take a look at the top\-level entity (and any parts it has):
\& $entity\->dump_skeleton;
.Ve
.PP
Here's some code which \fBcomposes and sends a \s-1MIME\s0 message\fR containing
three parts: a text file, an attached \s-1GIF,\s0 and some more text:
.PP
.Vb 1
\& use MIME::Entity;
\&
\& ### Create the top\-level, and set up the mail headers:
\& $top = MIME::Entity\->build(Type =>"multipart/mixed",
\& From => "me\e@myhost.com",
\& To => "you\e@yourhost.com",
\& Subject => "Hello, nurse!");
\&
\& ### Part #1: a simple text document:
\& $top\->attach(Path=>"./testin/short.txt");
\&
\& ### Part #2: a GIF file:
\& $top\->attach(Path => "./docs/mime\-sm.gif",
\& Type => "image/gif",
\& Encoding => "base64");
\&
\& ### Part #3: some literal text:
\& $top\->attach(Data=>$message);
\&
\& ### Send it:
\& open MAIL, "| /usr/lib/sendmail \-t \-oi \-oem" or die "open: $!";
\& $top\->print(\e*MAIL);
\& close MAIL;
.Ve
.PP
For more examples, look at the scripts in the \fBexamples\fR directory
of the MIME-tools distribution.
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
MIME-tools is a collection of Perl5 \s-1MIME::\s0 modules for parsing, decoding,
\&\fIand generating\fR single\- or multipart (even nested multipart) \s-1MIME\s0
messages. (Yes, kids, that means you can send messages with attached
\&\s-1GIF\s0 files).
.SH "REQUIREMENTS"
.IX Header "REQUIREMENTS"
You will need the following installed on your system:
.PP
.Vb 7
\& File::Path
\& File::Spec
\& IPC::Open2 (optional)
\& MIME::Base64
\& MIME::QuotedPrint
\& Net::SMTP
\& Mail::Internet, ... from the MailTools distribution.
.Ve
.PP
See the Makefile.PL in your distribution for the most-comprehensive
list of prerequisite modules and their version numbers.
.SH "A QUICK TOUR"
.IX Header "A QUICK TOUR"
.SS "Overview of the classes"
.IX Subsection "Overview of the classes"
Here are the classes you'll generally be dealing with directly:
.PP
.Vb 10
\& (START HERE) results() .\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-.
\& \e .\-\-\-\-\-\-\-\->| MIME:: |
\& .\-\-\-\-\-\-\-\-\-\-\-. / | Parser::Results |
\& | MIME:: |\-\-\*(Aq \`\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq
\& | Parser |\-\-. .\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-.
\& \`\-\-\-\-\-\-\-\-\-\-\-\*(Aq \e filer() | MIME:: |
\& | parse() \`\-\-\-\-\-\-\-\->| Parser::Filer |
\& | gives you \`\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\*(Aq
\& | a... | output_path()
\& | | determines
\& | | path() of...
\& | head() .\-\-\-\-\-\-\-\-. |
\& | returns... | MIME:: | get() |
\& V .\-\-\-\-\-\-\-\->| Head | etc... |
\& .\-\-\-\-\-\-\-\-./ \`\-\-\-\-\-\-\-\-\*(Aq |
\& .\-\-\-> | MIME:: | |
\& \`\-\-\-\-\-| Entity | .\-\-\-\-\-\-\-\-. |
\& parts() \`\-\-\-\-\-\-\-\-\*(Aq\e | MIME:: | /
\& returns \`\-\-\-\-\-\-\-\->| Body |<\-\-\-\-\-\-\-\-\-\*(Aq
\& sub\-entities bodyhandle() \`\-\-\-\-\-\-\-\-\*(Aq
\& (if any) returns... | open()
\& | returns...
\& |
\& V
\& .\-\-\-\-\-\-\-\-. read()
\& | IO:: | getline()
\& | Handle | print()
\& \`\-\-\-\-\-\-\-\-\*(Aq etc...
.Ve
.PP
To illustrate, parsing works this way:
.IP "\(bu" 4
\&\fBThe \*(L"parser\*(R" parses the \s-1MIME\s0 stream.\fR
A parser is an instance of \f(CW\*(C`MIME::Parser\*(C'\fR.
You hand it an input stream (like a filehandle) to parse a message from:
if the parse is successful, the result is an \*(L"entity\*(R".
.IP "\(bu" 4
\&\fBA parsed message is represented by an \*(L"entity\*(R".\fR
An entity is an instance of \f(CW\*(C`MIME::Entity\*(C'\fR (a subclass of \f(CW\*(C`Mail::Internet\*(C'\fR).
If the message had \*(L"parts\*(R" (e.g., attachments), then those parts
are \*(L"entities\*(R" as well, contained inside the top-level entity.
Each entity has a \*(L"head\*(R" and a \*(L"body\*(R".
.IP "\(bu" 4
\&\fBThe entity's \*(L"head\*(R" contains information about the message.\fR
A \*(L"head\*(R" is an instance of \f(CW\*(C`MIME::Head\*(C'\fR (a subclass of \f(CW\*(C`Mail::Header\*(C'\fR).
It contains information from the message header: content type,
sender, subject line, etc.
.IP "\(bu" 4
\&\fBThe entity's \*(L"body\*(R" knows where the message data is.\fR
You can ask to \*(L"open\*(R" this data source for \fIreading\fR or \fIwriting\fR,
and you will get back an \*(L"I/O handle\*(R".
.IP "\(bu" 4
\&\fBYou can \fBopen()\fB a \*(L"body\*(R" and get an \*(L"I/O handle\*(R" to read/write message data.\fR
This handle is an object that is basically like an IO::Handle... 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.
.PP
A typical multipart message containing two parts \*(-- a textual greeting
and an \*(L"attached\*(R" \s-1GIF\s0 file \*(-- would be a tree of MIME::Entity objects,
each of which would have its own MIME::Head. Like this:
.PP
.Vb 10
\& .\-\-\-\-\-\-\-\-.
\& | MIME:: | Content\-type: multipart/mixed
\& | Entity | Subject: Happy Samhaine!
\& \`\-\-\-\-\-\-\-\-\*(Aq
\& |
\& \`\-\-\-\-.
\& parts |
\& | .\-\-\-\-\-\-\-\-.
\& |\-\-\-| MIME:: | Content\-type: text/plain; charset=us\-ascii
\& | | Entity | Content\-transfer\-encoding: 7bit
\& | \`\-\-\-\-\-\-\-\-\*(Aq
\& | .\-\-\-\-\-\-\-\-.
\& |\-\-\-| MIME:: | Content\-type: image/gif
\& | Entity | Content\-transfer\-encoding: base64
\& \`\-\-\-\-\-\-\-\-\*(Aq Content\-disposition: inline;
\& filename="hs.gif"
.Ve
.SS "Parsing messages"
.IX Subsection "Parsing messages"
You usually start by creating an instance of \fBMIME::Parser\fR
and setting up certain parsing parameters: what directory to save
extracted files to, how to name the files, etc.
.PP
You then give that instance a readable filehandle on which waits a
\&\s-1MIME\s0 message. If all goes well, you will get back a \fBMIME::Entity\fR
object (a subclass of \fBMail::Internet\fR), which consists of...
.IP "\(bu" 4
A \fBMIME::Head\fR (a subclass of \fBMail::Header\fR) which holds the \s-1MIME\s0
header data.
.IP "\(bu" 4
A \fBMIME::Body\fR, which is a object that knows where the body data is.
You ask this object to \*(L"open\*(R" itself for reading, and it
will hand you back an \*(L"I/O handle\*(R" for reading the data: this could be
of any class, so long as it conforms to a subset of the \fBIO::Handle\fR
interface.
.PP
If the original message was a multipart document, the MIME::Entity
object will have a non-empty list of \*(L"parts\*(R", each of which is in
turn a MIME::Entity (which might also be a multipart entity, etc,
etc...).
.PP
Internally, the parser (in MIME::Parser) asks for instances
of \fBMIME::Decoder\fR 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.
.SS "Composing messages"
.IX Subsection "Composing messages"
All message composition is done via the \fBMIME::Entity\fR class.
For single-part messages, you can use the \fBMIME::Entity/build\fR
constructor to create \s-1MIME\s0 entities very easily.
.PP
For multipart messages, you can start by creating a top-level
\&\f(CW\*(C`multipart\*(C'\fR entity with \fB\fBMIME::Entity::build()\fB\fR, and then use
the similar \fB\fBMIME::Entity::attach()\fB\fR method to attach parts to
that message. \fIPlease note:\fR what most people think of as
\&\*(L"a text message with an attached \s-1GIF\s0 file\*(R" is \fIreally\fR a multipart
message with 2 parts: the first being the text message, and the
second being the \s-1GIF\s0 file.
.PP
When building \s-1MIME\s0 a entity, you'll have to provide two very important
pieces of information: the \fIcontent type\fR and the
\&\fIcontent transfer encoding\fR. The type is usually easy, as it is directly
determined by the file format; e.g., an \s-1HTML\s0 file is \f(CW\*(C`text/html\*(C'\fR.
The encoding, however, is trickier... for example, some \s-1HTML\s0 files are
\&\f(CW\*(C`7bit\*(C'\fR\-compliant, but others might have very long lines and would need to be
sent \f(CW\*(C`quoted\-printable\*(C'\fR for reliability.
.PP
See the section on encoding/decoding for more details, as well as
\&\*(L"A \s-1MIME PRIMER\*(R"\s0 below.
.SS "Sending email"
.IX Subsection "Sending email"
Since MIME::Entity inherits directly from Mail::Internet,
you can use the normal Mail::Internet mechanisms to send
email. For example,
.PP
.Vb 1
\& $entity\->smtpsend;
.Ve
.SS "Encoding/decoding support"
.IX Subsection "Encoding/decoding support"
The \fBMIME::Decoder\fR class can be used to \fIencode\fR as well; this is done
when printing \s-1MIME\s0 entities. All the standard encodings are supported
(see \*(L"A \s-1MIME PRIMER\*(R"\s0 below for details):
.PP
.Vb 7
\& 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 some long lines (or no line breaks).
\& quoted\-printable | Text files with some 8\-bit chars (e.g., Latin\-1 text).
\& base64 | Binary files.
.Ve
.PP
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.
.PP
In general, only \f(CW\*(C`quoted\-printable\*(C'\fR and \f(CW\*(C`base64\*(C'\fR guarantee reliable
transport of all data; the other three \*(L"no-encoding\*(R" encodings simply
pass the data through, and are only reliable if that data is 7bit \s-1ASCII\s0
with under 1000 characters per line, and has no conflicts with the
multipart boundaries.
.PP
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...
.SS "Message-logging"
.IX Subsection "Message-logging"
MIME-tools is a large and complex toolkit which tries to deal with
a wide variety of external input. It's sometimes helpful to see
what's really going on behind the scenes.
There are several kinds of messages logged by the toolkit itself:
.IP "Debug messages" 4
.IX Item "Debug messages"
These are printed directly to the \s-1STDERR,\s0 with a prefix of
\&\f(CW"MIME\-tools: debug"\fR.
.Sp
Debug message are only logged if you have turned
\&\*(L"debugging\*(R" on in the MIME::Tools configuration.
.IP "Warning messages" 4
.IX Item "Warning messages"
These are logged by the standard Perl \fBwarn()\fR mechanism
to indicate an unusual situation.
They all have a prefix of \f(CW"MIME\-tools: warning"\fR.
.Sp
Warning messages are only logged if \f(CW$^W\fR is set true
and MIME::Tools is not configured to be \*(L"quiet\*(R".
.IP "Error messages" 4
.IX Item "Error messages"
These are logged by the standard Perl \fBwarn()\fR mechanism
to indicate that something actually failed.
They all have a prefix of \f(CW"MIME\-tools: error"\fR.
.Sp
Error messages are only logged if \f(CW$^W\fR is set true
and MIME::Tools is not configured to be \*(L"quiet\*(R".
.IP "Usage messages" 4
.IX Item "Usage messages"
Unlike \*(L"typical\*(R" warnings above, which warn about problems processing
data, usage-warnings are for alerting developers of deprecated methods
and suspicious invocations.
.Sp
Usage messages are currently only logged if \f(CW$^W\fR is set true
and MIME::Tools is not configured to be \*(L"quiet\*(R".
.PP
When a MIME::Parser (or one of its internal helper classes)
wants to report a message, it generally does so by recording
the message to the \fBMIME::Parser::Results\fR object
immediately before invoking the appropriate function above.
That means each parsing run has its own trace-log which
can be examined for problems.
.SS "Configuring the toolkit"
.IX Subsection "Configuring the toolkit"
If you want to tweak the way this toolkit works (for example, to
turn on debugging), use the routines in the \fBMIME::Tools\fR module.
.IP "debugging" 4
.IX Item "debugging"
Turn debugging on or off.
Default is false (off).
.Sp
.Vb 1
\& MIME::Tools\->debugging(1);
.Ve
.IP "quiet" 4
.IX Item "quiet"
Turn the reporting of warning/error messages on or off.
Default is true, meaning that these message are silenced.
.Sp
.Vb 1
\& MIME::Tools\->quiet(1);
.Ve
.IP "version" 4
.IX Item "version"
Return the toolkit version.
.Sp
.Vb 1
\& print MIME::Tools\->version, "\en";
.Ve
.SH "THINGS YOU SHOULD DO"
.IX Header "THINGS YOU SHOULD DO"
.SS "Take a look at the examples"
.IX Subsection "Take a look at the examples"
The MIME-Tools distribution comes with an \*(L"examples\*(R" directory.
The scripts in there are basically just tossed-together, but
they'll give you some ideas of how to use the parser.
.SS "Run with warnings enabled"
.IX Subsection "Run with warnings enabled"
\&\fIAlways\fR run your Perl script with \f(CW\*(C`\-w\*(C'\fR.
If you see a warning about a deprecated method, change your
code \s-1ASAP.\s0 This will ease upgrades tremendously.
.SS "Avoid non-standard encodings"
.IX Subsection "Avoid non-standard encodings"
Don't try to MIME-encode using the non-standard \s-1MIME\s0 encodings.
It's just not a good practice if you want people to be able to
read your messages.
.SS "Plan for thrown exceptions"
.IX Subsection "Plan for thrown exceptions"
For example, if your mail-handling code absolutely must not die,
then perform mail parsing like this:
.PP
.Vb 1
\& $entity = eval { $parser\->parse(\e*INPUT) };
.Ve
.PP
Parsing is a complex process, and some components may throw exceptions
if seriously-bad things happen. Since \*(L"seriously-bad\*(R" is in the
eye of the beholder, you're better off \fIcatching\fR possible exceptions
instead of asking me to propagate \f(CW\*(C`undef\*(C'\fR 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 \f(CW\*(C`eval{}\*(C'\fR is good for.
.SS "Check the parser results for warnings/errors"
.IX Subsection "Check the parser results for warnings/errors"
As of 5.3xx, the parser tries extremely hard to give you a
MIME::Entity. If there were any problems, it logs warnings/errors
to the underlying \*(L"results\*(R" object (see MIME::Parser::Results).
Look at that object after each parse.
Print out the warnings and errors, \fIespecially\fR if messages don't
parse the way you thought they would.
.SS "Don't plan on printing exactly what you parsed!"
.IX Subsection "Don't plan on printing exactly what you parsed!"
\&\fIParsing is a (slightly) lossy operation.\fR
Because of things like ambiguities in base64\-encoding, the following
is \fInot\fR going to spit out its input unchanged in all cases:
.PP
.Vb 2
\& $entity = $parser\->parse(\e*STDIN);
\& $entity\->print(\e*STDOUT);
.Ve
.PP
If you're using MIME::Tools to process email, remember to save
the data you parse if you want to send it on unchanged.
This is vital for things like PGP-signed email.
.SS "Understand how international characters are represented"
.IX Subsection "Understand how international characters are represented"
The \s-1MIME\s0 standard allows for text strings in headers to contain
characters from any character set, by using special sequences
which look like this:
.PP
.Vb 1
\& =?ISO\-8859\-1?Q?Keld_J=F8rn_Simonsen?=
.Ve
.PP
To be consistent with the existing Mail::Field classes, MIME::Tools
does \fInot\fR automatically unencode these strings, since doing so would
lose the character-set information and interfere with the parsing
of fields (see \*(L"decode_headers\*(R" in MIME::Parser for a full explanation).
That means you should be prepared to deal with these encoded strings.
.PP
The most common question then is, \fBhow do I decode these encoded strings?\fR
The answer depends on what you want to decode them \fIto\fR:
\&\s-1ASCII,\s0 Latin1, \s-1UTF\-8,\s0 etc. Be aware that your \*(L"target\*(R" representation
may not support all possible character sets you might encounter;
for example, Latin1 (\s-1ISO\-8859\-1\s0) has no way of representing Big5
(Chinese) characters. A common practice is to represent \*(L"untranslateable\*(R"
characters as \*(L"?\*(R"s, or to ignore them completely.
.PP
To unencode the strings into some of the more-popular Western byte
representations (e.g., Latin1, Latin2, etc.), you can use the decoders
in MIME::WordDecoder (see MIME::WordDecoder).
The simplest way is by using \f(CW\*(C`unmime()\*(C'\fR, a function wrapped
around your \*(L"default\*(R" decoder, as follows:
.PP
.Vb 3
\& use MIME::WordDecoder;
\& ...
\& $subject = unmime $entity\->head\->get(\*(Aqsubject\*(Aq);
.Ve
.PP
One place this \fIis\fR done automatically is in extracting the recommended
filename for a part while parsing. That's why you should start by
setting up the best \*(L"default\*(R" decoder if the default target of Latin1
isn't to your liking.
.SH "THINGS I DO THAT YOU SHOULD KNOW ABOUT"
.IX Header "THINGS I DO THAT YOU SHOULD KNOW ABOUT"
.SS "Fuzzing of \s-1CRLF\s0 and newline on input"
.IX Subsection "Fuzzing of CRLF and newline on input"
\&\s-1RFC 2045\s0 dictates that \s-1MIME\s0 streams have lines terminated by \s-1CRLF\s0
(\f(CW"\er\en"\fR). However, it is extremely likely that folks will want to
parse \s-1MIME\s0 streams where each line ends in the local newline
character \f(CW"\en"\fR instead.
.PP
An attempt has been made to allow the parser to handle both \s-1CRLF\s0
and newline-terminated input.
.SS "Fuzzing of \s-1CRLF\s0 and newline when decoding"
.IX Subsection "Fuzzing of CRLF and newline when decoding"
The \f(CW"7bit"\fR and \f(CW"8bit"\fR decoders will decode both
a \f(CW"\en"\fR and a \f(CW"\er\en"\fR end-of-line sequence into a \f(CW"\en"\fR.
.PP
The \f(CW"binary"\fR decoder (default if no encoding specified)
still outputs stuff verbatim... so a \s-1MIME\s0 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... \fIbut this is as it should be\fR.
.SS "Fuzzing of \s-1CRLF\s0 and newline when encoding/composing"
.IX Subsection "Fuzzing of CRLF and newline when encoding/composing"
\&\s-1TODO FIXME\s0
All encoders currently output the end-of-line sequence as a \f(CW"\en"\fR,
with the assumption that the local mail agent will perform
the conversion from newline to \s-1CRLF\s0 when sending the mail.
However, there probably should be an option to output \s-1CRLF\s0 as per \s-1RFC 2045\s0
.SS "Inability to handle multipart boundaries with embedded newlines"
.IX Subsection "Inability to handle multipart boundaries with embedded newlines"
Let's get something straight: this is an evil, \s-1EVIL\s0 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 \s-1MIME\s0 mail like this, regard it as syntactically
incorrect, which it is.
.SS "Ignoring non-header headers"
.IX Subsection "Ignoring non-header headers"
People like to hand the parser raw messages straight from
\&\s-1POP3\s0 or from a mailbox. There is often predictable non-header
information in front of the real headers; e.g., the initial
\&\*(L"From\*(R" line in the following message:
.PP
.Vb 3
\& From \- Wed Mar 22 02:13:18 2000
\& Return\-Path: <eryq@zeegee.com>
\& Subject: Hello
.Ve
.PP
The parser simply ignores such stuff quietly. Perhaps it
shouldn't, but most people seem to want that behavior.
.SS "Fuzzing of empty multipart preambles"
.IX Subsection "Fuzzing of empty multipart preambles"
Please note that there is currently an ambiguity in the way
preambles are parsed in. The following message fragments \fIboth\fR
are regarded as having an empty preamble (where \f(CW\*(C`\en\*(C'\fR indicates a
newline character):
.PP
.Vb 5
\& Content\-type: multipart/mixed; boundary="xyz"\en
\& Subject: This message (#1) has an empty preamble\en
\& \en
\& \-\-xyz\en
\& ...
\&
\& Content\-type: multipart/mixed; boundary="xyz"\en
\& Subject: This message (#2) also has an empty preamble\en
\& \en
\& \en
\& \-\-xyz\en
\& ...
.Ve
.PP
In both cases, the \fIfirst\fR completely-empty line (after the \*(L"Subject\*(R")
marks the end of the header.
.PP
But we should clearly ignore the \fIsecond\fR empty line in message #2,
since it fills the role of \fI\*(L"the newline which is only there to make
sure that the boundary is at the beginning of a line\*(R"\fR.
Such newlines are \fInever\fR part of the content preceding the boundary;
thus, there is no preamble \*(L"content\*(R" in message #2.
.PP
However, it seems clear that message #1 \fIalso\fR has no preamble
\&\*(L"content\*(R", and is in fact merely a compact representation of an
empty preamble.
.SS "Use of a temp file during parsing"
.IX Subsection "Use of a temp file during parsing"
\&\fIWhy not do everything in core?\fR
Although the amount of core available on even a modest home
system continues to grow, the size of attachments continues
to grow with it. I wanted to make sure that even users with small
systems could deal with decoding multi-megabyte sounds and movie files.
That means not being core-bound.
.PP
As of the released 5.3xx, MIME::Parser gets by with only
one temp file open per parser. This temp file provides
a sort of infinite scratch space for dealing with the current
message part. It's fast and lightweight, but you should know
about it anyway.
.SS "Why do I assume that \s-1MIME\s0 objects are email objects?"
.IX Subsection "Why do I assume that MIME objects are email objects?"
Achim Bohnet once pointed out that \s-1MIME\s0 headers do nothing more than
store a collection of attributes, and thus could be represented as
objects which don't inherit from Mail::Header.
.PP
I agree in principle, but \s-1RFC 2045\s0 says otherwise.
\&\s-1RFC 2045\s0 [\s-1MIME\s0] headers are a syntactic subset of \s-1RFC\-822\s0 [email] headers.
Perhaps a better name for these modules would have been \s-1RFC1521::\s0
instead of \s-1MIME::,\s0 but we're a little beyond that stage now.
.PP
When I originally wrote these modules for the \s-1CPAN, I\s0 agonized for a long
time about whether or not they really should subclass from \fBMail::Internet\fR
(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.
.SH "A MIME PRIMER"
.IX Header "A MIME PRIMER"
So you need to parse (or create) \s-1MIME,\s0 but you're not quite up on
the specifics? No problem...
.SS "Glossary"
.IX Subsection "Glossary"
Here are some definitions adapted from \s-1RFC 1521\s0 (predecessor of the
current \s-1RFC\s0 204[56789] defining \s-1MIME\s0) explaining the terminology we
use; each is accompanied by the equivalent in \s-1MIME::\s0 module terms...
.IP "attachment" 4
.IX Item "attachment"
An \*(L"attachment\*(R" is common slang for any part of a multipart message \*(--
except, perhaps, for the first part, which normally carries a user
message describing the attachments that follow (e.g.: \*(L"Hey dude, here's
that \s-1GIF\s0 file I promised you.\*(R").
.Sp
In our system, an attachment is just a \fBMIME::Entity\fR under the
top-level entity, probably one of its parts.
.IP "body" 4
.IX Item "body"
The \*(L"body\*(R" of an entity is that portion of the entity
which follows the header and which contains the real message
content. For example, if your \s-1MIME\s0 message has a \s-1GIF\s0 file attachment,
then the body of that attachment is the base64\-encoded \s-1GIF\s0 file itself.
.Sp
A body is represented by an instance of \fBMIME::Body\fR. You get the
body of an entity by sending it a \fBbodyhandle()\fR
message.
.IP "body part" 4
.IX Item "body part"
One of the parts of the body of a multipart \fB/entity\fR.
A body part has a \fB/header\fR and a \fB/body\fR, so it makes sense to
speak about the body of a body part.
.Sp
Since a body part is just a kind of entity, it's represented by
an instance of \fBMIME::Entity\fR.
.IP "entity" 4
.IX Item "entity"
An \*(L"entity\*(R" means either a \fB/message\fR or a \fB/body part\fR.
All entities have a \fB/header\fR and a \fB/body\fR.
.Sp
An entity is represented by an instance of \fBMIME::Entity\fR.
There are instance methods for recovering the
header (a \fBMIME::Head\fR) and the
body (a \fBMIME::Body\fR).
.IP "header" 4
.IX Item "header"
This is the top portion of the \s-1MIME\s0 message, which contains the
\&\*(L"Content-type\*(R", \*(L"Content-transfer-encoding\*(R", etc. Every \s-1MIME\s0 entity has
a header, represented by an instance of \fBMIME::Head\fR. You get the
header of an entity by sending it a \fBhead()\fR message.
.IP "message" 4
.IX Item "message"
A \*(L"message\*(R" generally means the complete (or \*(L"top-level\*(R") message being
transferred on a network.
.Sp
There currently is no explicit package for \*(L"messages\*(R"; under \s-1MIME::,\s0
messages are streams of data which may be read in from files or
filehandles. You can think of the \fBMIME::Entity\fR returned by the
\&\fBMIME::Parser\fR as representing the full message.
.SS "Content types"
.IX Subsection "Content types"
This indicates what kind of data is in the \s-1MIME\s0 message, usually
as \fImajortype/minortype\fR. The standard major types are shown below.
A more-comprehensive listing may be found in \s-1RFC\-2046.\s0
.IP "application" 4
.IX Item "application"
Data which does not fit in any of the other categories, particularly
data to be processed by some type of application program.
\&\f(CW\*(C`application/octet\-stream\*(C'\fR, \f(CW\*(C`application/gzip\*(C'\fR, \f(CW\*(C`application/postscript\*(C'\fR...
.IP "audio" 4
.IX Item "audio"
Audio data.
\&\f(CW\*(C`audio/basic\*(C'\fR...
.IP "image" 4
.IX Item "image"
Graphics data.
\&\f(CW\*(C`image/gif\*(C'\fR, \f(CW\*(C`image/jpeg\*(C'\fR...
.IP "message" 4
.IX Item "message"
A message, usually another mail or \s-1MIME\s0 message.
\&\f(CW\*(C`message/rfc822\*(C'\fR...
.IP "multipart" 4
.IX Item "multipart"
A message containing other messages.
\&\f(CW\*(C`multipart/mixed\*(C'\fR, \f(CW\*(C`multipart/alternative\*(C'\fR...
.IP "text" 4
.IX Item "text"
Textual data, meant for humans to read.
\&\f(CW\*(C`text/plain\*(C'\fR, \f(CW\*(C`text/html\*(C'\fR...
.IP "video" 4
.IX Item "video"
Video or video+audio data.
\&\f(CW\*(C`video/mpeg\*(C'\fR...
.SS "Content transfer encodings"
.IX Subsection "Content transfer encodings"
This is how the message body is packaged up for safe transit.
There are the 5 major \s-1MIME\s0 encodings.
A more-comprehensive listing may be found in \s-1RFC\-2045.\s0
.IP "7bit" 4
.IX Item "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 \s-1CRLF\s0).
.IP "8bit" 4
.IX Item "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 \s-1CRLF\s0).
.IP "binary" 4
.IX Item "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 \fIleast\fR likely to get through mail
gateways.
.IP "base64" 4
.IX Item "base64"
A standard encoding, which maps arbitrary binary data to the 7bit domain.
Like \*(L"uuencode\*(R", but very well-defined. This is how you should send
essentially binary information (tar files, GIFs, JPEGs, etc.).
.IP "quoted-printable" 4
.IX Item "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).
.SH "SEE ALSO"
.IX Header "SEE ALSO"
MIME::Parser, MIME::Head, MIME::Body, MIME::Entity, MIME::Decoder, Mail::Header,
Mail::Internet
.PP
At the time of this writing, the MIME-tools homepage was
\&\fIhttp://www.mimedefang.org/static/mime\-tools.php\fR. Check there for
updates and support.
.PP
The \s-1MIME\s0 format is documented in RFCs 1521\-1522, and more recently
in RFCs 2045\-2049.
.PP
The \s-1MIME\s0 header format is an outgrowth of the mail header format
documented in \s-1RFC 822.\s0
.SH "SUPPORT"
.IX Header "SUPPORT"
Please file support requests via rt.cpan.org.
.SH "CHANGE LOG"
.IX Header "CHANGE LOG"
Released as MIME-parser (1.0): 28 April 1996.
Released as MIME-tools (2.0): Halloween 1996.
Released as MIME-tools (4.0): Christmas 1997.
Released as MIME-tools (5.0): Mother's Day 2000.
.PP
See ChangeLog file for full details.
.SH "AUTHOR"
.IX Header "AUTHOR"
Eryq (\fIeryq@zeegee.com\fR), ZeeGee Software Inc (\fIhttp://www.zeegee.com\fR).
Dianne Skoll (\fIdfs@roaringpenguin.com\fR) \fIhttp://www.roaringpenguin.com\fR.
.PP
Copyright (c) 1998, 1999 by ZeeGee Software Inc (www.zeegee.com).
Copyright (c) 2004 by Roaring Penguin Software Inc (www.roaringpenguin.com)
.PP
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
.PP
See the \s-1COPYING\s0 file in the distribution for details.
.SH "ACKNOWLEDGMENTS"
.IX Header "ACKNOWLEDGMENTS"
\&\fBThis kit would not have been possible\fR but for the direct
contributions of the following:
.PP
.Vb 9
\& 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.
.Ve
.PP
Not to mention the Accidental Beta Test Team, whose bug reports (and
comments) have been invaluable in improving the whole:
.PP
.Vb 10
\& Phil Abercrombie
\& Mike Blazer
\& 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
.Ve
.PP
Please forgive me if I've accidentally left you out.
Better yet, email me, and I'll put you in.
.SH "LICENSE"
.IX Header "LICENSE"
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
.PP
See the \s-1COPYING\s0 file for more details.