Gmail IMAP: Search with Gmail Search Syntax
See more GMail SMTP/IMAP/POP Examples
Search with Gmail operators through X-GM-RAW, using QueryMbx to obtain UIDs. The example discovers All Mail and opens it read-only. It quotes the query for the IMAP command rather than URL-encoding it.
IMAP search is limited to the selected mailbox. All Mail normally covers inbox and archived messages, but excludes Spam and Trash; a Gmail search operator cannot expand an IMAP search beyond its selected mailbox. An empty result is successful. Returned IMAP UIDs are mailbox-specific and are not Gmail message IDs. Persist them only with their mailbox and UIDVALIDITY.
Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.
Chilkat PureBasic Downloads
IncludeFile "CkMessageSet.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkMailboxes.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
replacements.i
; Assumes Chilkat is unlocked. See Global Unlock Sample.
token.i = CkJsonObject::ckCreate()
If token.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJsonObject::ckLoadFile(token,"gmail-mail-token.json")
If success <> 1
Debug "Could not load the protocol token from the private working directory."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
If (CkJsonObject::ckJsonTypeOf(token,"access_token") <> 1) OR (CkJsonObject::ckStringOfEquals(token,"access_token","",1) = 1)
Debug "Authorize or refresh the mailbox first."
CkJsonObject::ckDispose(token)
ProcedureReturn
EndIf
mailbox.s = "user@example.com"
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
CkImap::setCkRequireSslCertVerify(imap, 1)
CkImap::setCkAuthMethod(imap, "XOAUTH2")
success = CkImap::ckConnect(imap,"imap.gmail.com")
If success <> 1
Debug "Could not establish the Gmail IMAP TLS connection."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,mailbox,CkJsonObject::ckStringOf(token,"access_token"))
If success <> 1
CkImap::ckDisconnect(imap)
Debug "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
capabilities.s = CkImap::ckCapability(imap)
If (CkImap::ckLastMethodSuccess(imap) <> 1) OR (CkImap::ckHasCapability(imap,"X-GM-EXT-1",capabilities) <> 1)
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Gmail IMAP extensions are unavailable."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
folders.i = CkMailboxes::ckCreate()
If folders.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkImap::ckMbxList(imap,0,"","*",folders)
If success <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Could not list Gmail folders."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
ProcedureReturn
EndIf
allMail.s = ""
folderMatches.i = 0
f.i = 0
While (f < CkMailboxes::ckCount(folders))
If (CkMailboxes::ckHasFlag(folders,f,"\All") = 1) AND (CkMailboxes::ckIsSelectable(folders,f) = 1)
allMail = CkMailboxes::ckGetName(folders,f)
folderMatches = folderMatches + 1
EndIf
f = f + 1
Wend
If folderMatches <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Expected one selectable folder with the \All special-use attribute. Check label visibility."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
ProcedureReturn
EndIf
success = CkImap::ckExamineMailbox(imap,allMail)
If success <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Could not open All Mail read-only."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
ProcedureReturn
EndIf
; This search uses Gmail syntax inside a quoted IMAP search argument.
query.s = "has:attachment from:sender@example.net"
quoted.i = CkStringBuilder::ckCreate()
If quoted.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(quoted,query)
If (CkStringBuilder::ckContains(quoted,Chr(13),1) = 1) OR (CkStringBuilder::ckContains(quoted,Chr(10),1) = 1)
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "A search query must not contain CR or LF."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(quoted)
ProcedureReturn
EndIf
replacements = CkStringBuilder::ckReplace(quoted,"\","\\")
replacements = CkStringBuilder::ckReplace(quoted,Chr(34),"\" + Chr(34))
joined1.i = CkStringBuilder::ckCreate()
If joined1.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(joined1,"X-GM-RAW " + Chr(34))
CkStringBuilder::ckAppend(joined1,CkStringBuilder::ckGetAsString(quoted))
CkStringBuilder::ckAppend(joined1,Chr(34))
criteria.s = CkStringBuilder::ckGetAsString(joined1)
found.i = CkMessageSet::ckCreate()
If found.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkImap::ckQueryMbx(imap,criteria,1,found)
If success <> 1
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
Debug "Gmail search failed."
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(quoted)
CkStringBuilder::ckDispose(joined1)
CkMessageSet::ckDispose(found)
ProcedureReturn
EndIf
; Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
Debug "Matching messages: " + Str(CkMessageSet::ckCount(found))
Debug "UID set: " + CkMessageSet::ckToCompactString(found)
CkImap::ckLogout(imap)
CkImap::ckDisconnect(imap)
CkJsonObject::ckDispose(token)
CkImap::ckDispose(imap)
CkMailboxes::ckDispose(folders)
CkStringBuilder::ckDispose(quoted)
CkStringBuilder::ckDispose(joined1)
CkMessageSet::ckDispose(found)
ProcedureReturn
EndProcedure