Mail::I...tructure(3)
π NAME π
Mail::IMAPClient::BodyStructure β bestie, it parses your fetched results β¨
This is the Mail::I...tructure(3) page, from the User Contributed Perl Documentation collection, and we are about to make sense of all of it, together π«Ά
π SYNOPSIS π
use Mail::IMAPClient;
use Mail::IMAPClient::BodyStructure;
my $imap = Mail::IMAPClient->new(
Server => $server, User => $login, Password => $pass
);
$imap->select("INBOX") or die "Could not select INBOX: $@\n";
my @recent = $imap->search("recent") or die "No recent msgs in INBOX\n";
foreach my $id (@recent) {
my $bsdat = $imap->fetch( $id, "bodystructure" );
my $bso = Mail::IMAPClient::BodyStructure->new( join("", $imap->History) );
my $mime = $bso->bodytype . "/" . $bso->bodysubtype;
my $parts = map( "\n\t" . $_, $bso->parts );
print "Msg $id (Content-type: $mime) contains these parts:$parts\n";
}
π¦ DESCRIPTION π¦
Okay so here's the vibe: this extension parses the result of an IMAP FETCH BODYSTRUCTURE command and turns it into a perl data structure. Iconic. It also hands you a bunch of helper methods so you can actually pull the info back out of that data structure without crying π
This module requires Parse::RecDescent. No cap, you need it.
π Class Methods π
The following class method is available:
π new
This class method is the constructor method for instantiating new Mail::IMAPClient::BodyStructure objects. It accepts exactly one argument: a string containing a server response to a FETCH BODYSTRUCTURE directive. That's it. That's the whole input π
The module Mail::IMAPClient provides the get_bodystructure convenience method to simplify use of this module when you're starting with just a message's sequence number or unique ID (UID). Very much a "let me handle that for you" moment π«Ά
π Object Methods π
The following object methods are available:
β bodytype
Requires no arguments. Returns the bodytype for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodysubtype
Requires no arguments. Returns the bodysubtype for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodyparms
Requires no arguments. Returns the bodyparms for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodydisp
Requires no arguments. Returns the bodydisp for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodyid
Requires no arguments. Returns the bodyid for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodydesc
Requires no arguments. Returns the bodydesc for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodyenc
Requires no arguments. Returns the bodyenc for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodysize
Requires no arguments. Returns the bodysize for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodylang
Requires no arguments. Returns the bodylang for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β bodystructure
Requires no arguments. Returns the bodystructure for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
β envelopestruct
Requires no arguments. Returns a Mail::IMAPClient::BodyStructure::Envelope object for the message, from the calling Mail::IMAPClient::Bodystructure object. A whole little object, just for you π§
β textlines
Requires no arguments. Returns the textlines for the message whose structure is described by the calling Mail::IMAPClient::Bodystructure object.
πΈ Mail::IMAPClient::BodyStructure::Envelope πΈ
So the IMAP standard says output from the IMAP FETCH ENVELOPE command will be an RFC2060 envelope structure. It further says that output from the FETCH BODYSTRUCTURE command may also contain embedded envelope structures β like, if a message's subparts contain one or more included messages, that's where they'd be hiding π
Objects belonging to Mail::IMAPClient::BodyStructure::Envelope are Perl representations of these envelope structures. Which is a fancy way of saying: the nested parenthetical lists of RFC2060, translated into a Perl datastructure. Slay π
Bestie, listen β all of the fields relate to the specific part to which they belong. So output from aFETCH nnnn ENVELOPEcommand (or, inMail::IMAPClient,$imap->fetch($msgid,"ENVELOPE")ormy $env = $imap->get_envelope($msgid)) is for the message, but fields from within a bodystructure relate to the message subpart, not the parent message. Do not mix these up, I am begging π
An envelope structure's Mail::IMAPClient::BodyStructure::Envelope representation is a hash of thingies that looks like this:
{
subject => "subject",
inreplyto => "reference_message_id",
from => [ addressStruct1 ],
messageid => "message_id",
bcc => [ addressStruct1, addressStruct2 ],
date => "Tue, 09 Jul 2002 14:15:53 -0400",
replyto => [ adressStruct1, addressStruct2 ],
to => [ adressStruct1, addressStruct2 ],
sender => [ adressStruct1 ],
cc => [ adressStruct1, addressStruct2 ],
}
The ...::Envelope object also has methods for accessing data in the structure. They are:
π¦ date
Returns the date of the message.
π¦ inreplyto
Returns the message id of the message to which this message is a reply.
π¦ subject
Returns the subject of the message.
π¦ messageid
Returns the message id of the message.
You can also use the following methods to get addressing information. Each of these methods returns an array of Mail::IMAPClient::BodyStructure::Address objects, which are perl data structures representing RFC2060 address structures π
Some of these arrays would naturally contain one element (such as from, which normally contains a single "From:" address); others will often contain more than one address. But because RFC2060 defines all of these as "lists of address structures", they are all translated into arrays of ...::Address objects. Rules are rules, periodt.
See the section on Mail::IMAPClient::BodyStructure::Address, below, for alternate (and preferred) ways of accessing these data π
The methods available are:
π bcc
Returns an array of blind cc'ed recipients' address structures. (Don't expect much in here unless the message was sent from the mailbox you're poking around in, by the way.)
π cc
Returns an array of cc'ed recipients' address structures.
π from
Returns an array of "From:" address structures β usually just one.
π replyto
Returns an array of "Reply-to:" address structures. Once again there is usually just one address in the list.
π sender
Returns an array of senders' address structures β usually just one, and usually the same as from.
π to
Returns an array of recipients' address structures.
Each of the methods that returns a list of address structures (i.e. a list of Mail::IMAPClient::BodyStructure::Address arrays) also has an analogous method that will return a list of E-Mail addresses instead. The addresses are in the format personalname <mailboxname@hostname> (see the section on Mail::IMAPClient::BodyStructure::Address, below). However, if the personal name is 'NIL' then it is omitted from the address. Gone. Not invited to the party π
These methods are:
π bcc_addresses
Returns a list (or an array reference if called in scalar context) of blind cc'ed recipients' email addresses. (Don't expect much in here unless the message was sent from the mailbox you're poking around in, by the way.)
π cc_addresses
Returns a list of cc'ed recipients' email addresses. If called in a scalar context it returns a reference to an array of email addresses.
π from_addresses
Returns a list of "From:" email addresses. If called in a scalar context it returns the first email address in the list. (It's usually a list of just one anyway.)
π replyto_addresses
Returns a list of "Reply-to:" email addresses. If called in a scalar context it returns the first email address in the list.
π sender_addresses
Returns a list of senders' email addresses. If called in a scalar context it returns the first email address in the list.
π to_addresses
Returns a list of recipients' email addresses. If called in a scalar context it returns a reference to an array of email addresses.
Ngl, context affects the behavior of all of the above methods. Scalar context and list context are two completely different girlies here π
Those fields that will commonly contain multiple entries (i.e. they are recipients) will return an array reference when called in scalar context. You can use this behavior to optimize performance π₯
Those fields that will commonly contain just one address (the sender's) will return the first (and usually only) address. You can use this behavior to optimize your development time. Which is the real flex, honestly π
π Addresses and the Mail::IMAPClient::BodyStructure::Address π
Okay so bestie, several components of an envelope structure are literally address structures. Mail::IMAPClient parses each one into its very own object, Mail::IMAPClient::BodyStructure::Address, and it looks like this:
{
mailboxname => 'somebody.special',
hostname => 'somplace.weird.com'
personalname => 'Somebody Special
sourceroute => 'NIL'
}
Yes, copy that exactly β quirks, missing bits, and all. π«Ά
RFC2060 says that each address component of a bodystructure is a list of address structures, so Mail::IMAPClient::BodyStructure parses each of these into an array of Mail::IMAPClient::BodyStructure::Address objects. Itβs giving βone address, but make it a whole arrayβ energy. β¨
Each of these objects has the following methods available to it:
mailboxnameβ returns the"mailboxname"portion of the address, which is the part to the left of the'@'sign.hostnameβ returns the"hostname"portion of the address, which is the part to the right of the'@'sign.personalnameβ returns the"personalname"portion of the address, which is the part of the address thatβs treated like a comment.sourcerouteβ returns the"sourceroute"portion of the address, which is typically"NIL".
Taken together, the parts of an address structure form an address that will look something like this:
"personalname <mailboxname@hostname>"
And hereβs the little note, because this is important: because the Mail::IMAPClient::BodyStructure::Address objects come in arrays, itβs generally easier to use the methods available to Mail::IMAPClient::BodyStructure::Envelope to obtain all of the addresses in a particular array in one operation. These methods are provided, however, in case youβd rather do things the hard way. (And also because the aforementioned methods from Mail::IMAPClient::BodyStructure::Envelope need them anyway.) No cap β sometimes the βhard wayβ is just the supporting-character way. π
βοΈ AUTHOR βοΈ
Original author: David J. Kernen; Reworked by: Mark Overmeer; Maintained by Phil Pearl.
π SEE ALSO π
perl(1), Mail::IMAPClient, Parse::RecDescent, and RFC2060.
xoxo, your Unix bestie π β this Mail::I...tructure(3) page comes from perl v5.40.2, dated 2024-10-16. π
π¦ where this page comes from
- alpine 3.22: perl-mail-imapclient-doc 3.43-r2 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms