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

Download the original Gmail message as EML

See more GMail REST API Examples

Supply a Gmail message ID and request format=raw. Decode the complete MIME message directly to bytes and save them in downloaded.eml. Parse those same bytes with Chilkat.Email to read the subject, sender, and attachment count without a text-encoding round trip.

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 PHP Downloads

PHP
<?php

use Chilkat2\BinData;
use Chilkat2\Email;
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;

$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=raw');
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('raw') != 1) || ($reply->StringOfEquals('raw', '', true))) {
    echo "Expected raw in the response.\n";
    exit;
}

$mime = new BinData();
try {
    $reply->BytesOf('raw', 'base64url', $mime);
} catch (\Chilkat2\Exception $e) {
    echo "Could not decode the raw MIME.\n";
    exit;
}

try {
    $mime->WriteFile('downloaded.eml');
} catch (\Chilkat2\Exception $e) {
    echo "Could not write downloaded.eml.\n";
    exit;
}

$email = new Email();
try {
    $email->SetFromMimeBd($mime);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo 'From: ', $email->From, "\n";
echo 'Subject: ', $email->Subject, "\n";
echo 'Attachments: ', $email->NumAttachments, "\n";