Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token [new_CkJsonObject]

set success [CkJsonObject_LoadFile $token "gmail-token.json"]
if {$success != 1} then {
    puts "Could not read gmail-token.json from the private working directory."
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "An OAuth access token is required."
    delete_CkJsonObject $token
    exit
}

set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_AuthToken $http [CkJsonObject_stringOf $token "access_token"]
set resp [new_CkHttpResponse]

set reply [new_CkJsonObject]

set url [new_CkStringBuilder]

CkStringBuilder_Append $url "https://gmail.googleapis.com/gmail/v1/users/me/messages/"
CkStringBuilder_Append $url "GMAIL_MESSAGE_ID"
CkStringBuilder_Append $url "?format=full"
set success [CkHttp_HttpNoBody $http "GET" [CkStringBuilder_getAsString $url] $resp]
if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
    puts "Gmail did not return an HTTP response."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkStringBuilder $url
    exit
}

if {expr [$success != 1]  ||  [expr [[CkHttpResponse_get_StatusCode $resp] < 200]  ||  [[CkHttpResponse_get_StatusCode $resp] >= 300]]} then {
    puts "Gmail HTTP [CkHttpResponse_get_StatusCode $resp]"
    set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
    if {$success == 1} then {
        puts [CkJsonObject_stringOf $reply error.message]
    }

    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkStringBuilder $url
    exit
}

set success [CkJsonObject_Load $reply [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Expected a Gmail JSON response."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkStringBuilder $url
    exit
}

if {[CkJsonObject_JsonTypeOf $reply "payload"] != 3} then {
    puts "Expected a MIME payload."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkStringBuilder $url
    exit
}

#  Queue paths into the original JSON document so any MIME nesting depth is supported.
set paths [new_CkStringTable]

set success [CkStringTable_Append $paths "payload"]
if {$success != 1} then {
    puts "Could not queue the root part."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkStringBuilder $url
    delete_CkStringTable $paths
    exit
}

set part [new_CkJsonObject]

set childPath [new_CkStringBuilder]

set p 0
while {$p < [CkStringTable_get_Count $paths]} {
    set success [CkJsonObject_ObjectOf2 $reply [CkStringTable_stringAt $paths $p] $part]
    if {$success != 1} then {
        puts "Could not read a MIME part."
        delete_CkJsonObject $token
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $reply
        delete_CkStringBuilder $url
        delete_CkStringTable $paths
        delete_CkJsonObject $part
        delete_CkStringBuilder $childPath
        exit
    }

    puts "Part: [CkJsonObject_stringOf $part mimeType]  bytes: [CkJsonObject_IntOf $part body.size]"
    if {[CkJsonObject_JsonTypeOf $part "filename"] == 1} then {
        if {[CkJsonObject_StringOfEquals $part "filename" "" 1] != 1} then {
            puts "Filename: [CkJsonObject_stringOf $part filename]"
        }

    }

    if {[CkJsonObject_JsonTypeOf $part "body.attachmentId"] == 1} then {
        puts "Attachment ID: [CkJsonObject_stringOf $part body.attachmentId]"
    }     else {
        if {[CkJsonObject_JsonTypeOf $part "body.data"] == 1} then {
            puts "Body data is available in this part."
        }

    }

    set j 0
    while {$j < [CkJsonObject_SizeOfArray $part "parts"]} {
        CkStringBuilder_SetString $childPath [CkStringTable_stringAt $paths $p]
        CkStringBuilder_Append $childPath ".parts["
        CkStringBuilder_AppendInt $childPath $j
        CkStringBuilder_Append $childPath "]"
        set success [CkStringTable_Append $paths [CkStringBuilder_getAsString $childPath]]
        if {$success != 1} then {
            puts "Could not queue a child part."
            delete_CkJsonObject $token
            delete_CkHttp $http
            delete_CkHttpResponse $resp
            delete_CkJsonObject $reply
            delete_CkStringBuilder $url
            delete_CkStringTable $paths
            delete_CkJsonObject $part
            delete_CkStringBuilder $childPath
            exit
        }

        set j [expr $j + 1]
    }
    set p [expr $p + 1]
}

delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkStringBuilder $url
delete_CkStringTable $paths
delete_CkJsonObject $part
delete_CkStringBuilder $childPath