METHODS

See METHODS in Mail::Reporter

Constructors

$obj->clone( OPTIONS )

Create a copy of this message. Returned is a 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 always be a Mail::Message object. For example, a Mail::Box::Message's clone is detached from the folder of its original. When you use Mail::Box::addMessage() with the cloned message at hand, then the clone will automatically be coerced into the right message type to be added.

See also Mail::Box::Message::copyTo() and Mail::Box::Message::moveTo().

Option Default

shallow

<false>

shallow_body

<false>

shallow_head

<false>

shallow => BOOLEAN
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 Mail::Box::Message::moveTo() is used and you are sure that the original message cannot get undeleted after the move.
shallow_body => BOOLEAN
A rather safe bet, because you are not allowed to modify the body of a message: you may only set a new body with body().
shallow_head => BOOLEAN
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.
» Example:
 $copy = $msg->clone;
$class->new( OPTIONS )
Option Defined in Default

body

undef

body_type

Mail::Message::Body::Lines

deleted

<false>

field_type

undef

head

undef

head_type

Mail::Message::Head::Complete

labels

{}

log

Mail::Reporter

'WARNINGS'

messageId

undef

modified

<false>

trace

Mail::Reporter

'WARNINGS'

trusted

<false>

body => OBJECT
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 body() and storeBody().
body_type => CLASS
Default type of body to be created for readBody().
deleted => BOOLEAN
Is the file deleted from the start?
field_type => CLASS
head => OBJECT
Instantiate the message with a head which has been created somewhere before the message is constructed. The OBJECT must be a (sub-)class of Mail::Message::Head. See also head().
head_type => CLASS
Default type of head to be created for readHead().
labels => ARRAY|HASH
Initial values of the labels. In case of Mail::Box::Message's, this shall reflect the state the message is in. For newly constructed Mail::Message's, this may be anything you want, because coerce() will take care of the folder specifics once the message is added to one.
log => LEVEL
messageId => STRING
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.
modified => BOOLEAN
Flags this message as being modified from the beginning on. Usually, modification is auto-detected, but there may be reasons to be extra explicit.
trace => LEVEL
trusted => BOOLEAN
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.

Constructing a message

$obj->bounce( [RG-OBJECT|OPTIONS] )
See bounce in Mail::Message::Construct::Bounce.
$class->build( [MESSAGE|PART|BODY], CONTENT )
See build in Mail::Message::Construct::Build.
$class->buildFromBody( BODY, [HEAD], HEADERS )
See buildFromBody in Mail::Message::Construct::Build.
$obj->forward( OPTIONS )
See forward in Mail::Message::Construct::Forward.
$obj->forwardAttach( OPTIONS )
See forwardAttach in Mail::Message::Construct::Forward.
$obj->forwardEncapsulate( OPTIONS )
See forwardEncapsulate in Mail::Message::Construct::Forward.
$obj->forwardInline( OPTIONS )
See forwardInline in Mail::Message::Construct::Forward.
$obj->forwardNo( OPTIONS )
See forwardNo in Mail::Message::Construct::Forward.
$obj->forwardPostlude
See forwardPostlude in Mail::Message::Construct::Forward.
$obj->forwardPrelude
See forwardPrelude in Mail::Message::Construct::Forward.
$obj->forwardSubject( STRING )
See forwardSubject in Mail::Message::Construct::Forward.
$class->read( FILEHANDLE|SCALAR|REF-SCALAR|ARRAY-OF-LINES, OPTIONS )
See read in Mail::Message::Construct::Read.
$obj->rebuild( OPTIONS )
See rebuild in Mail::Message::Construct::Rebuild.
$obj->reply( OPTIONS )
See reply in Mail::Message::Construct::Reply.
$obj->replyPrelude( [STRING|FIELD|ADDRESS|ARRAY-OF-THINGS] )
See replyPrelude in Mail::Message::Construct::Reply.
$obj->replySubject( STRING )
$class->replySubject( STRING )
See replySubject in Mail::Message::Construct::Reply.

The message

$obj->container

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

» 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
$obj->isDummy

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 Mail::Message::Dummy objects, where this will return true. On any extension of Mail::Message, this will return false.

$obj->isPart

Returns true if the message is a part of another message. This is the case for Mail::Message::Part extensions of Mail::Message.

$obj->messageId

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

$obj->print( [FILEHANDLE] )

Print the message to the FILE-HANDLE, which defaults to the selected filehandle, without the encapsulation sometimes required by a folder type, like write() does.

» 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.
$obj->send( [MAILER], OPTIONS )

Transmit the message to anything outside this Perl program. MAILER is a 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 Mail::Transport::Send::new() and Mail::Transport::Send::send().

» 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 Mail::Box.

» Example:
 $message->send(via => 'sendmail')
» Error: No default mailer found to send message.

The message send() mechanism had not enough information to automatically find a mail transfer agent to sent this message. Specify a mailer explicitly using the via options.

$obj->size

Returns an estimated size of the whole message in bytes. In many occasions, the functions which process the message further, for instance send() or print() 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 nrLines() larger than this method returns.

$obj->toplevel

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

$obj->write( [FILEHANDLE] )

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 write will be the same as with print(). 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 '>' added in front.

The header

$obj->bcc

Returns the addresses which are specified on the Bcc header line (or lines) A list of Mail::Address objects is returned. Bcc stands for 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.

$obj->cc

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

$obj->date

Method has been removed for reasons of consistency. Use timestamp() or < $msg->head->get('Date') >.

$obj->destinations

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

$obj->from

Returns the addresses from the senders. It is possible to have more than one address specified in the 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 sender().

» Example: using from() to get all sender addresses
 my @from = $message->from;
$obj->get( FIELDNAME )

Returns the value which is stored in the header field with the specified name. The FIELDNAME is case insensitive. The unfolded body of the field is returned, stripped from any attributes. See Mail::Message::Field::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 Mail::Message::Head::get() yourself. See study() when you want to be smart, doing the better (but slower) job.

» 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";
$obj->guessTimestamp

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

This method may return undef if the header is not parsed or only partially known. If you require a time, then use the timestamp() method, described below.

» Example: using guessTimestamp() to get a transmission date
 print "Receipt ", ($message->timestamp || 'unknown'), "\n";
$obj->nrLines

Returns the number of lines used for the whole message.

$obj->sender

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

» Example: using sender() to get exactly one sender address
 my $sender = $message->sender;
 print "Reply to: ", $sender->format, "\n" if defined $sender;
$obj->study( FIELDNAME )

Study the content of a field, like get() does, with as main difference that a Mail::Message::Field::Full object is returned. These objects stringify to an utf8 decoded representation of the data contained in the field, where get() does not decode. When the field does not exist, then undef is returned. See Mail::Message::Field::study().

» 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";

or better:

 if(my $to = $msg->study('to')) { print "$to\n" }
 if(my $to = $msg->get('to')) { print $to->study, "\n" }
$obj->subject

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

» Example: using subject() to get the message's subject
 print $msg->subject;
$obj->timestamp

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 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 Mail::Message::Head::Complete::recvstamp()) and only then the Date field. In very rare cases, only with some locally produced messages, no stamp can be found.

$obj->to

Returns the addresses which are specified on the 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.

» Example: using to() to get all primar destination addresses
 my @to = $message->to;

The body

$obj->body( [BODY] )

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 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 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 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.

» 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);
$obj->contentType

Returns the content type header line, or text/plain if it is not defined. The parameters will be stripped off.

$obj->decoded( OPTIONS )

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 Default

charset

PERL

result_type

<type of body>

charset => CODESET|'PERL'
Translate the bytes of the message into the CODESET. When PERL is given, the content will be translated into Perl strings.
result_type => BODYTYPE
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 Mail::Message::Body.
» Example:
 $message->decoded->print(\*OUT);
 $message->decoded->print;
$obj->encode( OPTIONS )

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

$obj->isMultipart

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.

$obj->isNested

Returns true for message/rfc822 messages and message parts.

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

Returns the parts of this message. Usually, the term part is used with 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 except the multipart and message/rfc822 wrappers. Objects returned are Mail::Message's and 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 message; each part as RECURSE would return.

» 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 ()

Flags

$obj->delete

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 deleted() 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.

» Example:
 $message->delete;
 $message->deleted(1);  # exactly the same
 $message->label(deleted => 1);
 delete $message;
$obj->deleted( [BOOLEAN] )

Set the delete flag for this message. Without argument, the method returns the same as isDeleted(), which is prefered. When a true value is given, delete() is called.

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

 $message->deleted(0);          # undelete

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

Short-cut for

 $msg->label('deleted')

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

» Example:
 next if $message->isDeleted;

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

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.

$obj->label( LABEL|PAIRS )

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 Status and X-Status lines. Folder types like MH define a separate label file, and Maildir adds letters to the message filename. But the MailBox labels are always the same.

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

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

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 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.

$obj->labelsToStatus

When the labels were changed, that may effect the Status and/or 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 modified() when there is a real change of flags, so not for each call to label().

$obj->modified( [BOOLEAN] )

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

$obj->statusToLabels

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

The whole message as text

$obj->file
See file in Mail::Message::Construct::Text.
$obj->lines
See lines in Mail::Message::Construct::Text.
$obj->printStructure( [FILEHANDLE|undef],[INDENT] )
See printStructure in Mail::Message::Construct::Text.
$obj->string
See string in Mail::Message::Construct::Text.

Internals

$obj->clonedFrom

Returns the MESSAGE which is the source of this message, which was created by a clone() operation.

$class->coerce( MESSAGE, OPTIONS )

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 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

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

» 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);
» Error: Cannot coerce a $class object into a $class object
» Error: coercion starts with some object
$obj->isDelayed

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

$obj->readBody( PARSER, HEAD [, BODYTYPE] )

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.

$obj->readFromParser( PARSER, [BODYTYPE] )

Read one message from file. The PARSER is opened on the file. First readHead() is called, and the head is stored in the message. Then readBody() 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.

$obj->readHead( PARSER [,CLASS] )

Read a head into an object of the specified CLASS. The CLASS defaults to new(head_type). The PARSER is the access to the folder's file.

$obj->recursiveRebuildPart( PART, OPTIONS )
See recursiveRebuildPart in Mail::Message::Construct::Rebuild.
$obj->storeBody( BODY )

Where 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.

$obj->takeMessageId( [STRING] )

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

Angles (if present) are removed from the id.

Error handling

$obj->AUTOLOAD
See AUTOLOAD in Mail::Message::Construct.
$obj->addReport( OBJECT )
See addReport in Mail::Reporter.
$obj->defaultTrace( [LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK] )
$class->defaultTrace( [LEVEL]|[LOGLEVEL, TRACELEVEL]|[LEVEL, CALLBACK] )
See defaultTrace in Mail::Reporter.
$obj->errors
See errors in Mail::Reporter.
$obj->log( [LEVEL [,STRINGS]] )
$class->log( [LEVEL [,STRINGS]] )
See log in Mail::Reporter.
$obj->logPriority( LEVEL )
$class->logPriority( LEVEL )
See logPriority in Mail::Reporter.
$obj->logSettings
See logSettings in Mail::Reporter.
$obj->notImplemented
See notImplemented in Mail::Reporter.
$obj->report( [LEVEL] )
See report in Mail::Reporter.
$obj->reportAll( [LEVEL] )
See reportAll in Mail::Reporter.
$obj->shortSize( [VALUE] )
$class->shortSize( [VALUE] )

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.

$obj->shortString

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.

$obj->trace( [LEVEL] )
See trace in Mail::Reporter.
$obj->warnings
See warnings in Mail::Reporter.

Cleanup

$obj->DESTROY

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!

$obj->destruct

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 undef to the variable which contains the reference. Normal garbage collection will call DESTROY() when possible.

This method is only provided to hide differences with messages which are located in folders: their Mail::Box::Message::destruct() works quite differently.

» Example: of Mail::Message destruct
 my $msg = Mail::Message->read;
 $msg->destruct;
 $msg = undef;    # same
$obj->inGlobalDestruction
See inGlobalDestruction in Mail::Reporter.