
=head1 NAME

Mail::Message - general message object


=head1 INHERITANCE

 Mail::Message has extra code in
   Mail::Message::Construct::Read
   Mail::Message::Construct::Forward
   Mail::Message::Construct::Bounce
   Mail::Message::Construct
   Mail::Message::Construct::Reply
   Mail::Message::Construct::Build
   Mail::Message::Construct::Text
   Mail::Message::Construct::Rebuild

 Mail::Message
   is a Mail::Reporter

 Mail::Message is extended by
   Mail::Box::Message
   Mail::Message::Dummy
   Mail::Message::Part
   Mail::Message::Replace::MailInternet


=head1 SYNOPSIS

 use Mail::Box::Manager;
 my $mgr    = Mail::Box::Manager->new;
 my $folder = $mgr->open(folder => 'InBox');
 my $msg    = $folder->message(2);    # $msg is a Mail::Message now

 my $subject = $msg->subject;         # The message's subject
 my @cc      = $msg->cc;              # List of Mail::Address'es

 my $msg       = Mail::Message->build(...);
 my $reply_msg = Mail::Message->reply(...);
 my $frwd_msg  = Mail::Message->forward(...);

 my Mail::Message::Head $head = $msg->head;
 my Mail::Message::Body $body = $msg->decoded;
 $msg->decoded->print($outfile);


=head1 DESCRIPTION

A C<Mail::Message> object is a container for MIME-encoded message information,
as defined by RFC2822.  Everything what is not specificly related to storing
the messages in mailboxes (folders) is implemented in this class.  Methods
which are related to folders is implemented in the L<Mail::Box::Message|Mail::Box::Message>
extension.

The main methods are L<get()|Mail::Message/"The header">, to get information from a message header
field, and L<decoded()|Mail::Message/"The body"> to get the intended content of a message.
But there are many more which can assist your program.

Complex message handling, like construction of replies and forwards, are
implemented in separate packages which are autoloaded into this class.
This means you can simply use these methods as if they are part of this class.
Those package add functionality to all kinds of message objects.



=head1 METHODS


=head2 Constructors


$obj-E<gt>B<clone>(OPTIONS)

=over 4

Create a copy of this message.  Returned is a C<Mail::Message> object.
The head and body, the log and trace levels are taken.  Labels are
copied with the message, but the delete and modified flags are not.
 
BE WARNED: the clone of any kind of message (or a message part)
will B<always> be a C<Mail::Message> object.  For example, a
L<Mail::Box::Message|Mail::Box::Message>'s clone is detached from the folder of its original.
When you use L<Mail::Box::addMessage()|Mail::Box/"The folder"> with the cloned message at hand,
then the clone will automatically be coerced into the right message type
to be added.

See also L<Mail::Box::Message::copyTo()|Mail::Box::Message/"The message"> and L<Mail::Box::Message::moveTo()|Mail::Box::Message/"The message">.

 Option        Defined in       Default
 shallow                        <false>
 shallow_body                   <false>
 shallow_head                   <false>

. shallow BOOLEAN

=over 4

When a shallow clone is made, the header and body of the message will not
be cloned, but shared.  This is quite dangerous: for instance in some
folder types, the header fields are used to store folder flags.  When
one of both shallow clones change the flags, that will update the header
and thereby be visible in both.

There are situations where a shallow clone can be used safely.  For instance,
when L<Mail::Box::Message::moveTo()|Mail::Box::Message/"The message"> is used and you are sure that the
original message cannot get undeleted after the move.

=back

. shallow_body BOOLEAN

=over 4

A rather safe bet, because you are not allowed to modify the body of a
message: you may only set a new body with L<body()|Mail::Message/"The body">.

=back

. shallow_head BOOLEAN

=over 4

Only the head uses is reused, not the body.  This is probably a bad choice,
because the header fields can be updated, for instance when labels change.

=back

I<Example:> 

 $copy = $msg->clone;

=back

Mail::Message-E<gt>B<new>(OPTIONS)

=over 4

 Option      Defined in       Default                                                       
 body                         undef                                                         
 body_type                    L<Mail::Message::Body::Lines|Mail::Message::Body::Lines>      
 deleted                      <false>                                                       
 field_type                   undef                                                         
 head                         undef                                                         
 head_type                    L<Mail::Message::Head::Complete|Mail::Message::Head::Complete>
 labels                       {}                                                            
 log         L<Mail::Reporter>  C<'WARNINGS'>                                                 
 messageId                    undef                                                         
 modified                     <false>                                                       
 trace       L<Mail::Reporter>  C<'WARNINGS'>                                                 
 trusted                      <false>                                                       

. body OBJECT

=over 4

Instantiate the message with a body which has been created somewhere
before the message is constructed.  The OBJECT must be a sub-class
of Mail::Message::Body.  See also L<body()|Mail::Message/"The body"> and L<storeBody()|Mail::Message/"Internals">.

=back

. body_type CLASS

=over 4

Default type of body to be created for L<readBody()|Mail::Message/"Internals">.

=back

. deleted BOOLEAN

=over 4

Is the file deleted from the start?

=back

. field_type CLASS

. head OBJECT

=over 4

Instantiate the message with a head which has been created somewhere
before the message is constructed.  The OBJECT must be a (sub-)class
of L<Mail::Message::Head|Mail::Message::Head>. See also L<head()|Mail::Message/"The header">.

=back

. head_type CLASS

=over 4

Default type of head to be created for L<readHead()|Mail::Message/"Internals">.

=back

. labels ARRAY|HASH

=over 4

Initial values of the labels.  In case of L<Mail::Box::Message|Mail::Box::Message>'s, this
shall reflect the state the message is in.  For newly constructed
L<Mail::Message|Mail::Message>'s, this may be anything you want, because L<coerce()|Mail::Message/"Internals">
will take care of the folder specifics once the message is added to one.

=back

. log LEVEL

. messageId STRING

=over 4

The id on which this message can be recognized.  If none specified and
not defined in the header --but one is needed-- there will be one assigned
to the message to be able to pass unique message-ids between objects.

=back

. modified BOOLEAN

=over 4

Flags this message as being modified from the beginning on.  Usually,
modification is auto-detected, but there may be reasons to be extra
explicit.

=back

. trace LEVEL

. trusted BOOLEAN

=over 4

Is this message from a trusted source?  If not, the content must be
checked before use.  This checking will be performed when the
body data is decoded or used for transmission.

=back

=back

=head2 Constructing a message


$obj-E<gt>B<bounce>([RG-OBJECT|OPTIONS])

=over 4

See L<Mail::Message::Construct::Bounce/"Constructing a message">

=back

Mail::Message-E<gt>B<build>([MESSAGE|PART|BODY], CONTENT)

=over 4

See L<Mail::Message::Construct::Build/"Constructing a message">

=back

Mail::Message-E<gt>B<buildFromBody>(BODY, [HEAD], HEADERS)

=over 4

See L<Mail::Message::Construct::Build/"Constructing a message">

=back

$obj-E<gt>B<forward>(OPTIONS)

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

$obj-E<gt>B<forwardAttach>(OPTIONS)

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

$obj-E<gt>B<forwardEncapsulate>(OPTIONS)

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

$obj-E<gt>B<forwardInline>(OPTIONS)

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

$obj-E<gt>B<forwardNo>(OPTIONS)

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

$obj-E<gt>B<forwardPostlude>

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

$obj-E<gt>B<forwardPrelude>

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

$obj-E<gt>B<forwardSubject>(STRING)

=over 4

See L<Mail::Message::Construct::Forward/"Constructing a message">

=back

Mail::Message-E<gt>B<read>(FILEHANDLE|SCALAR|REF-SCALAR|ARRAY-OF-LINES, OPTIONS)

=over 4

See L<Mail::Message::Construct::Read/"Constructing a message">

=back

$obj-E<gt>B<rebuild>(OPTIONS)

=over 4

See L<Mail::Message::Construct::Rebuild/"Constructing a message">

=back

$obj-E<gt>B<reply>(OPTIONS)

=over 4

See L<Mail::Message::Construct::Reply/"Constructing a message">

=back

$obj-E<gt>B<replyPrelude>([STRING|FIELD|ADDRESS|ARRAY-OF-THINGS])

=over 4

See L<Mail::Message::Construct::Reply/"Constructing a message">

=back

$obj-E<gt>B<replySubject>(STRING)

Mail::Message-E<gt>B<replySubject>(STRING)

=over 4

See L<Mail::Message::Construct::Reply/"Constructing a message">

=back

=head2 The message


$obj-E<gt>B<container>

=over 4

If the message is a part of another message, C<container> returns the
reference to the containing body.

I<Example:> 

 my Mail::Message $msg = ...
 return unless $msg->body->isMultipart;
 my $part   = $msg->body->part(2);

 return unless $part->body->isMultipart;
 my $nested = $part->body->part(3);

 $nested->container;  # returns $msg->body
 $nested->toplevel;   # returns $msg
 $msg->container;     # returns undef
 $msg->toplevel;      # returns $msg
 $msg->isPart;        # returns false
 $part->isPart;       # returns true

=back

$obj-E<gt>B<isDummy>

=over 4

Dummy messages are used to fill holes in linked-list and such, where only
a message-id is known, but not the place of the header of body data.

This method is also available for L<Mail::Message::Dummy|Mail::Message::Dummy> objects,
where this will return C<true>.  On any extension of C<Mail::Message>,
this will return C<false>.

=back

$obj-E<gt>B<isPart>

=over 4

Returns true if the message is a part of another message.  This is
the case for L<Mail::Message::Part|Mail::Message::Part> extensions of C<Mail::Message>.

=back

$obj-E<gt>B<messageId>

=over 4

Retrieve the message's id.  Every message has a unique message-id.  This id
is used mainly for recognizing discussion threads.

=back

$obj-E<gt>B<print>([FILEHANDLE])

=over 4

Print the message to the FILE-HANDLE, which defaults to the selected
filehandle, without the encapsulation sometimes required by a folder
type, like L<write()|Mail::Message/"The message"> does.

I<Example:> 

 $message->print(\*STDERR);  # to the error output
 $message->print;            # to the selected file

 my $out = IO::File->new('out', 'w');
 $message->print($out);      # no encapsulation: no folder
 $message->write($out);      # with encapsulation: is folder.

=back

$obj-E<gt>B<send>([MAILER], OPTIONS)

=over 4

Transmit the message to anything outside this Perl program.  MAILER
is a L<Mail::Transport::Send|Mail::Transport::Send> object.  When the MAILER is not specified, one
will be created, and kept as default for the next messages as well.

The OPTIONS are mailer specific, and a mixture of what is usable for
the creation of the mailer object and the sending itself.  Therefore, see
for possible options L<Mail::Transport::Send::new()|Mail::Transport::Send/"METHODS"> and
L<Mail::Transport::Send::send()|Mail::Transport::Send/"Sending mail">.

I<Example:> 

 $message->send;

is short (but little less flexibile) for

 my $mailer = Mail::Transport::SMTP->new(@smtpopts);
 $mailer->send($message, @sendopts);

See examples/send.pl in the distribution of L<Mail::Box|Mail::Box>.

I<Example:> 

 $message->send(via => 'sendmail')

=back

$obj-E<gt>B<size>

=over 4

Returns an estimated size of the whole message in bytes.  In many occasions,
the functions which process the message further, for instance L<send()|Mail::Message/"The message">
or L<print()|Mail::Message/"The message"> will need to add/change header lines or add CR characters,
so the size is only an estimate with a few percent margin of the real
result.

The computation assumes that each line ending is represented by one
character (like UNIX, MacOS, and sometimes Cygwin), and not two characters
(like Windows and sometimes Cygwin).  If you write the message to file on
a system which uses CR and LF to end a single line (all Windows versions),
the result in that file will be at least L<nrLines()|Mail::Message/"The header"> larger than this
method returns.

=back

$obj-E<gt>B<toplevel>

=over 4

Returns a reference to the main message, which will be the current
message if the message is not part of another message.

=back

$obj-E<gt>B<write>([FILEHANDLE])

=over 4

Write the message to the FILE-HANDLE, which defaults to the selected
FILEHANDLE, with all surrounding information which is needed to put
it correctly in a folder file.

In most cases, the result of C<write> will be the same as with L<print()|Mail::Message/"The message">.
The main exception is for Mbox folder messages, which will get printed
with their leading 'From ' line and a trailing blank.  Each line of
their body which starts with 'From ' will have an 'E<gt>' added in front.

=back

=head2 The header


$obj-E<gt>B<bcc>

=over 4

Returns the addresses which are specified on the C<Bcc> header line (or lines)
A list of Mail::Address objects is returned.
C<Bcc> stands for I<Blind Carbon Copy>: destinations of the message which are
not listed in the messages actually sent.  So, this field will be empty
for received messages, but may be present in messages you construct yourself.

=back

$obj-E<gt>B<cc>

=over 4

Returns the addresses which are specified on the C<Cc> header line (or lines)
A list of Mail::Address objects is returned.  C<Cc> stands for
I<Carbon Copy>; the people addressed on this line receive the message
informational, and are usually not expected to reply on its content.

=back

$obj-E<gt>B<date>

=over 4

Method has been removed for reasons of consistency.  Use L<timestamp()|Mail::Message/"The header">
or C<$msg->head->get('Date')>.

=back

$obj-E<gt>B<destinations>

=over 4

Returns a list of Mail::Address objects which contains the combined
info of active C<To>, C<Cc>, and C<Bcc> addresses.  Double addresses are
removed if detectable.

=back

$obj-E<gt>B<from>

=over 4

Returns the addresses from the senders.  It is possible to have more than
one address specified in the C<From> field of the message, according
to the specification. Therefore a list of Mail::Address objects is
returned, which usually has length 1.

If you need only one address from a sender, for instance to create a
"original message by" line in constructed forwarded message body, then use
L<sender()|Mail::Message/"The header">.

I<Example:> using from() to get all sender addresses


 my @from = $message->from;

=back

$obj-E<gt>B<get>(FIELDNAME)

=over 4

Returns the value which is stored in the header field with the specified
name.  The FIELDNAME is case insensitive.  The I<unfolded body> of the
field is returned, stripped from any attributes.
See L<Mail::Message::Field::body()|Mail::Message::Field/"Access to the body">.

If the field has multiple appearances in the header, only the last
instance is returned.  If you need more complex handing of fields, then
call L<Mail::Message::Head::get()|Mail::Message::Head/"Access to the header"> yourself.  See L<study()|Mail::Message/"The header"> when you
want to be smart, doing the better (but slower) job.

I<Example:> the get() short-cut for header fields


 print $msg->get('Content-Type'), "\n";

Is equivalent to:

 print $msg->head->get('Content-Type')->body, "\n";

=back

$obj-E<gt>B<guessTimestamp>

=over 4

Return an estimate on the time this message was sent.  The data is
derived from the header, where it can be derived from the C<date> and
C<received> lines.  For MBox-like folders you may get the date from
the from-line as well.

This method may return C<undef> if the header is not parsed or only
partially known.  If you require a time, then use the L<timestamp()|Mail::Message/"The header">
method, described below.

I<Example:> using guessTimestamp() to get a transmission date


 print "Receipt ", ($message->timestamp || 'unknown'), "\n";

=back

$obj-E<gt>B<head>([HEAD])

=over 4

Return (optionally after setting) the HEAD of this message.
The head must be an (sub-)class of L<Mail::Message::Head|Mail::Message::Head>.
When the head is added, status information is taken from it
and transformed into labels.  More labels can be added by the
LABELS hash.  They are added later.

I<Example:> 

 $msg->head(Mail::Message::Head->new);  # set
 my $head = $msg->head;                 # get

=back

$obj-E<gt>B<nrLines>

=over 4

Returns the number of lines used for the whole message.

=back

$obj-E<gt>B<sender>

=over 4

Returns exactly one address, which is the originator of this message.
The returned Mail::Address object is taken from the C<Sender> header
field, unless that field does not exists, in which case the first
address from the C<From> field is taken.  If none of both provide
an address, C<undef> is returned.

I<Example:> using sender() to get exactly one sender address


 my $sender = $message->sender;
 print "Reply to: ", $sender->format, "\n" if defined $sender;

=back

$obj-E<gt>B<study>(FIELDNAME)

=over 4

Study the content of a field, like L<get()|Mail::Message/"The header"> does, with as main difference
that a L<Mail::Message::Field::Full|Mail::Message::Field::Full> object is returned.  These objects
stringify to an utf8 decoded representation of the data contained in
the field, where L<get()|Mail::Message/"The header"> does not decode.
See L<Mail::Message::Field::study()|Mail::Message::Field/"Access to the content">.

I<Example:> the study() short-cut for header fields


 print $msg->study('to'), "\n";

Is equivalent to:

 print $msg->head->study('to'), "\n";       # and
 print $msg->head->get('to')->study, "\n";

=back

$obj-E<gt>B<subject>

=over 4

Returns the message's subject, or the empty string.

I<Example:> using subject() to get the message's subject


 print $msg->subject;

=back

$obj-E<gt>B<timestamp>

=over 4

Get a good timestamp for the message, doesn't matter how much work it is.
The value returned is compatible with the platform dependent result of
function time().

In these days, the timestamp as supplied by the message (in the C<Date>
field) is not trustable at all: many spammers produce illegal or
unreal dates to influence their location in the displayed folder.

To start, the received headers are tried for a date (see
L<Mail::Message::Head::Complete::recvstamp()|Mail::Message::Head::Complete/"About the body">) and only then the C<Date>
field.  In very rare cases, only with some locally produced messages,
no stamp can be found.

=back

$obj-E<gt>B<to>

=over 4

Returns the addresses which are specified on the C<To> header line (or lines).
A list of Mail::Address objects is returned.  The people addressed
here are the targets of the content, and should read it contents
carefully.

I<Example:> using to() to get all primar destination addresses


 my @to = $message->to;

=back

=head2 The body


$obj-E<gt>B<body>([BODY])

=over 4

Return the body of this message.  BE WARNED that this returns
you an object which may be encoded: use decoded() to get a body
with usable data.

With options, a new BODY is set for this message.  This is B<not>
for normal use unless you understand the consequences: you change
the message content without changing the message-ID.  The right
way to go is via

 $message = Mail::Message->buildFromBody($body);  # or
 $message = Mail::Message->build($body);          # or
 $message = $origmsg->forward(body => $body);

The BODY must be an (sub-)class of L<Mail::Message::Body|Mail::Message::Body>.  In this case,
information from the specified body will be copied into the header.  The
body object will be encoded if needed, because messages written to file
or transmitted shall not contain binary data.  The converted body
is returned.

When BODY is C<undef>, the current message body will be dissected from
the message.  All relation will be cut.  The body is returned, and
can be connected to a different message.

I<Example:> 

 my $body      = $msg->body;
 my @encoded   = $msg->body->lines;

 my $new       = Mail::Message::Body->new(mime_type => 'text/html');
 my $converted = $msg->body($new);

=back

$obj-E<gt>B<decoded>(OPTIONS)

=over 4

Decodes the body of this message, and returns it as a body object.  If there
was no encoding, the body object as read from file is passed on, however,
some more work will be needed when a serious encoding is encountered.
The OPTIONS control how the conversion takes place.

 Option       Defined in       Default       
 keep                          <false>       
 result_type                   <type of body>

. keep BOOLEAN

=over 4

Controls whether the decoded result will be kept.  If not, the decoding
may be performed more than once.  However, it will consume extra
resources...

=back

. result_type BODYTYPE

=over 4

Specifies which kind of body should be used for the final result, and
eventual intermediate conversion stages.  It is not sure that this
will be the type of the body returned.  BODYTYPE extends
L<Mail::Message::Body|Mail::Message::Body>.

=back

I<Example:> 

 $message->decoded->print(\*OUT);
 $message->decoded->print;

 my $dec = $message->body($message->decoded);
 my $dec = $message->decoded(keep => 1);   # same

=back

$obj-E<gt>B<encode>(OPTIONS)

=over 4

Encode the message to a certain format.  Read the details in the
dedicated manual page L<Mail::Message::Body::Encode|Mail::Message::Body::Encode>.  The OPTIONS which
can be specified here are those of the L<Mail::Message::Body::encode()|Mail::Message::Body::Encode/"Constructing a body">
method.  

=back

$obj-E<gt>B<isMultipart>

=over 4

Check whether this message is a multipart message (has attachments).  To
find this out, we need at least the header of the message; there is no
need to read the body of the message to detect this.

=back

$obj-E<gt>B<isNested>

=over 4

Returns C<true> for C<message/rfc822> messages and message parts.

=back

$obj-E<gt>B<parts>(['ALL'|'ACTIVE'|'DELETED'|'RECURSE'|FILTER])

=over 4

Returns the I<parts> of this message. Usually, the term I<part> is used
with I<multipart> messages: messages which are encapsulated in the body
of a message.  To abstract this concept: this method will return you
all header-body combinations which are stored within this message
B<except> the multipart and message/rfc822 wrappers.
Objects returned are C<Mail::Message>'s and L<Mail::Message::Part|Mail::Message::Part>'s.

The option default to 'ALL', which will return the message itself for
single-parts, the nested content of a message/rfc822 object, respectively
the parts of a multipart without recursion.  In case of 'RECURSE', the
parts of multiparts will be collected recursively.  This option cannot
be combined with the other options, which you may want: it that case
you have to test yourself.

'ACTIVE' and 'DELETED' check for the deleted flag on messages and
message parts.  The FILTER is a code reference, which is called for
each part of the messagei; each part as C<RECURSE> would return.

I<Example:> 

 my @parts = $msg->parts;           # $msg not multipart: returns ($msg)
 my $parts = $msg->parts('ACTIVE'); # returns ($msg)

 $msg->delete;
 my @parts = $msg->parts;           # returns ($msg)
 my $parts = $msg->parts('ACTIVE'); # returns ()

=back

=head2 Flags


$obj-E<gt>B<delete>

=over 4

Flag the message to be deleted, which is a shortcut for
 $msg->label(deleted => time);
The real deletion only takes place on a synchronization of the folder.
See L<deleted()|Mail::Message/"Flags"> as well.

The time stamp of the moment of deletion is stored as value, but that
is not always preserved in the folder (depends on the implementation).
When the same message is deleted more than once, the first time stamp
will stay.

I<Example:> 

 $message->delete;
 $message->deleted(1);  # exactly the same
 $message->label(deleted => 1);
 delete $message;

=back

$obj-E<gt>B<deleted>([BOOLEAN])

=over 4

Set the delete flag for this message.  Without argument, the method
returns the same as L<isDeleted()|Mail::Message/"Flags">, which is prefered.  When a true
value is given, L<delete()|Mail::Message/"Flags"> is called.

I<Example:> 

 $message->deleted(1);          # delete
 $message->delete;              # delete (prefered)

 $message->deleted(0);          # undelete

 if($message->deleted) {...}    # check
 if($message->isDeleted) {...}  # check (prefered)

=back

$obj-E<gt>B<isDeleted>

=over 4

Short-cut for
 $msg->label('deleted')

For some folder types, you will get the time of deletion in return.  This
depends on the implementation.

I<Example:> 

 next if $message->isDeleted;

 if(my $when = $message->isDeleted) {
    print scalar localtime $when;
 }

=back

$obj-E<gt>B<isModified>

=over 4

Returns whether this message is flagged as being modified.  Modifications
are changes in header lines, when a new body is set to the message
(dangerous), or when labels change.

=back

$obj-E<gt>B<label>(LABEL|PAIRS)

=over 4

Return the value of the LABEL, optionally after setting some values.  In
case of setting values, you specify key-value PAIRS.

Labels are used to store knowledge about handling of the message within
the folder.  Flags about whether a message was read, replied to, or
scheduled for deletion.

Some labels are taken from the header's C<Status> and C<X-Status> lines,
however folder types like MH define a separate label file.

I<Example:> 

 print $message->label('seen');
 if($message->label('seen')) {...};
 $message->label(seen => 1);

 $message->label(deleted => 1);  # same as $message->delete

=back

$obj-E<gt>B<labels>

=over 4

Returns all known labels.  In SCALAR context, it returns the knowledge
as reference to a hash.  This is a reference to the original data, but
you shall *not* change that data directly: call C<label> for
changes!

In LIST context, you get a list of names which are defined.  Be warned
that they will not all evaluate to true, although most of them will.

=back

$obj-E<gt>B<labelsToStatus>

=over 4

When the labels were changed, that may effect the C<Status> and/or
C<X-Status> header lines of mbox messages.  Read about the relation
between these fields and the labels in the DETAILS chapter.

The method will carefully only affect the result of L<modified()|Mail::Message/"Flags"> when
there is a real change of flags, so not for each call to L<label()|Mail::Message/"Flags">.

=back

$obj-E<gt>B<modified>([BOOLEAN])

=over 4

Returns (optionally after setting) whether this message is flagged as
being modified.  See isModified().

=back

$obj-E<gt>B<statusToLabels>

=over 4

Update the labels according the status lines in the header.  See the
description in the DETAILS chapter.

=back

=head2 The whole message as text


$obj-E<gt>B<file>

=over 4

See L<Mail::Message::Construct::Text/"The whole message as text">

=back

$obj-E<gt>B<lines>

=over 4

See L<Mail::Message::Construct::Text/"The whole message as text">

=back

$obj-E<gt>B<printStructure>([FILEHANDLE][, INDENT])

=over 4

See L<Mail::Message::Construct::Text/"The whole message as text">

=back

$obj-E<gt>B<string>

=over 4

See L<Mail::Message::Construct::Text/"The whole message as text">

=back

=head2 Internals


$obj-E<gt>B<clonedFrom>

=over 4

Returns the MESSAGE which is the source of this message, which was
created by a L<clone()|Mail::Message/"Constructors"> operation.

=back

Mail::Message-E<gt>B<coerce>(MESSAGE, OPTIONS)

=over 4

Coerce a MESSAGE into a Mail::Message.  In some
occasions, for instance where you add a message to a folder, this
coercion is automatically called to ensure that the correct message
type is stored.

The coerced message is returned on success, otherwise C<undef>.  The
coerced message may be a reblessed version of the original message
or a new object.  In case the message has to be specialized, for
instance from a general Mail::Message into a Mail::Box::Mbox::Message,
no copy is needed.  However, to coerce a Mail::Internet object into
a Mail::Message, a lot of copying and converting will take place.

Valid MESSAGEs which can be coerced into Mail::Message objects
are of type

=over 4

=item * Any type of L<Mail::Box::Message|Mail::Box::Message>

=item * MIME::Entity's, using L<Mail::Message::Convert::MimeEntity|Mail::Message::Convert::MimeEntity>

=item * Mail::Internet's, using L<Mail::Message::Convert::MailInternet|Mail::Message::Convert::MailInternet>

=back

L<Mail::Message::Part|Mail::Message::Part>'s, which are extensions of C<Mail::Message>'s,
can also be coerced directly from a L<Mail::Message::Body|Mail::Message::Body>.

I<Example:> 

 my $folder  = Mail::Box::Mbox->new;
 my $message = Mail::Message->build(...);

 my $coerced = Mail::Box::Mbox::Message->coerce($message);
 $folder->addMessage($coerced);

Simpler replacement for the previous two lines:

 my $coerced = $folder->addMessage($message);

=back

$obj-E<gt>B<isDelayed>

=over 4

Check whether the message is delayed (not yet read from file).  Returns
true or false, dependent on the body type.

=back

$obj-E<gt>B<readBody>(PARSER, HEAD [, BODYTYPE])

=over 4

Read a body of a message.  The PARSER is the access to the folder's
file, and the HEAD is already read.  Information from the HEAD is used
to create expectations about the message's length, but also to determine
the mime-type and encodings of the body data.

The BODYTYPE determines which kind of body will be made and defaults to
the value specified by new(body_type).
BODYTYPE may be the name of a body class, or a reference
to a routine which returns the body's class when passed the HEAD as only
argument.

=back

$obj-E<gt>B<readFromParser>(PARSER, [BODYTYPE])

=over 4

Read one message from file.  The PARSER is opened on the file.  First
L<readHead()|Mail::Message/"Internals"> is called, and the head is stored in the message.  Then
L<readBody()|Mail::Message/"Internals"> is called, to produce a body.  Also the body is added to
the message without decodings being done.

The optional BODYTYPE may be a body class or a reference to a code
which returns a body-class based on the header.

=back

$obj-E<gt>B<readHead>(PARSER [,CLASS])

=over 4

Read a head into an object of the specified CLASS.  The CLASS defaults to
L<new(head_type)|Mail::Message/"Constructors">.  The PARSER is the access to the folder's file.

=back

$obj-E<gt>B<recursiveRebuildPart>(PART, OPTIONS)

=over 4

See L<Mail::Message::Construct::Rebuild/"Internals">

=back

$obj-E<gt>B<storeBody>(BODY)

=over 4

Where the L<body()|Mail::Message/"The body"> method can be used to set and get a body, with all
the necessary checks, this method is bluntly adding the specified body
to the message.  No conversions, not checking.

=back

$obj-E<gt>B<takeMessageId>([STRING])

=over 4

Take the message-id from the STRING, or create one when the C<undef>
is specified.  If not STRING nor C<undef> is given, the current header
of the message is requested for the value of the C<'Message-ID'> field.

Angles (if present) are removed from the id. 

=back

=head2 Error handling


$obj-E<gt>B<AUTOLOAD>

=over 4

See L<Mail::Message::Construct/"METHODS">

=back

$obj-E<gt>B<addReport>(OBJECT)

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<defaultTrace>([LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK])

Mail::Message-E<gt>B<defaultTrace>([LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<errors>

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<log>([LEVEL [,STRINGS]])

Mail::Message-E<gt>B<log>([LEVEL [,STRINGS]])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<logPriority>(LEVEL)

Mail::Message-E<gt>B<logPriority>(LEVEL)

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<logSettings>

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<notImplemented>

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<report>([LEVEL])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<reportAll>([LEVEL])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<shortSize>([VALUE])

Mail::Message-E<gt>B<shortSize>([VALUE])

=over 4

Represent an integer VALUE representing the size of file or memory,
(which can be large) into a short string using M and K (Megabytes
and Kilobytes).  Without VALUE, the size of the message head is used.

=back

$obj-E<gt>B<shortString>

=over 4

Convert the message header to a short string (without trailing newline),
representing the most important facts (for debugging purposes only).  For
now, it only reports size and subject.

=back

$obj-E<gt>B<trace>([LEVEL])

=over 4

See L<Mail::Reporter/"Error handling">

=back

$obj-E<gt>B<warnings>

=over 4

See L<Mail::Reporter/"Error handling">

=back

=head2 Cleanup


$obj-E<gt>B<DESTROY>

=over 4

When a message is to accessible anymore by any user's reference, Perl
will call DESTROY for final clean-up.  In this case, the head and
body are released, and de-registered for the folder.  You shall not call
this yourself!

=back

$obj-E<gt>B<destruct>

=over 4

Remove the information contained in the message object.  This will be
ignored when more than one reference to the same message object exists,
because the method has the same effect as assigning C<undef> to the
variable which contains the reference.  Normal garbage collection will
call L<DESTROY()|Mail::Message/"Cleanup"> when possible.

This method is only provided to hide differences with messages which are
located in folders: their L<Mail::Box::Message::destruct()|Mail::Box::Message/"Cleanup"> works quite
differently.

I<Example:> of Mail::Message destruct


 my $msg = Mail::Message->read;
 $msg->destruct;
 $msg = undef;    # same

=back

$obj-E<gt>B<inGlobalDestruction>

=over 4

See L<Mail::Reporter/"Cleanup">

=back



=head1 DIAGNOSTICS

I<Error:> Cannot include forward source as $include.

Unknown alternative for the L<forward(include)|Mail::Message::Construct::Forward/"Constructing a message">.  Valid choices are
C<NO>, C<INLINE>, C<ATTACH>, and C<ENCAPSULATE>.

I<Error:> Cannot include reply source as $include.

Unknown alternative for the C<include> option of L<reply()|Mail::Message::Construct::Reply/"Constructing a message">.  Valid
choices are C<NO>, C<INLINE>, and C<ATTACH>.

I<Error:> No address to create forwarded to.

If a forward message is created, a destination address must be specified.

I<Error:> No default mailer found to send message.

The message L<send()|Mail::Message/"The message"> mechanism had not enough information to automatically
find a mail transfer agent to sent this message.  Specify a mailer
explicitly using the C<via> options.

I<Error:> Only build() Mail::Message's; they are not in a folder yet

You may wish to construct a message to be stored in a some kind
of folder, but you need to do that in two steps.  First, create a
normal L<Mail::Message|Mail::Message>, and then add it to the folder.  During this
L<Mail::Box::addMessage()|Mail::Box/"The folder"> process, the message will get L<coerce()|Mail::Message/"Internals">-d
into the right message type, adding storage information and the like.

I<Error:> Package $package does not implement $method.

Fatal error: the specific package (or one of its superclasses) does not
implement this method where it should. This message means that some other
related classes do implement this method however the class at hand does
not.  Probably you should investigate this and probably inform the author
of the package.

I<Error:> forwardAttach requires a preamble object


I<Error:> forwardEncapsulate requires a preamble object


I<Error:> no rebuild rule $name defined.



=head1 DETAILS


=head2 Structure of a Message

A MIME-compliant message is build upon two parts: the I<header> and the
I<body>.

=head3 The header

The header is a list of fields, some spanning more than one line
(I<folded>) each telling something about the message. Information stored
in here are for instance the sender of the message, the receivers of
the message, when it was transported, how it was transported, etc.
Headers can grow quite large.

In MailBox, each message object manages exactly one header object
(a L<Mail::Message::Head|Mail::Message::Head>) and one body object (a L<Mail::Message::Body|Mail::Message::Body>).
The header contains a list of header fields, which are represented by
L<Mail::Message::Field|Mail::Message::Field> objects.

=head3 The body

The body contains the "payload": the data to be transfered.
The data can be encoded, only accessible with a specific application,
and may use some weird character-set, like Vietnamese; the MailBox
distribution tries to assist you with handling these e-mails without
the need to know all the details.  This additional information
("meta-information") about the body data is stored in the header.
The header contains more information, for instance about the message
transport and relations to other messages.

=head2 Message object implementation

The general idea about the structure of a message is

 Mail::Message
  |  |
  |  `-has-one--Mail::Message::Body
  |
  `----has-one--Mail::Message::Head
                  |
                  `-has-many--Mail::Message::Field

However: there are about 7 kinds of body objects, 3 kinds of headers and
3 kinds of fields.  You will usually not see too much of these kinds,
because they are merely created for performance reasons and can be used
all the same, with the exception of the multipart bodies.

A multipart body is either a L<Mail::Message::Body::Multipart|Mail::Message::Body::Multipart>
(mime type C<multipart/*>) or a L<Mail::Message::Body::Nested|Mail::Message::Body::Nested>
(mime type C<message/rfc822>).  These bodies are more complex:

 Mail::Message::Body::Multipart
  |
  `-has-many--Mail::Message::Part
               |  |
               |  `-has-one--Mail::Message::Body
               |
               `----has-one--Mail::Message::Head

Before you try to reconstruct multiparts or nested messages yourself,
you can better take a look at L<Mail::Message::Construct::Rebuild|Mail::Message::Construct::Rebuild>.

=head2 Message class implementation

The class structure of messages is very close to that of folders.  For
instance, a L<Mail::Box::File::Message|Mail::Box::File::Message> relates to a L<Mail::Box::File|Mail::Box::File>
folder.

As extra level of inheritance, it has a L<Mail::Message|Mail::Message>, which
is a message without location.  And there is a special case of
message: L<Mail::Message::Part|Mail::Message::Part> is a message encapsulated in a
multipart body.

The message types are:

 Mail::Box::Mbox::Message            Mail::Box::POP3::Message
 |  Mail::Box::Dbx::Message      Mail::Box::IMAP4::Message  |
 |  |                                                    |  |
 Mail::Box::File::Message             Mail::Box::Net::Message
         |                                      |
         |       Mail::Box::Maildir::Message    |
         |       |   Mail::Box::MH::Message     |
         |       |   |                          |
         |       Mail::Box::Dir::Message        |
         |                |                     |
         `------------.   |   .-----------------'
                      |   |   |
                   Mail::Box::Message    Mail::Message::Part
                          |                     |
                          |       .-------------'
                          |       |
                      Mail::Message
                          |
                          |
                    Mail::Reporter (general base class)

By far most folder features are implemented in L<Mail::Box|Mail::Box>, so
available to all folder types.  Sometimes, features which appear
in only some of the folder types are simulated for folders that miss
them, like sub-folder support for MBOX.

Two strange other message types are defined:
the L<Mail::Message::Dummy|Mail::Message::Dummy>, which fills holes in
L<Mail::Box::Thread::Node|Mail::Box::Thread::Node> lists, and a L<Mail::Box::Message::Destructed|Mail::Box::Message::Destructed>,
this is an on purpose demolished message to reduce memory consumption.

=head2 Labels

Labels (also named "Flags") are used to indicate some special condition on
the message, primary targeted on organizational issues: which messages are
already read or should be deleted.  There is a very strong user relation
to labels.

The main complication is that each folder type has its own way of storing
labels.  To give an indication: MBOX folders use C<Status> and C<X-Status>
header fields, MH uses a C<.mh-sequences> file, MAILDIR encodes the flags
in the message's filename, and IMAP has flags as part of the protocol.

Besides, some folder types can store labels with user defined names,
where other lack that feature.  Some folders have case-insensitive
labels, other don't. Read all about the specifics in the manual page of
the message type you actually have.

=head3 Predefined labels

To standardize the folder types, MailBox has defined the following labels,
which can be used with the L<label()|Mail::Message/"Flags"> and L<labels()|Mail::Message/"Flags"> methods on all kinds
of messages:

=over 4

=item * deleted

This message is flagged to be deleted once the folder closes.  Be very
careful about the concept of 'delete' in a folder context : it is only a
flag, and does not involve immediate action!  This means, for instance,
that the memory which is used by Perl to store the message is not released
immediately (see L<destruct()|Mail::Message/"Cleanup"> if you need to).

The methods L<delete()|Mail::Message/"Flags">, L<deleted()|Mail::Message/"Flags">, and L<isDeleted()|Mail::Message/"Flags"> are only
short-cuts for managing the C<delete> label (as of MailBox 2.052).

=item * draft

The user has prepared this message, but is has not been send (yet).  This
flag is not automatically added to a message by MailBox, and has only
a meaning in user applications.

=item * flagged

Messages can be I<flagged> for some purpose, for instance as result of
a search for spam in a folder.  The L<Mail::Box::messages()|Mail::Box/"The messages"> method
can be used to collect all these flagged messages from the folder.

Probably it is more useful to use an understandable name (like C<spam>)
for these selections, however these self-defined labels can not stored
in all folder types.

=item * old

The message was already in the folder when it was opened the last time,
so was not recently added to the folder.  This flag will never automatically
be set by MailBox, because it would probably conflict with the user's
idea of what is old.

=item * passed

Not often used or kept, this flag indicates that the message was bounced
or forwarded to someone else.

=item * replied

The user (or application) has sent a message back to the sender of
the message, as response of this one.  This flag is automatically
set if you use L<reply()|Mail::Message::Construct::Reply/"Constructing a message">, but not with L<forward()|Mail::Message::Construct::Forward/"Constructing a message"> or L<bounce()|Mail::Message::Construct::Bounce/"Constructing a message">.

=item * seen

When this flag is set, the receiver of the message has consumed the message.
A mail user agent (MUA) will set this flag when the user has opened the
message once.

=back

=head3 Status and X-Status fields

Mbox folders have no special means of storing information about messages
(except the message separator line), and therefore have to revert to
adding fields to the message header when something special comes up.
This feature is also enabled for POP3, although whether that works
depends on the POP server.

All applications which can handle mbox folders support the C<Status> and
C<X-Status> field convensions.  The following encoding is used:

 Flag   Field       Label
 R      Status   => seen    (Read)
 O      Status   => old     (not recent)
 A      X-Status => replied (Answered)
 F      X-Status => flagged

There is no special flag for C<deleted>, which most other folders support:
messages flagged to be deleted will never be written to a folder file when
it is closed.





=head1 REFERENCES

See the MailBox website at L<http://perl.overmeer.net/mailbox/> for more details.

=head1 COPYRIGHTS

Distribution version 2.058.
Written by Mark Overmeer (mark@overmeer.net).  See the ChangeLog for
other contributors.

Copyright (c) 2001-2003 by the author(s). All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.


