Mail::Message - general message object
Mail::Message + ::Construct is a Mail::Reporter
use Mail::Box::Manager; my $mgr = Mail::Box::Manager->new; my $folder = $mgr->open(folder => $MAIL); my $msg = $folder->message(2); # $msg isa Mail::Message $msg->decoded->print($outfile); my Mail::Message $construct = Mail::Message->new; my Mail::Message $construct = Mail::Message->build(...); my Mail::Message::Head $head = $msg->head; my Mail::Message::Body $body = $msg->decoded; my $subject = $msg->get('subject');
A Mail::Message object is a container for message information read from a file. Everything what is not folder related will be found here.
Mail::Message
Complex message handling (like construction of replies) are handled by the Mail::Message::Construct package which is autoloaded. That package adds functionality to the Mail::Message objects.
Mail::Message::Construct
The main methods are get() -to get information from a message header- and decoded() to get the intented content of a message.
get()
decoded()
Methods prefixed with an abbreviation are described in Mail::Reporter (MR), Mail::Message::Construct (MMC).
The general methods for Mail::Message objects:
bcc MR log [LEVEL [,STRINGS]] MMC bounce OPTIONS messageId MMC build [MESSAGE|BODY], CONTENT modified [BOOL] MMC buildFromBody BODY, HEADERS new OPTIONS cc nrLines date parent decoded OPTIONS parts destinations print [FILEHANDLE] encode OPTIONS printUndisclosed [FILEHANDLE] MR errors MMC reply OPTIONS MMC forward OPTIONS MMC replyPrelude [STRING|FIELD|... MMC forwardPostlude MMC replySubject STRING MMC forwardPrelude MR report [LEVEL] MMC forwardSubject STRING MR reportAll [LEVEL] from send [MAILER], OPTIONS get FIELD size guessTimestamp subject isDummy timestamp isMultipart to isPart toplevel label LABEL [,VALUE [LABEL,... MR trace [LEVEL]
The extra methods for extension writers:
MR AUTOLOAD labelsToStatus DESTROY MR logPriority LEVEL body [BODY] MR logSettings clone MR notImplemented coerce MESSAGE read PARSER, [BODYTYPE] head [HEAD] readBody PARSER, HEAD [, BO... MR inGlobalDestruction readHead PARSER [,CLASS] isDelayed statusToLabels labels storeBody BODY
Create a new message object. The message's head and body will be read later, unless specified at construction.
OPTION DESCRIBED IN DEFAULT body Mail::Message undef head Mail::Message undef log Mail::Reporter 'WARNINGS' messageId Mail::Message undef modified Mail::Message 0 trace Mail::Reporter 'WARNINGS' trusted Mail::Message 0 head_wrap Mail::Message 72
Only for extension writers:
OPTION DESCRIBED IN DEFAULT body_type Mail::Message 'Mail::Message::Body::Lines' field_type Mail::Message undef head_type Mail::Message 'Mail::Message::Head::Complete'
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.
Mail::Message::Body
body_type => CLASS
Default type of body to be created for readBody().
readBody()
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.
Mail::Message::Head
head_type => CLASS
Default type of head to be created for readHead().
readHead()
head_wrap > WIDTH
The soft maximum line width of header lines in the folder to write.
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 => BOOL
Flags this message as being modified, from the beginning on. Usually, modification is auto-detected, but there may be reasons to be extra explicit.
trusted => BOOL
Is this message from a trusted source? If not, the content must be checked before use.
Returns the value which is stored in the header FIELD with the specified name. If the field has multiple appearances in the header, the last instance is returned.
The field name is case insensitive. Only the `body' of the field is returned, not the comment (after ';'). If you need more complex handing of fields, then call
Example:
print $msg->get('Content-Type'), "\n";
Is equivalent to:
print $msg->head->get('Content-Type')->body, "\n";
Returns the address of the sender. This can only be one address. Of this is a bounced message, the Mail::Address representation of the Resent-From line is returned. Otherwise, the From line is scanned. If that line is not present, the Sender line is probed. Otherwise, undef is returned.
Mail::Address
Resent-From
From
Sender
undef
my $from = $message->from;
Returns the addresses which are defined for the indicated header-lines. Most methods return a list of Mail::Message objects, except from which returns one address only, and date returns the last date as string.
from
date
These methods are a little more complicated than just fetching these header lines by the existence of Resent- header lines. These Resent- lines are added when messages get bounced, and take preference over their counterparts. Only the last Resent- line can be used.
Resent-
Examples:
my @to = $message->to; my $date = $message->date;
Returns a list of Mail::Address objects which contains the combined info of active To, Cc, and Bcc addresses. Doubles are removed.
To
Cc
Bcc
Returns the message's subject, just as short-cut for writing
$message->get('subject')
Retrieve the message's id. Every message has a unique message-id. This id is used mainly for recognizing discussion threads.
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.
received
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.
timestamp()
print "Receipt ", ($message->timestamp || 'unknown'), "\n";
Get a timestamp, doesn't matter how much work it is. If it is impossible to get a time from the header-lines, the current time-of-living is taken.
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 DESCRIBED IN DEFAULT keep Mail::Message 0 result_type Mail::Message <type of the body>
keep => BOOL
Controls whether the decoded result will be kept. If not, the decoding may be performed more than once. However, it will consume extra resources...
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.
Mail::Message:Body
$message->decoded->print(\*OUT); $message->decoded->print; my $dec = $message->body($message->decoded); my $dec = $message->decoded(keep => 1); # same
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 encode() method.
Mail::Message::Body::Encode
encode()
Returns (optionally after setting) whether this message is flagged as being modified. The modification flag is set true when header lines are changed, the header or body replaced by a new one, or when labels are modified.
true
If the message is a part of another message, parent returns the reference to the containing message. parent returns undef if the message is not a part, but rather the main message. isPart returns true if the message is a part of another message. toplevel returns a reference to the main message, which will be the current message if the message is not part of another message.
parent
isPart
toplevel
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->parent; # returns $part $nested->toplevel; # returns $msg $msg->parent; # returns undef $msg->toplevel; # returns $msg $msg->isPart; # returns false $part->isPart; # returns true
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 extention of Mail::Message, this will return false.
Mail::Message::Dummy
false
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.
Returns the parts of this message. If the message is not a multi-part, it will be returned itself. However, if this is a multi-part then a list with all the parts is returned.
Print the message to the FILE-HANDLE, which defaults to the selected filehandle. In the former case of print including the Bcc and Resent-Bcc lines, in the latter case without them.
print
Resent-Bcc
$message->print(\*STDERR); $message->print; my $out = IO::File->new('out', 'w'); $message->print($out);
Transmit the message to anything outside this Perl program. Writing
my $mailer = Mail::Transport::SMTP->new(@smtpopts); $message->send($mailer, @sendopts);
is a short for
my $mailer = Mail::Transport::SMTP->new(@smtpopts); $mailer->send($message, @sendopts);
However, when the MAILER is not specified, one will be auto-generated via Mail::Transport::new. This object will be re-used. For instance
Mail::Transport::new
$message->send(@sendopts);
is equivalent to
Mail::Transport->new->send($message, @sendopts);
The OPTIONS are mailer specific.
Returns the size of the whole message in bytes.
Returns the number of lines used for the whole message.
Return the value of the LABEL, optionally after setting it to VALUE. If the VALUE is undef then the label is removed. You may specify a list of LABEL-VALUE pairs at once. In the latter case, the first VALUE is returned.
Labels are used to store knowledge about handling of the message within the folder. Flags about whether a message was read, replied to, or (in some cases) scheduled for deletion.
Some labels are taken from the header's Status and X-Status lines, however folder types like MH define a seperate label file.
Status
X-Status
print $message->label('seen'); if($message->label('seen')) {...}; $message->label(seen => 1);
Create a copy of this message. The head and body, the log and trace levels are taken. The copy will not be added to any folder automatically.
$copy = $msg->clone;
See also the copyTo FOLDER, moveTo FOLDER, and reply methods.
copyTo FOLDER
moveTo FOLDER
reply
Read one message from file. The PARSER is opened on the file. First readHeader 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.
readHeader
readBody
The optional BODYTYPE may be a body class or a reference to a code which returns a body-class based on the header.
Read a head into an object of the specified CLASS. The CLASS defaults to the head_type option specified at creation of the message (see new). The PARSER is the access to the folder's file.
head_type
new
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 the body_type option at message creation (see new). 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.
body_type
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.
decoded
With options, a new BODY is set for this message. The body must be an (sub-)class of Mail::Message::Body. In this case, information from the specified body will be compied 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 disected from the message. All relation will be cut. The body is returned, and can be connected to a diffent message.
my $body = $msg->body; my @encoded = $msg->body->lines; my $new = Mail::Message::Body->new(mime_type => 'text/html'); my $converted = $msg->body($new);
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.
body()
Return (optionally after setting) the HEAD of this message. The head must be an (sub-)class of 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 LABALS hash. They are added later.
my $head = $msg->head(new Mail::Message::Head);
Check whether the message is delayed (not yet read from file). Returns true or false, dependent on the body type.
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.
'Message-ID'
Angles (if present) are removed from the id.
(Class method) Coerce a MESSAGE into a Mail::Message. In some occations, 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.
Valid MESSAGEs which can be coerced into Mail::Message objects are of type
MIME::Entity's, using Mail::Message::Convert::MimeEntity
MIME::Entity
Mail::Message::Convert::MimeEntity
Mail::Internet's, using Mail::Message::Convert::MimeEntity
Mail::Internet
any extension of Mail::Message is left untouched
my $message = Mail::Message->new(...); my $coerced = Mail::Box::MBox::Message->coerce($message); # now $coerced is a Mail::Box::Mbox::Message
It is better to use (when the message will be stored in that folder):
my $folder = Mail::Box::Mbox->new; my $coerced = $folder->coerce($message); my $coerced = $folder->addMessage($message);
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!
label
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.
When the labels were changes, there may be an effect for the Status and/or X-Status header-lines. Whether this update has to take place depends on the type of folder.
Update de labels accoring the status lines in the header.
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!
Mail::Box-Overview
Mark Overmeer (mailbox@overmeer.net). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This code is beta, version 2.010.
Copyright (c) 2001 Mark Overmeer. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Mail::Box, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mail::Box
CPAN shell
perl -MCPAN -e shell install Mail::Box
For more information on module installation, please visit the detailed CPAN module installation guide.