Sample code for 30+ languages & platforms
Perl Requires Chilkat v11.0.0+

Inspect the MIME parts of a Gmail message

See more GMail REST API Examples

Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

# Assumes Chilkat is unlocked. See Global Unlock Sample.

my $token = Chilkat2::JsonObject->new;
my $success = $token->LoadFile('gmail-token.json');
if (!$success) {
    print "Could not read gmail-token.json from the private working directory.\n";
    exit;
}

if (($token->JsonTypeOf('access_token') != 1) || ($token->StringOfEquals('access_token', '', 1))) {
    print "An OAuth access token is required.\n";
    exit;
}

my $http = Chilkat2::Http->new;
$http->RequireSslCertVerify(1);
$http->AuthToken($token->StringOf('access_token'));
my $resp = Chilkat2::HttpResponse->new;
my $reply = Chilkat2::JsonObject->new;
my $url = Chilkat2::StringBuilder->new;
$url->Append('https://gmail.googleapis.com/gmail/v1/users/me/messages/');
$url->Append('GMAIL_MESSAGE_ID');
$url->Append('?format=full');
$success = $http->HttpNoBody('GET', $url->GetAsString(), $resp);
if ($resp->StatusCode == 0) {
    print "Gmail did not return an HTTP response.\n";
    exit;
}

if ((!$success) || (($resp->StatusCode < 200) || ($resp->StatusCode >= 300))) {
    print 'Gmail HTTP ', $resp->StatusCode, "\n";
    $success = $reply->Load($resp->BodyStr);
    if ($success) {
        print $reply->StringOf('error.message'), "\n";
    }

    exit;
}

$success = $reply->Load($resp->BodyStr);
if (!$success) {
    print "Expected a Gmail JSON response.\n";
    exit;
}

if ($reply->JsonTypeOf('payload') != 3) {
    print "Expected a MIME payload.\n";
    exit;
}

# Queue paths into the original JSON document so any MIME nesting depth is supported.
my $paths = Chilkat2::StringTable->new;
$success = $paths->Append('payload');
if (!$success) {
    print "Could not queue the root part.\n";
    exit;
}

my $part = Chilkat2::JsonObject->new;
my $childPath = Chilkat2::StringBuilder->new;
my $p = 0;
while ($p < $paths->Count) {
    $success = $reply->ObjectOf2($paths->StringAt($p), $part);
    if (!$success) {
        print "Could not read a MIME part.\n";
        exit;
    }

    print 'Part: ', $part->StringOf('mimeType'), '  bytes: ', $part->IntOf('body.size'), "\n";
    if ($part->JsonTypeOf('filename') == 1) {
        if (!$part->StringOfEquals('filename', '', 1)) {
            print 'Filename: ', $part->StringOf('filename'), "\n";
        }
    }

    if ($part->JsonTypeOf('body.attachmentId') == 1) {
        print 'Attachment ID: ', $part->StringOf('body.attachmentId'), "\n";
    } else {
        if ($part->JsonTypeOf('body.data') == 1) {
            print "Body data is available in this part.\n";
        }
    }

    my $j = 0;
    while ($j < $part->SizeOfArray('parts')) {
        $childPath->SetString($paths->StringAt($p));
        $childPath->Append('.parts[');
        $childPath->AppendInt($j);
        $childPath->Append(']');
        $success = $paths->Append($childPath->GetAsString());
        if (!$success) {
            print "Could not queue a child part.\n";
            exit;
        }

        $j = $j + 1;
    }

    $p = $p + 1;
}