Mail::Message(3pm) | User Contributed Perl Documentation | Mail::Message(3pm) |
Mail::Message - general message object
Mail::Message has extra code in Mail::Message::Construct Mail::Message::Construct::Bounce Mail::Message::Construct::Build Mail::Message::Construct::Forward Mail::Message::Construct::Read Mail::Message::Construct::Rebuild Mail::Message::Construct::Reply Mail::Message::Construct::Text Mail::Message is a Mail::Reporter Mail::Message is extended by Mail::Box::Message Mail::Message::Dummy Mail::Message::Part Mail::Message::Replace::MailInternet
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 Mail::Message::Head $head = $msg->head; my Mail::Message::Body $body = $msg->decoded; $msg->decoded->print($outfile); # Send a simple email Mail::Message->build ( To => 'you@example.com' , From => 'me@example.com' , Subject => "My subject" , data => "Some plain text content" )->send(via => 'postfix'); my $reply_msg = Mail::Message->reply(...); my $frwd_msg = Mail::Message->forward(...);
A "Mail::Message" object is a container for MIME-encoded message information, as defined by RFC2822. Everything what is not specificaly related to storing the messages in mailboxes (folders) is implemented in this class. Methods which are related to folders is implemented in the Mail::Box::Message extension.
The main methods are get(), to get information from a message header field, and decoded() 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.
Extends "DESCRIPTION" in Mail::Reporter.
Extends "METHODS" in Mail::Reporter.
Extends "Constructors" in Mail::Reporter.
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>
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.
example:
$copy = $msg->clone;
-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>
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
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".
Usually, this string is very short. Numbering follows the IMAP4 design, see RFC2060 section 6.4.5.
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.
The optional $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(). That object also provides a "trySend()" method which gives more low-level control.
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')
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.
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.
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;
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";
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";
example:
my $header = Mail::Message::Head->new; $msg->head($header); # set my $head = $msg->head; # get
example: using sender() to get exactly one sender address
my $sender = $message->sender; print "Reply to: ", $sender->format, "\n" if defined $sender;
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" }
example: using subject() to get the message's subject
print $msg->subject; print $msg->study('subject');
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.
example: using to() to get all primar destination addresses
my @to = $message->to;
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);
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 ()
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;
example:
$message->deleted(1); # delete $message->delete; # delete (preferred) $message->deleted(0); # undelete if($message->deleted) {...} # check if($message->isDeleted) {...} # check (preferred)
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; }
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
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.
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().
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);
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.
The optional $bodytype may be a body class or a reference to a code which returns a body-class based on the header.
Angles (if present) are removed from the id.
Extends "Error handling" in Mail::Reporter.
Extends "Cleanup" in Mail::Reporter.
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
A MIME-compliant message is build upon two parts: the header and the body.
The header
The header is a list of fields, some spanning more than one line (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 Mail::Message::Head) and one body object (a Mail::Message::Body). The header contains a list of header fields, which are represented by Mail::Message::Field objects.
The body
The body contains the "payload": the data to be transferred. 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.
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 Mail::Message::Body::Multipart (mime type "multipart/*") or a Mail::Message::Body::Nested (mime type "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 Mail::Message::Construct::Rebuild.
The class structure of messages is very close to that of folders. For instance, a Mail::Box::File::Message relates to a Mail::Box::File folder.
As extra level of inheritance, it has a Mail::Message, which is a message without location. And there is a special case of message: 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 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 Mail::Message::Dummy, which fills holes in Mail::Box::Thread::Node lists, and a Mail::Box::Message::Destructed, this is an on purpose demolished message to reduce memory consumption.
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 "Status" and "X-Status" header fields, MH uses a ".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.
Predefined labels
To standardize the folder types, MailBox has defined the following labels, which can be used with the label() and labels() methods on all kinds of messages:
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 destruct() if you need to).
The methods delete(), deleted(), and isDeleted() are only short-cuts for managing the "delete" label (as of MailBox 2.052).
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.
Messages can be flagged for some purpose, for instance as result of a search for spam in a folder. The Mail::Box::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 "spam") for these selections, however these self-defined labels can not stored in all folder types.
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.
Not often used or kept, this flag indicates that the message was bounced or forwarded to someone else.
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 reply(), but not with forward() or bounce().
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.
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 "Status" and "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 "deleted", which most other folders support: messages flagged to be deleted will never be written to a folder file when it is closed.
This module is part of Mail-Message distribution version 3.010, built on October 14, 2020. Website: http://perl.overmeer.net/CPAN/
Copyrights 2001-2020 by [Mark Overmeer <markov@cpan.org>]. For other contributors see ChangeLog.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/
2020-10-18 | perl v5.30.3 |