AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not read gmail-token.json from the private working directory." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("An OAuth access token is required." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$oEmail = ObjCreate("Chilkat.Email")
; Use the authorized mailbox or one of its configured send-as addresses.
$oEmail.From = "Sender <sender@example.com>"
$bSuccess = $oEmail.AddTo("Recipient","recipient@example.net")
If ($bSuccess <> True) Then
ConsoleWrite($oEmail.LastErrorText & @CRLF)
Exit
EndIf
$oEmail.Subject = "Sample message"
$oEmail.Charset = "utf-8"
$oEmail.Body = "Hello from the Gmail API."
$oFileData = ObjCreate("Chilkat.BinData")
$bSuccess = $oFileData.LoadFile("attachment.pdf")
If ($bSuccess <> True) Then
ConsoleWrite("Could not read attachment.pdf." & @CRLF)
Exit
EndIf
$bSuccess = $oEmail.AddAttachmentBd("attachment.pdf",$oFileData,"application/pdf")
If ($bSuccess <> True) Then
ConsoleWrite($oEmail.LastErrorText & @CRLF)
Exit
EndIf
$oMime = ObjCreate("Chilkat.BinData")
$bSuccess = $oEmail.GetMimeBd($oMime)
If ($bSuccess <> True) Then
ConsoleWrite($oEmail.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oHttp.HttpBd("POST","https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media",$oMime,"message/rfc822",$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
Exit
EndIf
If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess = True) Then
ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
EndIf
Exit
EndIf
$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
Exit
EndIf
If (($oReply.JsonTypeOf("id") <> 1) Or ($oReply.StringOfEquals("id","",True) = True)) Then
ConsoleWrite("Expected id in the response." & @CRLF)
Exit
EndIf
ConsoleWrite("Gmail message ID: " & $oReply.StringOf("id") & @CRLF)