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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local token = chilkat.JsonObject.new()
local success = token:load_file("gmail-mail-token.json")
if not success then
print("Could not load the protocol token from the private working directory.")
return
end
if (token:json_type_of("access_token") ~= 1) or (token:string_of_equals("access_token", "", true)) then
print("Authorize or refresh the mailbox first.")
return
end
local mailbox = "user@example.com"
local imap = chilkat.Imap.new()
imap.ssl = true
imap.port = 993
imap.require_ssl_cert_verify = true
imap.auth_method = "XOAUTH2"
success = imap:connect("imap.gmail.com")
if not success then
print("Could not establish the Gmail IMAP TLS connection.")
return
end
success = imap:login(mailbox, token:string_of("access_token"))
if not success then
imap:disconnect()
print("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
return
end
local capabilities = imap:capability()
if (not imap.last_method_success) or (not imap:has_capability("X-GM-EXT-1", capabilities)) then
imap:logout()
imap:disconnect()
print("Gmail IMAP extensions are unavailable.")
return
end
local folders = chilkat.Mailboxes.new()
success = imap:mbx_list(false, "", "*", folders)
if not success then
imap:logout()
imap:disconnect()
print("Could not list Gmail folders.")
return
end
local allMail = ""
local folderMatches = 0
local f = 0
while f < folders.count do
if (folders:has_flag(f, "\\All")) and (folders:is_selectable(f)) then
allMail = folders:get_name(f)
folderMatches = folderMatches + 1
end
f = f + 1
end
if folderMatches ~= 1 then
imap:logout()
imap:disconnect()
print("Expected one selectable folder with the \\All special-use attribute. Check label visibility.")
return
end
success = imap:examine_mailbox(allMail)
if not success then
imap:logout()
imap:disconnect()
print("Could not open All Mail read-only.")
return
end
-- This search uses Gmail syntax inside a quoted IMAP search argument.
local query = "has:attachment from:sender@example.net"
local quoted = chilkat.StringBuilder.new()
quoted:append(query)
if (quoted:contains("\r", true)) or (quoted:contains("\n", true)) then
imap:logout()
imap:disconnect()
print("A search query must not contain CR or LF.")
return
end
local replacements = quoted:replace("\\", "\\\\")
replacements = quoted:replace("\"", "\\\"")
local joined1 = chilkat.StringBuilder.new()
joined1:append("X-GM-RAW \"")
joined1:append(quoted:get_as_string())
joined1:append("\"")
local criteria = joined1:get_as_string()
local found = chilkat.MessageSet.new()
success = imap:query_mbx(criteria, true, found)
if not success then
imap:logout()
imap:disconnect()
print("Gmail search failed.")
return
end
-- Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
print("Matching messages: " .. found.count)
print("UID set: " .. found:to_compact_string())
imap:logout()
imap:disconnect()