@node MIME/Entity, MIME/Field/ConTraEnc, MIME/Decoder, Module List
@unnumbered class for parsed-and-decoded MIME message


@unnumberedsec NAME

MIME::Entity - class for parsed-and-decoded MIME message

@unnumberedsec SYNOPSIS

Create a MIME entity from an array, and output it as a MIME stream to STDOUT:

@example
$ent = new MIME::Entity [
			  "Subject: Greetings\n",
			  "Content-type: text/plain\n",
			  "Content-transfer-encoding: 7bit\n",
			  "\n",
			  "Hi there!\n", 
			  "Bye there!\n"
    		  ];
$ent->print(\*STDOUT);
@end example

Create a document for an ordinary 7-bit ASCII text file (lots of 
stuff is defaulted for us):

@example
$ent = build MIME::Entity Path=>"english-msg.txt";
@end example

Create a document for a text file with 8-bit (Latin-1) characters:

@example
$ent = build MIME::Entity Path     =>"french-msg.txt",
                          Encoding =>"quoted-printable";
@end example

Create a document for a GIF file (the description is completely optional,
and note that we have to specify content-type and encoding since they@'re
not the default values):

@example
$ent = build MIME::Entity Description => "A pretty picture",
                          Path        => "./docs/mime-sm.gif",
                          Type        => "image/gif",
                          Encoding    => "base64";
@end example

Create a document that you already have the text for:

@example
$ent = build MIME::Entity  Type        => "text/plain",
                           Encoding    => "quoted-printable",
                           Data        => [
                                 "First line.\n",
                                 "Second line.\n",
                                 "Last line.\n",
                           ];
@end example

Create a multipart message (could it @emph{be} much easier?)

@example
# 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";
 
# Attachment #3: text we@'ll create with text we have on-hand:
attach $top Data=>$contents;

# Output!
$top->print(\*STDOUT);
@end example

Extract information from MIME entities:

@example
# Get the head, a MIME::Head:
$head = $ent->head;

# Get the body, as a MIME::Body;
$bodyh = $ent->bodyhandle;
@end example

See MIME::Parser for additional examples of usage.

@unnumberedsec DESCRIPTION

A subclass of @strong{Mail::Internet}.

This package provides a class for representing MIME message entities,
as specified in RFC 1521, @emph{Multipurpose Internet Mail Extensions}.

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

@table @asis
@item Message
From RFC-1521:

@example
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".
@end example

There currently is no explicit package for messages; under MIME::, 
messages may be read in from readable files or filehandles.
A future extension will allow them to be read from any object 
reference that responds to a special "next line" method.

@item Body part
From RFC-1521:

@example
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.
@end example

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

@item Entity
From RFC-1521:

@example
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.
@end example

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

@item Body
From RFC-1521:

@example
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.
@end example

Well, this is a toughie.  Both Mail::Internet (1.17) and Mail::MIME (1.03)
represent message bodies in-core; unfortunately, this is not always the
best way to handle things, especially for MIME streams that contain
multi-megabyte tar files.

@end table
@unnumberedsec PUBLIC INTERFACE

@unnumberedsubsec Constructors and converters

@table @asis
@item new [SOURCE]
@emph{Class method.}
Create a new, empty MIME entity.
Basically, this uses the Mail::Internet constructor...

If SOURCE is an ARRAYREF, it is assumed to be an array of lines
that will be used to create both the header and an in-core body.

Else, if SOURCE is defined, it is assumed to be a filehandle
from which the header and in-core body is to be read. 

@strong{Note:} in either case, the body will not be @emph{parsed:} merely read!

@item build PARAMHASH
@emph{Class/instance method.}
A quick-and-easy catch-all way to create an entity.  Use it like this
to build a "normal" single-part entity:

@example
$ent = build MIME::Entity Type     => "image/gif",
		             Encoding => "base64",
                          Path     => "/path/to/xyz12345.gif",
                          Filename => "saveme.gif";
@end example

And like this to build a "multipart" entity:

@example
$ent = build MIME::Entity Type     => "multipart/mixed",
                          Boundary => "---1234567";
@end example

A minimal MIME header will be created.  The params are:

@table @asis
@item Boundary
@emph{Multipart entities only. Optional.}  
The boundary string.
If you omit this, a random string will be chosen... which is probably safer.

@item Data
@emph{Single-part entities only. Optional.}  
An alternative to Path (q.v.): the actual data, either as a scalar
or an array reference (whose elements are joined together to make
the actual scalar).  The body is opened on the data using 
MIME::Body::Scalar.

@item Description
@emph{Optional.}  
The text of the content-description.  
If you don@'t specify it, the field is not put in the header.

@item Encoding
@emph{Optional.}  
The content-transfer-encoding.
If you don@'t specify it, the field is not put in the header... 
which means that the encoding implicitly defaults to @code{"7bit"} as per 
RFC-1521.  @emph{Do yourself a favor: put it in.}

@item Filename
@emph{Single-part entities only. Optional.}  
The recommended filename.  Overrides any name extracted from Path.
The information is stored both the deprecated (content-type) and
preferred (content-disposition) locations.

@item Path
@emph{Single-part entities only. Optional.}  
The path to the file to attach.  The body is opened on that file
using MIME::Body::File.

@item Top
@emph{Optional.}  
Is this a top-level entity?  If so, it must sport a MIME-Version.
The default is true.

@item Type
@emph{Optional.}  
The content-type. 
If you don@'t specify it, it defaults to @code{"text/plain"} 
as per RFC-1521.  @emph{Do yourself a favor: put it in.}

@end table
@end table
@unnumberedsubsec Instance methods

@table @asis
@item add_part ENTITY
Assuming we are a multipart message, add a body part (a MIME::Entity)
to the array of body parts.  Do @strong{not} call this for single-part messages;
i.e., don@'t call it unless the header has a @code{"multipart"} content-type.

Returns the part that was just added.

@item attach PARAMHASH
The real quick-and-easy way to create multipart messages.
Basically equivalent to:

@example
$entity->add_part(ref($entity)->build(PARAMHASH));
@end example

Except that it@'s a lot nicer to look at.

@item body [VALUE]
@table @asis
@item @strong{If emulating version 1.x:}
Get or set the path to the file containing the body.

If @code{VALUE} @emph{is not} given, the current body file is returned.
If @code{VALUE} @emph{is} given, the body file is set to the new value,
and the previous value is returned.

@item Otherwise:
Get or set the body, as an array of lines.  This should be regarded
as a read-only data structure: changing its contents will have 
unpredictable results (you can, of course, make your own copy,
and work with that).  

Provided for compatibility with Mail::Internet, and it might not 
be as efficient as you@'d like.  Also, it@'s somewhat silly/wrongheaded
for binary bodies, like GIFs and tar files.

@end table
Both forms are deprecated for MIME entities: instead, use the bodyhandle()
method to get and use a MIME::Body.  The content-type of the entity
will tell you whether that body is best read as text (via getline())
or raw data (via read()).

@item bodyhandle [VALUE]
Get or set an abstract object representing the body.

If @code{VALUE} @emph{is not} given, the current bodyhandle is returned.
If @code{VALUE} @emph{is} given, the bodyhandle is set to the new value,
and the previous value is returned.

@item dump_skeleton [FILEHANDLE]
Dump the skeleton of the entity to the given FILEHANDLE, or
to the currently-selected one if none given.  This is really
just useful for debugging purposes.

@item head [VALUE]
Get/set the head. 

If there is no VALUE given, returns the current head.  If none
exists, an empty instance of MIME::Head is created, set, and returned.

@strong{Note:} This is a patch over a bug in Mail::Internet, which doesn@'t 
provide a method for setting the head to some given object.

@item is_multipart
Does this entity@'s MIME type indicate that it@'s a multipart entity?
Returns undef (false) if the answer couldn@'t be determined, 0 (false)
if it was determined to be false, and true otherwise.

Note that this says nothing about whether or not parts were extracted.

@item mime_type
A purely-for-convenience method.  This simply relays the
request to the associated MIME::Head object.  The following
are identical:

@example
$x = $entity->mime_type;

$x = $entity->head->mime_type;
@end example

If there is no head, returns undef in a scalar context and
the empty array in a list context.

Note that, while parsed entities still have MIME types, they 
do not have MIME encodings, or MIME versions, or fields, etc., etc... 
for those attributes, you still have to go to the head explicitly.

@item parts
Return an array of all sub parts (each of which is a MIME::Entity), 
or the empty array if there are none.

For single-part messages, the empty array will be returned.
For multipart messages, the preamble and epilogue parts are @emph{not} in the 
list!

Note that in a scalar context, this returns you the number of parts.

@item print [FILEHANDLE]
@emph{Instance method, override.}
Print the entity to the given FILEHANDLE, or to the currently-selected
one if none given.  

@strong{If a single-part entity,}
the header and the body are both output, with the body being output
according to the encoding specified by the header.

@strong{If a multipart entity,}
this is invoked recursively on all its parts, with appropriate
boundaries and a preamble generated for you.

See @code{print_body()} for an important note on how the body is output.

@item print_body [FILEHANDLE]
@emph{Instance method, override.}
Print the body of the entity to the given FILEHANDLE, or to the 
currently-selected one if none given.  

@strong{Important note:} the body is output according to the encoding specified 
by  the header (@code{@'binary@'} if no encoding given).  This means that the
following code:

@example
$ent = new MIME::Entity ["Subject: Greetings\n",
			     "Content-transfer-encoding: base64\n",
			     "\n",
			     "Hi there!\n", 
			     "Bye there!\n"
			     ];
$ent->print;   # uses print_body() internally
@end example

Prints this:

@example
Subject: Greetings
Content-transfer-encoding: base64
@end example

@example
SGkgdGhlcmUhCkJ5ZSB0aGVyZSEK
@end example

The body is @emph{stored} in an unencoded form; however, the idea is that
the transfer encoding is used to determine how it should be @emph{output.}
This means that the @code{print()} method is always guaranteed to get you
a sendmail-ready stream whose body is consistent with its head.

If you want the raw body data to be output, you can either read it from
the bodyhandle yourself, or use:

@example
$ent->bodyhandle->print;
@end example

which uses read() calls to extract the information, and thus will 
work with both text and binary bodies.

@end table
@unnumberedsec NOTES

@unnumberedsubsec Under the hood

A @strong{MIME::Entity} is composed of the following elements:

@itemize @bullet
@item 
A head, which is a reference to a MIME::Head object
containing the header information.

@item 
A bodyhandle, which is a reference a MIME::Body object
containing the decoded body data.
(In pre-2.0 releases, this was accessed via body, 
which was a path to a file containing the decoded body.
Integration with Mail::Internet has forced this to change.)

@item 
A list of zero or more parts, each of which is a MIME::Entity 
object.  The number of parts will only be nonzero if the content-type 
is some subtype of @code{"multipart"}.

Note that, in 2.0+, a multipart entity does @emph{not} have a body.
Of course, any/all of its component parts can have bodies.

@end itemize
@unnumberedsubsec Design issues

@table @asis
@item Some things just can@'t be ignored
In multipart messages, the @emph{"preamble"} is the portion that precedes
the first encapsulation boundary, and the @emph{"epilogue"} is the portion
that follows the last encapsulation boundary.

According to RFC-1521:

@example
There appears to be room for additional information prior to the
first encapsulation boundary and following the final boundary.  These
areas should generally be left blank, and implementations must ignore
anything that appears before the first boundary or after the last one.
@end example

@example
NOTE: These "preamble" and "epilogue" areas are generally not used
because of the lack of proper typing of these parts and the lack
of clear semantics for handling these areas at gateways,
particularly X.400 gateways.  However, rather than leaving the
preamble area blank, many MIME implementations have found this to
be a convenient place to insert an explanatory note for recipients
who read the message with pre-MIME software, since such notes will
be ignored by MIME-compliant software.
@end example

In the world of standards-and-practices, that@'s the standard.  
Now for the practice: 

@emph{Some "MIME" mailers may incorrectly put a "part" in the preamble}.
Since we have to parse over the stuff @emph{anyway}, in the future I
@emph{may} allow the parser option of creating special MIME::Entity objects 
for the preamble and epilogue, with bogus MIME::Head objects.

For now, though, we@'re MIME-compliant, so I probably won@'t change
how we work.

@end table
@unnumberedsec SEE ALSO

MIME::Decoder,
MIME::Entity,
MIME::Head, 
MIME::Parser.

@unnumberedsec AUTHOR

Copyright (c) 1996 by Eryq / eryq@@rhine.gsfc.nasa.gov

All rights reserved.  This program is free software; you can redistribute 
it and/or modify it under the same terms as Perl itself.

@unnumberedsec VERSION

$Revision: 2.8 $ $Date: 1996/11/03 00:19:30 $

