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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Token
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Reply
string ls_SearchQuery
oleobject loo_BaseUrl
oleobject loo_Url
string ls_PageToken
oleobject loo_Page
long li_Total
long li_More
long i
string ls_MessageId
long h
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
destroy loo_Token
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_Token.LoadFile("gmail-token.json")
if li_Success <> 1 then
Write-Debug "Could not read gmail-token.json from the private working directory."
destroy loo_Token
return
end if
if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
Write-Debug "An OAuth access token is required."
destroy loo_Token
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Token.StringOf("access_token")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
ls_SearchQuery = "from:sender@example.com has:attachment filename:pdf"
loo_BaseUrl = create oleobject
li_rc = loo_BaseUrl.ConnectToNewObject("Chilkat.StringBuilder")
loo_BaseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=100&q=")
loo_BaseUrl.Append(loo_Http.UrlEncode(ls_SearchQuery))
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
ls_PageToken = ""
loo_Page = create oleobject
li_rc = loo_Page.ConnectToNewObject("Chilkat.JsonObject")
li_Total = 0
li_More = 1
do while (li_More = 1)
loo_Url.SetString(loo_BaseUrl.GetAsString())
loo_Url.Append("&pageToken=")
loo_Url.Append(loo_Http.UrlEncode(ls_PageToken))
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "Gmail did not return an HTTP response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success = 1 then
Write-Debug loo_Reply.StringOf("error.message")
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a Gmail JSON response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page
return
end if
// Keep this page while individual message requests reuse reply.
li_Success = loo_Reply.MakeCopy(loo_Page)
if li_Success <> 1 then
Write-Debug loo_Reply.LastErrorText
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page
return
end if
i = 0
do while (i < loo_Page.SizeOfArray("messages"))
loo_Page.I = i
ls_MessageId = loo_Page.StringOf("messages[i].id")
loo_Url.SetString("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
loo_Url.Append(loo_Http.UrlEncode(ls_MessageId))
loo_Url.Append("?format=metadata&metadataHeaders=From&metadataHeaders=Subject")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
// A message can disappear between the search and the metadata request.
if loo_Resp.StatusCode <> 404 then
if loo_Resp.StatusCode = 0 then
Write-Debug "Gmail did not return an HTTP response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page
return
end if
if (li_Success <> 1) OR (loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300) then
Write-Debug "Gmail HTTP " + string(loo_Resp.StatusCode)
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success = 1 then
Write-Debug loo_Reply.StringOf("error.message")
end if
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page
return
end if
li_Success = loo_Reply.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a Gmail JSON response."
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page
return
end if
Write-Debug "Message: " + ls_MessageId
h = 0
do while (h < loo_Reply.SizeOfArray("payload.headers"))
loo_Reply.I = h
Write-Debug loo_Reply.StringOf("payload.headers[i].name") + ": " + loo_Reply.StringOf("payload.headers[i].value")
h = h + 1
loop
li_Total = li_Total + 1
end if
i = i + 1
loop
ls_PageToken = ""
if loo_Page.JsonTypeOf("nextPageToken") = 1 then
ls_PageToken = loo_Page.StringOf("nextPageToken")
end if
li_More = ls_PageToken <> ""
loop
Write-Debug "Matching messages read: " + string(li_Total)
destroy loo_Token
destroy loo_Http
destroy loo_Resp
destroy loo_Reply
destroy loo_BaseUrl
destroy loo_Url
destroy loo_Page