Tcl Requires Chilkat v11.0.0+
Tcl
Send a Gmail message with a file attachment
See more GMail REST API Examples
Live send. Prepare a small file named attachment.pdf and replace the sample addresses. Add the file as a MIME attachment and upload the complete message as message/rfc822. The response is a Gmail Message. For larger uploads or unreliable connections, see Google’s resumable upload guidance.
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.
Chilkat Tcl Downloads
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."
set fileData [new_CkBinData]
set success [CkBinData_LoadFile $fileData "attachment.pdf"]
if {$success != 1} then {
puts "Could not read attachment.pdf."
delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $reply
delete_CkEmail $email
delete_CkBinData $fileData
exit
}
set success [CkEmail_AddAttachmentBd $email "attachment.pdf" $fileData "application/pdf"]
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 $fileData
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 $fileData
delete_CkBinData $mime
exit
}
set success [CkHttp_HttpBd $http "POST" "https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media" $mime "message/rfc822" $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 $fileData
delete_CkBinData $mime
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 $fileData
delete_CkBinData $mime
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 $fileData
delete_CkBinData $mime
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 $fileData
delete_CkBinData $mime
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 $fileData
delete_CkBinData $mime