Mail::Message - general message object
| Use Case | Command | Description |
|---|---|---|
| Open a folder and get a message | my $mgr = Mail::Box::Manager->new; my $folder = $mgr->open(folder => 'InBox'); my $msg = $folder->message(2); | Initialize a Mail::Box::Manager, open a folder, retrieve a message by index. |
| Get message subject | $msg->subject | Get the subject of the message. |
| Get CC addresses | $msg->cc | Get list of Mail::Address objects from CC header. |
| Get decoded body | $msg->decoded | Get the decoded body of the message. |
| Send a simple email | Mail::Message->build(...)->send(via => 'postfix'); | Build and send a message using a transport agent. |
| Reply to a message | Mail::Message->reply(...) | Create a reply message. |
| Forward a message | Mail::Message->forward(...) | Create a forward message. |
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 AT example.com'
, From => 'me AT 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.
$obj->clone(%options)
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.shallow_body => BOOLEAN - A rather safe bet, because you are not allowed to modify the body of a message.shallow_head => BOOLEAN - Only the head is reused, not the body. Probably a bad choice. $copy = $msg->clone;
Mail::Message->new(%options)
body => OBJECT - Instantiate the message with a body which has been created somewhere before the message is constructed.body_type => CLASS - Default type of body to be created for readBody().deleted => BOOLEAN - Is the file deleted from the start?field_type => CLASShead => OBJECT - Instantiate the message with a head which has been created somewhere before the message is constructed.head_type => CLASS - Default type of head to be created for readHead().labels => ARRAY|HASH - Initial values of the labels.log => LEVELmessageId => STRING - The id on which this message can be recognized.modified => BOOLEAN - Flags this message as being modified from the beginning.trace => LEVELtrusted => BOOLEAN - Is this message from a trusted source? If not, the content must be checked before use.$obj->bounce( [<$rg_object|%options>] ) - Inherited, see "Constructing a message" in Mail::Message::Construct::BounceMail::Message->build( [$message|$part|$body], $content ) - Inherited, see "Constructing a message" in Mail::Message::Construct::BuildMail::Message->buildFromBody($body, [$head], $headers) - Inherited, see "Constructing a message" in Mail::Message::Construct::Build$obj->forward(%options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Forward$obj->forwardAttach(%options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Forward$obj->forwardEncapsulate(%options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Forward$obj->forwardInline(%options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Forward$obj->forwardNo(%options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Forward$obj->forwardPostlude() - Inherited, see "Constructing a message" in Mail::Message::Construct::Forward$obj->forwardPrelude() - Inherited, see "Constructing a message" in Mail::Message::Construct::Forward$obj->forwardSubject(STRING) - Inherited, see "Constructing a message" in Mail::Message::Construct::ForwardMail::Message->read($fh|STRING|SCALAR|ARRAY, %options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Read$obj->rebuild(%options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Rebuild$obj->reply(%options) - Inherited, see "Constructing a message" in Mail::Message::Construct::Reply$obj->replyPrelude( [STRING|$field|$address|ARRAY-$of-$things] ) - Inherited, see "Constructing a message" in Mail::Message::Construct::Reply$obj->replySubject(STRING) / Mail::Message->replySubject(STRING) - Inherited, see "Constructing a message" in Mail::Message::Construct::Reply$obj->container() - If the message is a part of another message, "container" returns the reference to the containing body.$obj->isDummy() - Dummy messages are used to fill holes in linked-list and such. Returns true for Mail::Message::Dummy objects.$obj->isPart() - Returns true if the message is a part of another message.$obj->messageId() - Retrieve the message's id.$obj->partNumber() - Returns a string representing the location of this part.$obj->print( [$fh] ) - Print the message to the FILE-HANDLE without encapsulation.$obj->send( [$mailer], %options ) - Transmit the message to anything outside this Perl program.$obj->size() - Returns an estimated size of the whole message in bytes.$obj->toplevel() - Returns a reference to the main message.$obj->write( [$fh] ) - Write the message to the FILE-HANDLE with all surrounding information.$obj->bcc() - Returns the addresses specified on the "Bcc" header line.$obj->cc() - Returns the addresses specified on the "Cc" header line.$obj->date() - Method removed. Use timestamp() or $msg->head->get('Date').$obj->destinations() - Returns a list of Mail::Address objects from active "To", "Cc", and "Bcc".$obj->from() - Returns the addresses from the senders.$obj->get($fieldname) - Returns the value stored in the header field with the specified name.$obj->guessTimestamp() - Return an estimate on the time this message was sent.$obj->head( [$head] ) - Return (optionally after setting) the head of this message.$obj->nrLines() - Returns the number of lines used for the whole message.$obj->sender() - Returns exactly one address, the originator of this message.$obj->study($fieldname) - Study the content of a field, returning a Mail::Message::Field::Full object.$obj->subject() - Returns the message's subject.$obj->timestamp() - Get a good timestamp for the message.$obj->to() - Returns the addresses specified on the "To" header line.$obj->body( [$body] ) - Return the body of this message (optionally set a new body).$obj->contentType() - Returns the content type header line, or "text/plain".$obj->decoded(%options) - Decodes the body of this message.$obj->encode(%options) - Encode the message to a certain format.$obj->isMultipart() - Check whether this message is a multipart message.$obj->isNested() - Returns true for "message/rfc822" messages and message parts.$obj->parts( [<'ALL'|'ACTIVE'|'DELETED'|'RECURSE'|$filter>] ) - Returns the parts of this message.$obj->delete() - Flag the message to be deleted.$obj->deleted( [BOOLEAN] ) - Set or get the delete flag.$obj->isDeleted() - Short-cut for $msg->label('deleted').$obj->isModified() - Returns whether this message is flagged as modified.$obj->label($label|PAIRS) - Return the value of a label, optionally after setting.$obj->labels() - Returns all known labels.$obj->labelsToStatus() - Update Status and X-Status header lines from labels.$obj->modified( [BOOLEAN] ) - Returns (optionally after setting) the modified flag.$obj->statusToLabels() - Update labels from status lines.$obj->file() - Inherited, see "The whole message as text" in Mail::Message::Construct::Text$obj->lines() - Inherited, see "The whole message as text" in Mail::Message::Construct::Text$obj->printStructure( [$fh|undef],[$indent] ) - Inherited, see "The whole message as text" in Mail::Message::Construct::Text$obj->string() - Inherited, see "The whole message as text" in Mail::Message::Construct::Text$obj->clonedFrom() - Returns the source message from a clone() operation.Mail::Message->coerce($message, %options) - Coerce a message into a Mail::Message.$obj->isDelayed() - Check whether the message is delayed (not yet read from file).$obj->readBody( $parser, $head, [$bodytype] ) - Read a body of a message.$obj->readFromParser( $parser, [$bodytype] ) - Read one message from file.$obj->readHead( $parser, [$class] ) - Read a head into an object.$obj->recursiveRebuildPart($part, %options) - Inherited, see "Internals" in Mail::Message::Construct::Rebuild$obj->storeBody($body) - Bluntly add the specified body to the message.$obj->takeMessageId( [STRING] ) - Take the message-id from the STRING or create one.Extends "Error handling" in Mail::Reporter.
$obj->AUTOLOAD() - Inherited, see "METHODS" in Mail::Message::Construct$obj->addReport($object) - Inherited$obj->defaultTrace( [$level]|[$loglevel, $tracelevel]|[$level, $callback] ) / Mail::Message->defaultTrace(...) - Inherited$obj->errors() - Inherited$obj->log( [$level, [$strings]] ) / Mail::Message->log(...) - Inherited$obj->logPriority($level) / Mail::Message->logPriority($level) - Inherited$obj->logSettings() - Inherited$obj->notImplemented() - Inherited$obj->report( [$level] ) - Inherited$obj->reportAll( [$level] ) - Inherited$obj->shortSize( [$value] ) / Mail::Message->shortSize( [$value] ) - Represent an integer size as short string.$obj->shortString() - Convert the message header to a short string.$obj->trace( [$level] ) - Inherited$obj->warnings() - InheritedExtends "Cleanup" in Mail::Reporter.
$obj->DESTROY() - Inherited$obj->destruct() - Remove the information contained in the message object.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:
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 conventions. 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.012, built on February 11, 2022. Website: http://perl.overmeer.net/CPAN/
Copyrights 2001-2022 by Mark Overmeer (markov AT 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/
Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-21 00:13 @2600:1f28:365:80b0:d8a5:c3c6:bf94:28c0
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format