Search Gmail by sender, subject, date, or attachment
See more GMail REST API Examples
Choose a Gmail search query, follow every result page, and fetch From and Subject headers for each match. Edit the query to use operators such as from:, subject:, after:, before:, has:attachment, and filename:. Quoted phrases and special characters are URL-encoded once.
Gmail search dates use midnight Pacific Standard Time. Use Unix timestamps in seconds for exact date boundaries in another timezone. Spam and Trash are excluded unless includeSpamTrash=true is added. Search requires gmail.readonly or a broader mail scope. Query searches are unavailable with gmail.metadata.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. 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")
Local $searchQuery = "from:sender@example.com has:attachment filename:pdf"
$oBaseUrl = ObjCreate("Chilkat.StringBuilder")
$oBaseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100&q=")
$oBaseUrl.Append($oHttp.UrlEncode($searchQuery))
$oUrl = ObjCreate("Chilkat.StringBuilder")
Local $sPageToken = ""
$oPage = ObjCreate("Chilkat.JsonObject")
Local $iTotal = 0
Local $bMore = True
While ($bMore = True)
$oUrl.SetString($oBaseUrl.GetAsString())
$oUrl.Append("&pageToken=")
$oUrl.Append($oHttp.UrlEncode($sPageToken))
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$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
; Keep this page while individual message requests reuse reply.
$bSuccess = $oReply.MakeCopy($oPage)
If ($bSuccess <> True) Then
ConsoleWrite($oReply.LastErrorText & @CRLF)
Exit
EndIf
Local $i = 0
While ($i < $oPage.SizeOfArray("messages"))
$oPage.I = $i
Local $sMessageId = $oPage.StringOf("messages[i].id")
$oUrl.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
$oUrl.Append($oHttp.UrlEncode($sMessageId))
$oUrl.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
; A message can disappear between the search and the metadata request.
If ($oResp.StatusCode <> 404) Then
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
ConsoleWrite("Message: " & $sMessageId & @CRLF)
Local $iH = 0
While ($iH < $oReply.SizeOfArray("payload.headers"))
$oReply.I = $iH
ConsoleWrite($oReply.StringOf("payload.headers[i].name") & ": " & $oReply.StringOf("payload.headers[i].value") & @CRLF)
$iH = $iH + 1
Wend
$iTotal = $iTotal + 1
EndIf
$i = $i + 1
Wend
$sPageToken = ""
If ($oPage.JsonTypeOf("nextPageToken") = 1) Then
$sPageToken = $oPage.StringOf("nextPageToken")
EndIf
$bMore = $sPageToken <> ""
Wend
ConsoleWrite("Matching messages read: " & $iTotal & @CRLF)