AutoIt Requires Chilkat v11.0.0+
AutoIt
Insert an EML file into the Gmail inbox
See more GMail REST API Examples
Read an existing message.eml as bytes and insert it into the authenticated mailbox with the INBOX label in the same request. This stores a message without delivering it to recipients. Insert bypasses most scanning and classification. For mail that should undergo normal inbound processing, see messages.import.
Authorize the application with https://www.googleapis.com/auth/gmail.insert. 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")
$oMime = ObjCreate("Chilkat.BinData")
$bSuccess = $oMime.LoadFile("message.eml")
If ($bSuccess <> True) Then
ConsoleWrite("Could not read message.eml." & @CRLF)
Exit
EndIf
$oRequest = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oRequest.UpdateBd("raw","base64url",$oMime)
If ($bSuccess <> True) Then
ConsoleWrite($oRequest.LastErrorText & @CRLF)
Exit
EndIf
$oRequest.UpdateString("labelIds[0]","INBOX")
$bSuccess = $oHttp.HttpJson("POST","https://gmail.googleapis.com/gmail/v1/users/me/messages",$oRequest,"application/json",$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)