perldoc > Mail::Message::Body

๐Ÿ“Œ NAME

๐Ÿš€ Quick Reference

Use CaseCommandDescription
Get body from message$body = $msg->bodyRetrieve the body object of a message
Read body as lines@lines = $body->linesReturn content as list of lines
Read body as string$text = $body->stringReturn content as single string
Decode body to plain text$decoded = $body->decodedConvert to simplified representation (text/plain, no encoding)
Encode body for transport$encoded = $body->encode(%options)Encode with specified MIME type, charset, transfer encoding
Get MIME type$type = $body->mimeTypeReturns MIME::Type object
Get number of lines$n = $body->nrLinesReturns line count
Get body size$size = $body->sizeTotal bytes (in current encoding)
Write body to file$body->write(filename => $file)Write content to a file
Print body to handle$body->print($fh)Print to file handle (defaults to STDOUT)
Check if multipart$body->isMultipartReturns true if body contains sub-parts
Check if delayed$body->isDelayedTrue if body not yet read from file

๐Ÿงฌ INHERITANCE

๐Ÿ“– SYNOPSIS

my Mail::Message $msg = ...;
my $body  = $msg->body;
my @text  = $body->lines;
my $text  = $body->string;
my $file  = $body->file;  # IO::File
$body->print(\*FILE);

my $content_type = $body->type;
my $transfer_encoding = $body->transferEncoding;
my $encoded = $body->encode(mime_type => 'text/html',
   charset => 'us-ascii', transfer_encoding => 'none');
my $decoded = $body->decoded;

๐Ÿ“ DESCRIPTION

The encoding and decoding functionality of a Mail::Message::Body is implemented in the Mail::Message::Body::Encode package. That package is automatically loaded when encoding and decoding of messages needs to take place. Methods to simply build an process body objects are implemented in Mail::Message::Body::Construct.

The body of a message (a Mail::Message object) is stored in one of the many body types. The functionality of each body type is equivalent, but there are performance differences. Each body type has its own documentation with details about its implementation.

Extends "DESCRIPTION" in Mail::Reporter.

โšก OVERLOADED

๐Ÿ”ค overload: ""

(stringification) Returns the body as string โ€” which will trigger completion โ€” unless called to produce a string for "Carp". The latter to avoid deep recursions.

Example: stringification of body

 print $msg->body;   # implicit by print

 my $body = $msg->body;
 my $x    = "$body"; # explicit by interpolation

๐Ÿ”ข overload: '==' and '!='

(numeric comparison) compares if two references point to the same message. This only produces correct results is both arguments are message references within the same folder.

Example: use of numeric comparison on a body

 my $skip = $folder->message(3);
 foreach my $msg (@$folder)
 {   next if $msg == $skip;
     $msg->send;
 }

๐Ÿ“ฆ overload: @{}

When a body object is used as being an array reference, the lines of the body are returned. This is the same as using lines().

Example: using a body as array

 print $body->lines->[1];  # second line
 print $body->[1];         # same

 my @lines = $body->lines;
 my @lines = @$body;       # same

โœ… overload: bool

Always returns a true value, which is needed to have overloaded objects to be used as in "if($body)". Otherwise, "if(defined $body)" would be needed to avoid a runtime error.

๐Ÿ”ง METHODS

Extends "METHODS" in Mail::Reporter.

๐Ÿ—๏ธ Constructors

Extends "Constructors" in Mail::Reporter.

๐Ÿ› ๏ธ Constructing a body

๐Ÿ“ฆ The body

๐Ÿ“‹ About the payload

๐Ÿ” Access to the payload

โš™๏ธ Internals

โš ๏ธ Error handling

Extends "Error handling" in Mail::Reporter.

๐Ÿงน Cleanup

Extends "Cleanup" in Mail::Reporter.

๐Ÿ”ฌ DETAILS

๐Ÿ”‘ Access to the body

A body can be contained in a message, but may also live without a message. In both cases it stores data, and the same questions can be asked: what type of data it is, how many bytes and lines, what encoding is used. Any body can be encoded and decoded, returning a new body object. However, bodies which are part of a message will always be in a shape that they can be written to a file or send to somewhere: they will be encoded if needed.

Example

 my $body    = Mail::Message::Body::String->new(mime_type => 'image/gif');
 $body->print(\*OUT);    # this is binary image data...

 my $encoded = $message->body($body);
 $encoded->print(\*OUT); # ascii data, encoded image

Now encoded refers to the body of the $message which is the content of $body in a shape that it can be transmitted. Usually "base64" encoding is used.

๐Ÿ“‚ Body class implementation

The body of a message can be stored in many ways. Roughly, the implementations can be split in two groups: the data collectors and the complex bodies. The primer implement various ways to access data, and are full compatible: they only differ in performance and memory footprint under different circumstances. The latter are created to handle complex multiparts and lazy extraction.

Data collector bodies

Complex bodies

๐Ÿ”ค Character encoding PERL

A body object can be part of a message, or stand-alone. In case it is a part of a message, the "transport encoding" and the content must be in a shape that the data can be transported via SMTP.

However, when you want to process the body data in simple Perl (or when you construct the body data from normal Perl strings), you need to be aware of Perl's internal representation of strings. That can either be latin1 or utf8 (not real UTF-8, but something alike, see the perlunicode manual page) So, before you start using the data from an incoming message, do

    my $body  = $msg->decoded;
    my @lines = $body->lines;

Now, the body has character-set 'PERL' (when it is text)

When you create a new body which contains text content (the default), it will be created with character-set 'PERL' unless you specify a character-set explicitly.

   my $body = Mail::Box::Body::Lines->new(data => \@lines);
   # now mime=text/plain, charset=PERL

   my $msg  = Mail::Message->buildFromBody($body);
   $msg->body($body);
   $msg->attach($body);   # etc
   # these all will convert the charset=PERL into real utf-8

๐Ÿฉบ DIAGNOSTICS

๐Ÿ“š SEE ALSO

This module is part of Mail-Message distribution version 3.012, built on February 11, 2022. Website: http://perl.overmeer.net/CPAN/

๐Ÿ“œ LICENSE

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/

Mail::Message::Body
๐Ÿ“Œ NAME ๐Ÿš€ Quick Reference ๐Ÿงฌ INHERITANCE ๐Ÿ“– SYNOPSIS ๐Ÿ“ DESCRIPTION โšก OVERLOADED
๐Ÿ”ค overload: "" ๐Ÿ”ข overload: '==' and '!=' ๐Ÿ“ฆ overload: @{} โœ… overload: bool
๐Ÿ”ง METHODS
๐Ÿ—๏ธ Constructors ๐Ÿ› ๏ธ Constructing a body ๐Ÿ“ฆ The body ๐Ÿ“‹ About the payload ๐Ÿ” Access to the payload โš™๏ธ Internals โš ๏ธ Error handling ๐Ÿงน Cleanup
๐Ÿ”ฌ DETAILS
๐Ÿ”‘ Access to the body ๐Ÿ“‚ Body class implementation ๐Ÿ”ค Character encoding PERL
๐Ÿฉบ DIAGNOSTICS ๐Ÿ“š SEE ALSO ๐Ÿ“œ LICENSE

Generated by phpman v4.9.29 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-22 00:48 @2600:1f28:365:80b0:d8a5:c3c6:bf94:28c0
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^