Sample code for 30+ languages & platforms
Perl

MIME Body vs. Sub-Parts

See more MIME Examples

Explains the difference between sub-parts and body.

MIME documents (i.e. messages) can have a nested structure. The simplest MIME message contains a header followed by content (possibly encoded). The header is delimited from the content by two consecutive CRLF's.

A MIME message may be multipart. If so, then the Content-Type header field indicates "multipart" and the content the follows the header is itself a MIME message, which may also be multipart, etc. You can see how MIME messages effectively have a tree structure. The non-leaf nodes have Content-Types that are "multipart/" and the leaf nodes have non-multipart Content-Types.

The Chilkat MIME component/library uses two terms that need to be understood in order to effectively use the API. These are "body" and "sub-part". A multipart node has 1 or more sub-parts, but its body is empty. A leaf node (i.e. non-multipart node) has no sub-parts, but the body is (usually) non-empty.

This example creates a multipart MIME message and shows the results of getting the sub-parts and body of each node.

Chilkat Perl Downloads

Chilkat2 Perl
use strict;
use warnings;
use Chilkat2;

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

my $mime = Chilkat2::Mime->new;

# Create a multipart/mixed MIME message with two sub-parts.
# We'll use the Base64 encoding for the 2nd sub-part.
my $part1 = Chilkat2::Mime->new;
my $success = $part1->SetBodyFromPlainText('This is part 1');

my $part2 = Chilkat2::Mime->new;
$success = $part2->SetBodyFromPlainText('This is part 2');
$part2->Encoding('base64');

$success = $mime->NewMultipartMixed();
$mime->UseMmDescription(0);
$success = $mime->AppendPart($part1);
$success = $mime->AppendPart($part2);

# Show the MIME message:
print $mime->GetMime(), "\n";

# Here's the MIME:
# Content-Type: multipart/mixed;
#     boundary="------------040605030407000302060008";
#
# --------------040605030407000302060008
# Content-Type: text/plain;
#      charset="us-ascii";
# Content-Transfer-Encoding: 7bit
#
# This is part 1
# --------------040605030407000302060008
# Content-Type: text/plain;
#      charset="us-ascii";
# Content-Transfer-Encoding: base64
#
# VGhpcyBpcyBwYXJ0IDI=
#
# --------------040605030407000302060008--

# If we examine the root node of the MIME message,
# we see that it has 2 sub-parts and the body is empty (as expected)
my $n = $mime->NumParts;
print 'Num Parts = ', $n, "\n";
print 'Body = ', $mime->GetBodyDecoded(), "\n";

# The GetEntireBody retrieves the entire content after
# the header.  (Perhaps GetEntireBody should've been named
# GetEntireContent to make it less confusing...)
print "---- EntireBody:\n";
print $mime->GetEntireBody(), "\n";
print "********\n";

# Now examine the 2nd sub-part.  It has a body encoded
# using base64.  Get the contents of the body in both
# decoded and encoded forms:

# index 0 is the 1st part, index 1 is the 2nd part.
my $part = Chilkat2::Mime->new;
$mime->PartAt(1, $part);

print "Decoded Body:\n";
print $part->GetBodyDecoded(), "\n";
print "Encoded Body:\n";
print $part->GetBodyEncoded(), "\n";