Mail::M...::Encode(3)
π Mail::M...::Encode(3) β bestie edition π
okay so this is the User Contributed Perl Documentation page for Mail::M...::Encode, and it's literally all about how your message bodies get their encoding glow-up. Sit down, grab a boba, we're doing this. π§
β¨ NAME β¨
Mail::Message::Body::Encode β organize general message encodings π
πΈ SYNOPSIS πΈ
my Mail::Message $msg = ...;
my $decoded = $msg->decoded;
my $encoded = $msg->encode(mime_type => 'image/gif',
transfer_encoding => 'base64');
my $body = $msg->body;
my $decoded = $body->decoded;
my $encoded = $body->encode(transfer_encoding => '7bit');
see how easy that is?? two whole vibes: decode it, encode it. slay. π
π¦ DESCRIPTION π¦
So. This package handles the message's body encodings and decodings whenever the main program asks for it. It's the one adding extra functionality to the Mail::Message::Body class the moment you call decoded() or encode(). Not me giving the body class a whole new personality. π
There are four types of encodings being handled, and yes bestie, they happen in the right order:
- eol encoding β every operating system woke up and chose its own idea of line termination. UNIX uses a
LFcharacter, MacOS uses aCR, and Windows uses aCR/LFcombination. Messages transported over the Internet will always use theCRLFseparator. periodt. - transfer encoding β messages sent over the Internet have to be plain ASCII. So complicated characters and binary files (hi, images and archives π) must get encoded into an ASCII representation during transmission. The implementation of the required encoders and decoders lives in the
Mail::Message::TransferEncset of packages. That related manual page lists the transfer encodings that are supported. iykyk. β¨ - mime-type translation β NOT IMPLEMENTED YET π (the audacity, honestly)
- charset conversion β π
π METHODS π
π Attributes π
$obj->charsetDetectAlgorithm( [CODE|undef|METHOD] )β [3.013] When a body object doesn't specify its character-set but that detail is required, it gets autodetected. The default algorithm is implemented incharsetDetect(). You may change this default algorithm, or pass the optioncharset_detectfor each call toencode(). When you call this method with an explicitundef, you reset the default. Without a parameter, the current algorithm (CODEor method name) is returned. π«Ά
π Constructing a body π
$obj->charsetDetect(%options)β [3.013] bestie this one is tricky. It is hard to detect whether the body originates from the program, or from an external source. And what about a database database? are those octets or strings? π Please read "Autodetection of character-set" inMail::Message::Body.
Here's the option table, exactly as written:
-Option --Default
external <C<false>>
external => BOOLEANβ Do only consider externally valid character-sets, implicitly:"PERL"is not an acceptable answer. π ββοΈ
$obj->check()β Check the content of the body not to include illegal characters. Which characters are considered illegal depends on the encoding of this body. A body is returned which is checked. This may be the body where this method is called upon, but also a new object, when serious changes had to be made. If the check could not be made, because the decoder is not defined, thenundefis returned. π$obj->encode(%options)β Encode (translate) aMail::Message::Bodyinto a different format. See the DESCRIPTION above. Options which are not specified will not trigger conversions.
-Option --Default
charset PERL
charset_detect <built-in>
mime_type undef
result_type <same as source>
transfer_encoding undef
charset => CHARSET|'PERL'β Only applies when the mime_type is textual. If the CHARSET is explicitly specified (for instance"iso-8859-10"), then the data is being interpreted as raw bytes (blob), not as text. However, in case of"PERL", it is considered to be an internal representation of characters (either latin1 or Perl's utf8 β not the same as utf-8 β, you should not want to know). π This setting overrules the charset attribute in the mime_type FIELD.charset_detect => CODEβ [3.013] When the body does not contain an explicit charset specification, then the RFC says it is"us-ascii". In reality, this is not"true": it is just an unknown character set. This often happens when text files are included as attachment, for instance a footer attachment. When you want to be smarter than the default charset detector, you can provide your own function for this parameter. The function will get the transfer-decoded version of this body. You can change the default globally viacharsetDetectAlgorithm().mime_type => STRING|FIELDβ Convert into the specified mime type, which can be specified as STRING or FIELD. The FIELD is aMail::Message::Field-object, representing a"Content-Type"mime header. The STRING must be valid content for such header, and will be converted into a FIELD object. The FIELD may contain attributes. Usually, it has a"charset"attribute, which explains the CHARSET of the content after content transfer decoding. The"charset"option will update/add this attribute. Otherwise (hopefully in rare cases) the CHARSET will be auto-detected when the body gets decoded.result_type => CLASSβ The type of body to be created when the body is changed to fulfill the request on re-coding. Also the intermediate stages in the translation process (if needed) will use this type. CLASS must extendMail::Message::Body.transfer_encoding => STRING|FIELDβ that's it, that's the whole entry in the original. no extra lore given. πΈ
$obj->encoded(%options)β Encode the body to a format what is acceptable to transmit or write to a folder file. This returns the body where this method was called upon when everything was already prepared, or a new encoded body otherwise. In either case, the body is checked. β¨
-Option --Default
charset_detect <the default>
charset_detect => CODEβ SeecharsetDetectAlgorithm().
$obj->eol( ['CR'|'LF'|'CRLF'|'NATIVE'] )β Returns the character (or characters) which are used to separate lines within this body. When a kind of separator is specified, the body is translated to contain the specified line endings. π¦
Β» example:
my $body = $msg->decoded->eol('NATIVE');
my $char = $msg->decoded->eol;
$obj->unify($body)β Unify the type of the given$bodyobjects with the type of the called body."undef"is returned when unification is impossible. If the bodies have the same settings, the$bodyobject is returned unchanged.
Examples:
my $bodytype = Mail::Message::Body::Lines;
my $html = $bodytype->new(mime_type=>'text/html', data => []);
my $plain = $bodytype->new(mime_type=>'text/plain', ...);
my $unified = $html->unify($plain);
# $unified is the data of plain translated to html (if possible).
π About the payload π
$obj->dispositionFilename( [$directory] )β Various fields are searched for"filename"and"name"attributes. Without$directory, the name found will be returned unmodified. When a$directoryis given, a filename is composed. For security reasons, only the basename of the found name gets used and many potentially dangerous characters removed. If no name was found, or when the found name is already in use, then an unique name is generated. π‘οΈ Don't forget to read RFC6266 section 4.3 for the security aspects in your email application. fr fr, don't skip that one.$obj->isBinary()β Returns"true"when the un-encoded message is binary data. This information is retrieved from knowledge provided byMIME::Types.$obj->isText()β Returns"true"when the un-encoded message contains printable text. π
π§ Internals π§
$any->addTransferEncHandler( $name, <$class|$object> )β Relate the NAMEd transfer encoding to an OBJECTs or object of the specified$class. In the latter case, an object of that$classwill be created on the moment that one is needed to do encoding or decoding. The$classor$objectmust extendMail::Message::TransferEnc. It will replace existing class and object for this$name. Why aren't you contributing this class to MailBox? π$obj->getTransferEncHandler($type)β Get the transfer encoder/decoder which is able to handle$type, or return"undef"if there is no such handler.
π DIAGNOSTICS π
- Warning: Content-Transfer-Encoding '$te' requires explicit charset, defaulted to utf-8. Cast by
encode() - Warning: charset '$name' is not known. The encoding or decoding of a message body encounters a character set which is not understood by Perl's
Encodemodule. Cast byencode() - Warning: no decoder defined for transfer encoding '$encoding'. The data (message body) is encoded in a way which is not currently understood, therefore no decoding (or recoding) can take place. Cast by
encode() - Warning: no encoder defined for transfer encoding '$encoding'. The data (message body) has been decoded, but the required encoding is unknown. The decoded data is returned. Cast by
encode() - Error: unknown line terminator '$eol'. Cast by
eol()
π₯ SEE ALSO π₯
This module is part of Mail-Message version 4.04, built on February 04, 2026. Website: http://perl.overmeer.net/CPAN/ β¨
π LICENSE π
For contributors see file ChangeLog.
This software is copyright (c) 2001-2026 by Mark Overmeer. π«Ά
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page bestie, no cap π this lil manual came from the perl v5.42.0 build dated 2026-02-05 and it's filed under Mail::M...::Encode(3). go forth and encode responsibly, queen πππ¦
π¦ where this page comes from
- fedora 44: perl-Mail-Message 4.04-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms