Mail::...Message(3pm)
π Mail::SpamAssassin::Message β decode, render, and hold an RFC-2822 message π
okay bestie, so this is Mail::...Message(3pm) from the User Contributed Perl Documentation, and yes the name got a little squished but it's literally Mail::SpamAssassin::Message and it's the whole vibe of the SpamAssassin mail-handling world. let's get into it π«Ά
β¨ DESCRIPTION β¨
so this module is the main character: it encapsulates an email message and lets you get at all the various MIME message parts and the message metadata. iconic behavior, no cap.
once you've kicked off a parse() cycle, the message structure looks like this:
Message object, also top-level node in Message::Node tree
|
+---> Message::Node for other parts in MIME structure
| |---> [ more Message::Node parts ... ]
| [ others ... ]
|
+---> Message::Metadata object to hold metadata
it's giving tree. it's giving hierarchy. we love her π
π PUBLIC METHODS π
new()β Creates aMail::SpamAssassin::Messageobject. Takes a hash reference as a parameter. The used hash key/value pairs are as follows:"message"is eitherundef(which will use STDIN), a scalar β a string containing an entire message, a reference to such string, an array reference of the message with one line per array element, or either a file glob or anIO::Fileobject which holds the entire contents of the message.
Note: The message is expected to generally be in RFC 2822 format, optionally including an mbox message separator line (the "From " line) as the first line. π
"parse_now"specifies whether or not to create the MIME tree at object-creation time or later as necessary.
The parse_now option, by default, is set to false (0). This allows SpamAssassin to not have to generate the tree of Mail::SpamAssassin::Message::Node objects and their related data if the tree is not going to be used. This is handy, for instance, when running "spamassassin -d", which only needs the pristine header and body which is always handled when the object is created. lazy loading but make it β¨efficientβ¨
"subparse"specifies how many MIME recursion levels should be parsed. Defaults to20. that's deep bestie, don't go too far down there πΈ
find_parts()β Used to search the tree for specific MIME parts. SeeMail::SpamAssassin::Message::Nodefor more details. scavenger hunt energy πget_pristine_header()β Returns pristine headers of the message. If no specific header name is given as a parameter (case-insensitive), then all headers will be returned as a scalar, including the blank line at the end of the headers.
If called in an array context, an array will be returned with each specific header in a different element. In a scalar context, the last specific header is returned.
ie: If 'Subject' is specified as the header, and there are 2 Subject headers in a message, the last/bottom one in the message is returned in scalar context or both are returned in array context.
Btw, returning the last header field (not the first) happens to be consistent with DKIM signatures, which search for and cover multiple header fields bottom-up according to the 'h' tag. Let's keep it this way. this is intentional bestie, don't come for her
Note: the returned header will include the ending newline and any embedded whitespace folding. π§
get_mbox_separator()β Returns the mbox separator found in the message, orundefif there wasn't one. sometimes the answer is just nothing, and that's valid πget_body()β Returns an array of the pristine message body, one line per array element.get_pristine()β Returns a scalar of the entire pristine message.get_pristine_body()β Returns a scalar of the pristine message body.get_pristine_body_digest()β Returns SHA1 hex digest of the pristine message body. CRLF line endings are normalized to LF before hashing. fingerprinting the body, slay π₯get_msgid()β Returns Message-ID header for the message, with<>and surrounding whitespace removed. Returnsundef, if nothing found between<>. those little angle brackets get stripped, friends πgenerate_msgid()β Generate a calculated"Message-ID"insha1hex@sa_generatedformat, usingTo,Dateheaders and pristine body as source for hashing. making your own ID when the message won't provide one, main character manufacturing πextract_message_metadata($permsgstatus)β Extracts message metadata from the email, it's called once per message. one and done, periodt.$str = get_metadata($hdr)β Returns an item from the metadata structure.put_metadata($hdr, $text)β Inserts an item into the metadata structure.delete_metadata($hdr)β Removes an item from the metadata structure. bye boo π$str = get_all_metadata()β Returns all metadata as a string if in scalar context, otherwise as an array. the whole collection, bestie πfinish_metadata()β Destroys the metadata for this message. Once a message has been scanned fully, the metadata is no longer required. Destroying this will free up some memory. spring cleaning, but for RAM π§Ήfinish()β Clean up an object so that it can be destroyed.receive_date()β Return atime_tvalue with the received date of the current message, or current time if received time couldn't be determined. we're clocking her β°
π¦ PARSING METHODS, NON-PUBLIC π¦
okay so these are the backstage girlies. These methods take a RFC2822-esque formatted message and create a tree with all of the MIME body parts included. Those parts will be decoded as necessary, and text/html parts will be rendered into a standard text format, suitable for use in SpamAssassin. no cap, this is the pipeline πΈ
parse_body()βparse_body()passes the body part that was passed in onto the correct part parser, either_parse_multipart()for multipart/* parts, or_parse_normal()for everything else. Multipart sections become the root of sub-trees, while everything else becomes a leaf in the tree.
For multipart messages, the first call to parse_body() doesn't create a new sub-tree and just uses the parent node to contain children. All other calls to parse_body() will cause a new sub-tree root to be created and children will exist underneath that root. (this is just so the tree doesn't have a root node which points at the actual root node ...) π
imagine having a root pointing at the root. embarrassing. we don't do that here.
_parse_multipart()β Generate a root node, and for each child part callparse_body()to generate the tree._parse_normal()β Generate a leaf node and add it to the parent.
okay that's the whole page bestie, you ate π and just so you know the vibe-check details: this shipped as part of perl v5.40.1 and the date on the tin is 2026-07-23. go forth and parse those MIME parts, you monster πβ¨π
π¦ where this page comes from
- ubuntu stonking: spamassassin 4.0.2+svn1935691-1build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms