Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL lnReplacements
LOCAL loToken
LOCAL lcMailbox
LOCAL loImap
LOCAL lcCapabilities
LOCAL loFolders
LOCAL lcAllMail
LOCAL lnFolderMatches
LOCAL f
LOCAL lcQuery
LOCAL loQuoted
LOCAL loJoined1
LOCAL lcCriteria
LOCAL loFound

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-mail-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load the protocol token from the private working directory."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
    ? "Authorize or refresh the mailbox first."
    RELEASE loToken
    CANCEL
ENDIF

lcMailbox = "user@example.com"
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.AuthMethod = "XOAUTH2"
lnSuccess = loImap.Connect("imap.gmail.com")
IF (lnSuccess <> 1) THEN
    ? "Could not establish the Gmail IMAP TLS connection."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login(lcMailbox,loToken.StringOf("access_token"))
IF (lnSuccess <> 1) THEN
    loImap.Disconnect()
    ? "IMAP authentication failed. Check the mailbox, scope, expiry, and account policy."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

lcCapabilities = loImap.Capability()
IF ((loImap.LastMethodSuccess <> 1) OR (loImap.HasCapability("X-GM-EXT-1",lcCapabilities) <> 1)) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Gmail IMAP extensions are unavailable."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

loFolders = CreateObject('Chilkat.Mailboxes')
lnSuccess = loImap.MbxList(0,"","*",loFolders)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Could not list Gmail folders."
    RELEASE loToken
    RELEASE loImap
    RELEASE loFolders
    CANCEL
ENDIF

lcAllMail = ""
lnFolderMatches = 0
f = 0
DO WHILE (f < loFolders.Count)
    IF ((loFolders.HasFlag(f,"\All") = 1) AND (loFolders.IsSelectable(f) = 1)) THEN
        lcAllMail = loFolders.GetName(f)
        lnFolderMatches = lnFolderMatches + 1
    ENDIF

    f = f + 1
ENDDO
IF (lnFolderMatches <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Expected one selectable folder with the \All special-use attribute. Check label visibility."
    RELEASE loToken
    RELEASE loImap
    RELEASE loFolders
    CANCEL
ENDIF

lnSuccess = loImap.ExamineMailbox(lcAllMail)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Could not open All Mail read-only."
    RELEASE loToken
    RELEASE loImap
    RELEASE loFolders
    CANCEL
ENDIF

*  This search uses Gmail syntax inside a quoted IMAP search argument.
lcQuery = "has:attachment from:sender@example.net"
loQuoted = CreateObject('Chilkat.StringBuilder')
loQuoted.Append(lcQuery)
IF ((loQuoted.Contains(CHR(13),1) = 1) OR (loQuoted.Contains(CHR(10),1) = 1)) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "A search query must not contain CR or LF."
    RELEASE loToken
    RELEASE loImap
    RELEASE loFolders
    RELEASE loQuoted
    CANCEL
ENDIF

lnReplacements = loQuoted.Replace("\","\\")
lnReplacements = loQuoted.Replace('"','\"')
loJoined1 = CreateObject('Chilkat.StringBuilder')
loJoined1.Append('X-GM-RAW "')
loJoined1.Append(loQuoted.GetAsString())
loJoined1.Append('"')
lcCriteria = loJoined1.GetAsString()
loFound = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx(lcCriteria,1,loFound)
IF (lnSuccess <> 1) THEN
    loImap.Logout()
    loImap.Disconnect()
    ? "Gmail search failed."
    RELEASE loToken
    RELEASE loImap
    RELEASE loFolders
    RELEASE loQuoted
    RELEASE loJoined1
    RELEASE loFound
    CANCEL
ENDIF

*  Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
? "Matching messages: " + STR(loFound.Count)
? "UID set: " + loFound.ToCompactString()
loImap.Logout()
loImap.Disconnect()

RELEASE loToken
RELEASE loImap
RELEASE loFolders
RELEASE loQuoted
RELEASE loJoined1
RELEASE loFound