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 AutoIt Downloads
Local $bSuccess = False
Local $iReplacements
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-mail-token.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load the protocol token from the private working directory." & @CRLF)
Exit
EndIf
If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
ConsoleWrite("Authorize or refresh the mailbox first." & @CRLF)
Exit
EndIf
Local $sMailbox = "user@example.com"
$oImap = ObjCreate("Chilkat.Imap")
$oImap.Ssl = True
$oImap.Port = 993
$oImap.RequireSslCertVerify = True
$oImap.AuthMethod = "XOAUTH2"
$bSuccess = $oImap.Connect("imap.gmail.com")
If ($bSuccess <> True) Then
ConsoleWrite("Could not establish the Gmail IMAP TLS connection." & @CRLF)
Exit
EndIf
$bSuccess = $oImap.Login($sMailbox,$oToken.StringOf("access_token"))
If ($bSuccess <> True) Then
$oImap.Disconnect()
ConsoleWrite("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy." & @CRLF)
Exit
EndIf
Local $sCapabilities = $oImap.Capability()
If (($oImap.LastMethodSuccess <> True) Or ($oImap.HasCapability("X-GM-EXT-1",$sCapabilities) <> True)) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Gmail IMAP extensions are unavailable." & @CRLF)
Exit
EndIf
$oFolders = ObjCreate("Chilkat.Mailboxes")
$bSuccess = $oImap.MbxList(False,"","*",$oFolders)
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not list Gmail folders." & @CRLF)
Exit
EndIf
Local $sAllMail = ""
Local $iFolderMatches = 0
Local $iF = 0
While ($iF < $oFolders.Count)
If (($oFolders.HasFlag($iF,"\All") = True) And ($oFolders.IsSelectable($iF) = True)) Then
$sAllMail = $oFolders.GetName($iF)
$iFolderMatches = $iFolderMatches + 1
EndIf
$iF = $iF + 1
Wend
If ($iFolderMatches <> 1) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Expected one selectable folder with the \All special-use attribute. Check label visibility." & @CRLF)
Exit
EndIf
$bSuccess = $oImap.ExamineMailbox($sAllMail)
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Could not open All Mail read-only." & @CRLF)
Exit
EndIf
; This search uses Gmail syntax inside a quoted IMAP search argument.
Local $sQuery = "has:attachment from:sender@example.net"
$oQuoted = ObjCreate("Chilkat.StringBuilder")
$oQuoted.Append($sQuery)
If (($oQuoted.Contains(@CR,True) = True) Or ($oQuoted.Contains(@LF,True) = True)) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("A search query must not contain CR or LF." & @CRLF)
Exit
EndIf
$iReplacements = $oQuoted.Replace("\","\\")
$iReplacements = $oQuoted.Replace("""","\""")
$oJoined1 = ObjCreate("Chilkat.StringBuilder")
$oJoined1.Append("X-GM-RAW """)
$oJoined1.Append($oQuoted.GetAsString())
$oJoined1.Append("""")
Local $sCriteria = $oJoined1.GetAsString()
$oFound = ObjCreate("Chilkat.MessageSet")
$bSuccess = $oImap.QueryMbx($sCriteria,True,$oFound)
If ($bSuccess <> True) Then
$oImap.Logout()
$oImap.Disconnect()
ConsoleWrite("Gmail search failed." & @CRLF)
Exit
EndIf
; Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
ConsoleWrite("Matching messages: " & $oFound.Count & @CRLF)
ConsoleWrite("UID set: " & $oFound.ToCompactString() & @CRLF)
$oImap.Logout()
$oImap.Disconnect()