Tcl Requires Chilkat v11.0.0+
Tcl
SendGrid: Attach a PDF invoice to an email
See more SendGrid Examples
Read invoice.pdf from the working directory and send it as a downloadable file named invoice-1042.pdf. SendGrid expects Base64 file content in the JSON request, not a path or a multipart upload. The whole message, including attachments, must be under 30 MB; allow for Base64 to add about a third to the file size.
Use an API key with Mail Send permission and a verified sender identity. Replace the example addresses before sending.
SendGrid documentation: Mail Send size limits.
Chilkat Tcl Downloads
load ./chilkat.dll
# Assumes Chilkat is unlocked. See Global Unlock Sample.
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
# Supply a Mail Send API key from your application configuration. AuthToken adds the Bearer prefix.
CkHttp_put_AuthToken $http "SENDGRID_API_KEY"
set message [new_CkJsonObject]
# Replace the sender with a verified sender identity and the recipient with your destination.
CkJsonObject_UpdateString $message "from.email" "sender@example.com"
CkJsonObject_UpdateString $message "personalizations[0].to[0].email" "recipient@example.com"
CkJsonObject_UpdateString $message "subject" "Invoice 1042"
CkJsonObject_UpdateString $message "content[0].type" "text/plain"
CkJsonObject_UpdateString $message "content[0].value" "Your invoice is attached as invoice-1042.pdf."
set fileData [new_CkBinData]
set loaded [CkBinData_LoadFile $fileData "invoice.pdf"]
if {$loaded != 1} then {
puts "Could not read invoice.pdf. Check the path and file permissions."
delete_CkHttp $http
delete_CkJsonObject $message
delete_CkBinData $fileData
exit
}
if {[CkBinData_get_NumBytes $fileData] == 0} then {
puts "invoice.pdf is empty."
delete_CkHttp $http
delete_CkJsonObject $message
delete_CkBinData $fileData
exit
}
# Encode the file bytes once. The content field contains only Base64, without a data URI prefix.
CkJsonObject_UpdateString $message "attachments[0].content" [CkBinData_getEncoded $fileData "base64"]
CkJsonObject_UpdateString $message "attachments[0].type" "application/pdf"
CkJsonObject_UpdateString $message "attachments[0].filename" "invoice-1042.pdf"
CkJsonObject_UpdateString $message "attachments[0].disposition" "attachment"
set response [new_CkHttpResponse]
CkHttp_HttpJson $http "POST" "https://api.sendgrid.com/v3/mail/send" $message "application/json" $response
# HttpJson also returns false for HTTP errors. A zero status means no HTTP response arrived.
if {[CkHttpResponse_get_StatusCode $response] == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkJsonObject $message
delete_CkBinData $fileData
delete_CkHttpResponse $response
exit
}
if {[CkHttpResponse_get_StatusCode $response] != 202} then {
puts "Mail Send returned HTTP [CkHttpResponse_get_StatusCode $response]"
# For validation errors the response body identifies the offending field and explains why.
puts [CkHttpResponse_bodyStr $response]
delete_CkHttp $http
delete_CkJsonObject $message
delete_CkBinData $fileData
delete_CkHttpResponse $response
exit
}
puts "SendGrid accepted the email for delivery (HTTP 202)."
delete_CkHttp $http
delete_CkJsonObject $message
delete_CkBinData $fileData
delete_CkHttpResponse $response