PHP Requires Chilkat v11.0.0+
PHP
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.
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;
use Chilkat2\StringTable;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$token = new JsonObject();
try {
$token->LoadFile('gmail-token.json');
} catch (\Chilkat2\Exception $e) {
echo "Could not read gmail-token.json from the private working directory.\n";
exit;
}
if (($token->JsonTypeOf('access_token') != 1) || ($token->StringOfEquals('access_token', '', true))) {
echo "An OAuth access token is required.\n";
exit;
}
$http = new Http();
$http->RequireSslCertVerify = true;
$http->AuthToken = $token->StringOf('access_token');
$resp = new HttpResponse();
$reply = new JsonObject();
$url = new StringBuilder();
$url->Append('https://gmail.googleapis.com/gmail/v1/users/me/messages/');
$url->Append('GMAIL_MESSAGE_ID');
$url->Append('?format=full');
try {
$http->HttpNoBody('GET', $url->GetAsString(), $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($resp->StatusCode == 0) {
echo "Gmail did not return an HTTP response.\n";
exit;
}
if ((!$success) || (($resp->StatusCode < 200) || ($resp->StatusCode >= 300))) {
echo 'Gmail HTTP ', $resp->StatusCode, "\n";
try {
$reply->Load($resp->BodyStr);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ($success) {
echo $reply->StringOf('error.message'), "\n";
}
exit;
}
try {
$reply->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Expected a Gmail JSON response.\n";
exit;
}
if ($reply->JsonTypeOf('payload') != 3) {
echo "Expected a MIME payload.\n";
exit;
}
// Queue paths into the original JSON document so any MIME nesting depth is supported.
$paths = new StringTable();
try {
$paths->Append('payload');
} catch (\Chilkat2\Exception $e) {
echo "Could not queue the root part.\n";
exit;
}
$part = new JsonObject();
$childPath = new StringBuilder();
$p = 0;
while ($p < $paths->Count) {
try {
$reply->ObjectOf2($paths->StringAt($p), $part);
} catch (\Chilkat2\Exception $e) {
echo "Could not read a MIME part.\n";
exit;
}
echo 'Part: ', $part->StringOf('mimeType'), ' bytes: ', $part->IntOf('body.size'), "\n";
if ($part->JsonTypeOf('filename') == 1) {
if (!$part->StringOfEquals('filename', '', true)) {
echo 'Filename: ', $part->StringOf('filename'), "\n";
}
}
if ($part->JsonTypeOf('body.attachmentId') == 1) {
echo 'Attachment ID: ', $part->StringOf('body.attachmentId'), "\n";
} else {
if ($part->JsonTypeOf('body.data') == 1) {
echo "Body data is available in this part.\n";
}
}
$j = 0;
while ($j < $part->SizeOfArray('parts')) {
$childPath->SetString($paths->StringAt($p));
$childPath->Append('.parts[');
$childPath->AppendInt($j);
$childPath->Append(']');
try {
$paths->Append($childPath->GetAsString());
} catch (\Chilkat2\Exception $e) {
echo "Could not queue a child part.\n";
exit;
}
$j = $j + 1;
}
$p = $p + 1;
}