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

Send email to blind-copy recipients with Gmail

See more GMail REST API Examples

Live send. Replace all sample addresses. Gmail takes its recipients from the submitted MIME To, Cc, and Bcc headers. Use AddBcc on a new Email and keep the Bcc header when serializing it for Gmail. Treat the submission as private because it contains the blind-copy addresses.

Authorize the application with https://www.googleapis.com/auth/gmail.send. 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 email [new_CkEmail]

#  Use the authorized mailbox or one of its configured send-as addresses.
CkEmail_put_From $email "Sender <sender@example.com>"
set success [CkEmail_AddTo $email "Recipient" "recipient@example.net"]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkEmail $email
    exit
}

CkEmail_put_Subject $email "Sample message"
CkEmail_put_Charset $email "utf-8"
CkEmail_put_Body $email "Hello from the Gmail API."
#  Gmail needs the blind recipient in the submitted MIME; use the normal Chilkat 11.x Bcc behavior.
set success [CkEmail_AddBcc $email "Blind recipient" "blind@example.org"]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkEmail $email
    exit
}

set mime [new_CkBinData]

set success [CkEmail_GetMimeBd $email $mime]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkEmail $email
    delete_CkBinData $mime
    exit
}

set request [new_CkJsonObject]

set success [CkJsonObject_UpdateBd $request "raw" "base64url" $mime]
if {$success != 1} then {
    puts [CkJsonObject_lastErrorText $request]
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkEmail $email
    delete_CkBinData $mime
    delete_CkJsonObject $request
    exit
}

set success [CkHttp_HttpJson $http "POST" "https://gmail.googleapis.com/gmail/v1/users/me/messages/send" $request "application/json" $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_CkEmail $email
    delete_CkBinData $mime
    delete_CkJsonObject $request
    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_CkEmail $email
    delete_CkBinData $mime
    delete_CkJsonObject $request
    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_CkEmail $email
    delete_CkBinData $mime
    delete_CkJsonObject $request
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $reply "id"] != 1]  ||  [[CkJsonObject_StringOfEquals $reply "id" "" 1] == 1]} then {
    puts "Expected id in the response."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $reply
    delete_CkEmail $email
    delete_CkBinData $mime
    delete_CkJsonObject $request
    exit
}

puts "Gmail message ID: [CkJsonObject_stringOf $reply id]"

delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkEmail $email
delete_CkBinData $mime
delete_CkJsonObject $request