{
    "mode": "perldoc",
    "parameter": "Mail::Message::Body::Encode",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Mail%3A%3AMessage%3A%3ABody%3A%3AEncode/json",
    "generated": "2026-09-15T06:48:25Z",
    "synopsis": "my Mail::Message $msg = ...;\nmy $decoded = $msg->decoded;\nmy $encoded = $msg->encode(mimetype => 'image/gif',\ntransferencoding => 'base64');\nmy $body = $msg->body;\nmy $decoded = $body->decoded;\nmy $encoded = $body->encode(transferencoding => '7bit');",
    "sections": {
        "NAME": {
            "content": "Mail::Message::Body::Encode - organize general message encodings\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "my Mail::Message $msg = ...;\nmy $decoded = $msg->decoded;\nmy $encoded = $msg->encode(mimetype => 'image/gif',\ntransferencoding => 'base64');\n\nmy $body = $msg->body;\nmy $decoded = $body->decoded;\nmy $encoded = $body->encode(transferencoding => '7bit');\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Manages the message's body encodings and decodings on request of the main program. This package\nadds functionality to the Mail::Message::Body class when the decoded() or encode() method is\ncalled.\n\nFour types of encodings are handled (in the right order)\n\n*   eol encoding\n\nVarious operating systems have different ideas about how to encode the line termination.\nUNIX uses a LF character, MacOS uses a CR, and Windows uses a CR/LF combination. Messages\nwhich are transported over Internet will always use the CRLF separator.\n\n*   transfer encoding\n\nMessages transmitted over Internet have to be plain ASCII. Complicated characters and binary\nfiles (like images and archives) must be encoded during transmission to an ASCII\nrepresentation.\n\nThe implementation of the required encoders and decoders is found in the\nMail::Message::TransferEnc set of packages. The related manual page lists the transfer\nencodings which are supported.\n\n*   mime-type translation\n\nNOT IMPLEMENTED YET\n\n*   charset conversion\n",
            "subsections": []
        },
        "METHODS": {
            "content": "",
            "subsections": [
                {
                    "name": "Constructing a body",
                    "content": "$obj->charsetDetect(%options)\n[3.013] This is tricky. It is hard to detect whether the body originates from the program,\nor from an external source. And what about a database database? are those octets or strings?\nPlease read \"Autodetection of character-set\" in Mail::Message::Body.\n\n-Option  --Default\nexternal  <false>\n\nexternal => BOOLEAN\nDo only consider externally valid character-sets, implicitly: \"PERL\" is not an acceptable\nanswer.\n\nMail::Message::Body->charsetDetectAlgorithm( [CODE|undef|METHOD] )\n[3.013] When a body object does not specify its character-set, but that detail is required,\nthen it gets autodetected. The default algorithm is implemented in charsetDetect(). You may\nchange this default algorithm, or pass option \"charsetdetect\" for each call to encode().\n\nWhen you call this method with an explicit \"undef\", you reset the default. (Without\nparameter) the current algorithm (CODE or method name) is returned.\n\n$obj->check()\nCheck the content of the body not to include illegal characters. Which characters are\nconsidered illegal depends on the encoding of this body.\n\nA body is returned which is checked. This may be the body where this method is called upon,\nbut also a new object, when serious changes had to be made. If the check could not be made,\nbecause the decoder is not defined, then \"undef\" is returned.\n\n$obj->encode(%options)\nEncode (translate) a Mail::Message::Body into a different format. See the DESCRIPTION above.\nOptions which are not specified will not trigger conversions.\n\n-Option           --Default\ncharset            PERL\ncharsetdetect     <built-in>\nmimetype          undef\nresulttype        <same as source>\ntransferencoding  undef\n\ncharset => CHARSET|'PERL'\nOnly applies when the mimetype is textual.\n\nIf the CHARSET is explicitly specified (for instance \"iso-8859-10\", then the data is being\ninterpreted as raw bytes (blob), not as text. However, in case of \"PERL\", it is considered\nto be an internal representation of characters (either latin1 or Perl's utf8 --not the\nsame as utf-8--, you should not want to know).\n\nThis setting overrules the charset attribute in the mimetype FIELD.\n\ncharsetdetect => CODE\n[3.013] When the body does not contain an explicit charset specification, then the RFC\nsays it is \"us-ascii\". In reality, this is not true: it is just an unknown character set.\nThis often happens when text files are included as attachment, for instance a footer\nattachment.\n\nWhen you want to be smarter than the default charset detector, you can provide your own\nfunction for this parameter. The function will get the transfer-decoded version of this\nbody. You can change the default globally via charsetDetectAlgorithm().\n\nmimetype => STRING|FIELD\nConvert into the specified mime type, which can be specified as STRING or FIELD. The FIELD\nis a Mail::Message::Field-object, representing a \"Content-Type\" mime header. The STRING\nmust be valid content for such header, and will be converted into a FIELD object.\n\nThe FIELD may contain attributes. Usually, it has a \"charset\" attribute, which explains\nthe CHARSET of the content after content transfer decoding. The \"charset\" option will\nupdate/add this attribute. Otherwise (hopefully in rare cases) the CHARSET will be\nauto-detected when the body gets decoded.\n\nresulttype => CLASS\nThe type of body to be created when the body is changed to fulfill the request on\nre-coding. Also the intermediate stages in the translation process (if needed) will use\nthis type. CLASS must extend Mail::Message::Body.\n\ntransferencoding => STRING|FIELD\n\n$obj->encoded(%options)\nEncode the body to a format what is acceptable to transmit or write to a folder file. This\nreturns the body where this method was called upon when everything was already prepared, or\na new encoded body otherwise. In either case, the body is checked.\n\n-Option        --Default\ncharsetdetect  <the default>\n\ncharsetdetect => CODE\nSee charsetDetectAlgorithm().\n\n$obj->unify($body)\nUnify the type of the given $body objects with the type of the called body. \"undef\" is\nreturned when unification is impossible. If the bodies have the same settings, the $body\nobject is returned unchanged.\n\nExamples:\n\nmy $bodytype = Mail::Message::Body::Lines;\nmy $html  = $bodytype->new(mimetype=>'text/html', data => []);\nmy $plain = $bodytype->new(mimetype=>'text/plain', ...);\n\nmy $unified = $html->unify($plain);\n# $unified is the data of plain translated to html (if possible).\n"
                },
                {
                    "name": "About the payload",
                    "content": "$obj->dispositionFilename( [$directory] )\nVarious fields are searched for \"filename\" and \"name\" attributes. Without $directory, the\nname found will be returned unmodified.\n\nWhen a $directory is given, a filename is composed. For security reasons, only the basename\nof the found name gets used and many potentially dangerous characters removed. If no name\nwas found, or when the found name is already in use, then an unique name is generated.\n\nDon't forget to read RFC6266 section 4.3 for the security aspects in your email application.\n\n$obj->isBinary()\nReturns true when the un-encoded message is binary data. This information is retrieved from\nknowledge provided by MIME::Types.\n\n$obj->isText()\nReturns true when the un-encoded message contains printable text.\n"
                },
                {
                    "name": "Internals",
                    "content": "$obj->addTransferEncHandler( $name, <$class|$object> )\nMail::Message::Body->addTransferEncHandler( $name, <$class|$object> )\nRelate the NAMEd transfer encoding to an OBJECTs or object of the specified $class. In the\nlatter case, an object of that $class will be created on the moment that one is needed to do\nencoding or decoding.\n\nThe $class or $object must extend Mail::Message::TransferEnc. It will replace existing class\nand object for this $name.\n\nWhy aren't you contributing this class to MailBox?\n\n$obj->getTransferEncHandler($type)\nGet the transfer encoder/decoder which is able to handle $type, or return undef if there is\nno such handler.\n"
                }
            ]
        },
        "DIAGNOSTICS": {
            "content": "Warning: Charset $name is not known\nThe encoding or decoding of a message body encounters a character set which is not\nunderstood by Perl's Encode module.\n\nWarning: No decoder defined for transfer encoding $name.\nThe data (message body) is encoded in a way which is not currently understood, therefore no\ndecoding (or recoding) can take place.\n\nWarning: No encoder defined for transfer encoding $name.\nThe data (message body) has been decoded, but the required encoding is unknown. The decoded\ndata is returned.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "This module is part of Mail-Message distribution version 3.015, built on December 11, 2023.\nWebsite: http://perl.overmeer.net/CPAN/\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "Copyrights 2001-2023 by [Mark Overmeer <markov@cpan.org>]. For other contributors see ChangeLog.\n\nThis program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself. See http://dev.perl.org/licenses/\n",
            "subsections": []
        }
    },
    "summary": "Mail::Message::Body::Encode - organize general message encodings",
    "flags": [],
    "examples": [],
    "see_also": []
}