Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.1.0+

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.

Google documentation

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
long li_Replacements
oleobject loo_Token
string ls_Mailbox
oleobject loo_Imap
string ls_Capabilities
oleobject loo_Folders
string ls_AllMail
long li_FolderMatches
long f
string ls_Query
oleobject loo_Quoted
oleobject loo_Joined1
string ls_Criteria
oleobject loo_Found

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-mail-token.json")
if li_Success <> 1 then
    Write-Debug "Could not load the protocol token 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 "Authorize or refresh the mailbox first."
    destroy loo_Token
    return
end if

ls_Mailbox = "user@example.com"
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")

loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
loo_Imap.AuthMethod = "XOAUTH2"
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
    Write-Debug "Could not establish the Gmail IMAP TLS connection."
    destroy loo_Token
    destroy loo_Imap
    return
end if

li_Success = loo_Imap.Login(ls_Mailbox,loo_Token.StringOf("access_token"))
if li_Success <> 1 then
    loo_Imap.Disconnect()
    Write-Debug "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
    destroy loo_Token
    destroy loo_Imap
    return
end if

ls_Capabilities = loo_Imap.Capability()
if (loo_Imap.LastMethodSuccess <> 1) OR (loo_Imap.HasCapability("X-GM-EXT-1",ls_Capabilities) <> 1) then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Gmail IMAP extensions are unavailable."
    destroy loo_Token
    destroy loo_Imap
    return
end if

loo_Folders = create oleobject
li_rc = loo_Folders.ConnectToNewObject("Chilkat.Mailboxes")

li_Success = loo_Imap.MbxList(0,"","*",loo_Folders)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not list Gmail folders."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

ls_AllMail = ""
li_FolderMatches = 0
f = 0
do while (f < loo_Folders.Count)
    if (loo_Folders.HasFlag(f,"\All") = 1) AND (loo_Folders.IsSelectable(f) = 1) then
        ls_AllMail = loo_Folders.GetName(f)
        li_FolderMatches = li_FolderMatches + 1
    end if

    f = f + 1
loop
if li_FolderMatches <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Expected one selectable folder with the \All special-use attribute. Check label visibility."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

li_Success = loo_Imap.ExamineMailbox(ls_AllMail)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Could not open All Mail read-only."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    return
end if

//  This search uses Gmail syntax inside a quoted IMAP search argument.
ls_Query = "has:attachment from:sender@example.net"
loo_Quoted = create oleobject
li_rc = loo_Quoted.ConnectToNewObject("Chilkat.StringBuilder")

loo_Quoted.Append(ls_Query)
if (loo_Quoted.Contains("~r",1) = 1) OR (loo_Quoted.Contains("~n",1) = 1) then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "A search query must not contain CR or LF."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    destroy loo_Quoted
    return
end if

li_Replacements = loo_Quoted.Replace("\","\\")
li_Replacements = loo_Quoted.Replace("~"","\~"")
loo_Joined1 = create oleobject
li_rc = loo_Joined1.ConnectToNewObject("Chilkat.StringBuilder")

loo_Joined1.Append("X-GM-RAW ~"")
loo_Joined1.Append(loo_Quoted.GetAsString())
loo_Joined1.Append("~"")
ls_Criteria = loo_Joined1.GetAsString()
loo_Found = create oleobject
li_rc = loo_Found.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx(ls_Criteria,1,loo_Found)
if li_Success <> 1 then
    loo_Imap.Logout()
    loo_Imap.Disconnect()
    Write-Debug "Gmail search failed."
    destroy loo_Token
    destroy loo_Imap
    destroy loo_Folders
    destroy loo_Quoted
    destroy loo_Joined1
    destroy loo_Found
    return
end if

//  Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
Write-Debug "Matching messages: " + string(loo_Found.Count)
Write-Debug "UID set: " + loo_Found.ToCompactString()
loo_Imap.Logout()
loo_Imap.Disconnect()


destroy loo_Token
destroy loo_Imap
destroy loo_Folders
destroy loo_Quoted
destroy loo_Joined1
destroy loo_Found