Get all messages with search condition(s)
Returns messages filtered by Gmail search expression. Use WITH(Search='...') with the same syntax as the Gmail search box (e.g. after:, from:, has:attachment, is:unread). For more operators see Gmail search help.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
SELECT *
FROM Messages
--Use WITH clause --OR-- Key column(s) in WHERE clause. If you use Id in WHERE clause then Search parameter is ignored
--WHERE [Id] = '18fb7747b0136726'
WITH(
Search='after:<<yearstart+1d|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1d|~|yyyy/MM/dd,FUN_TO_DATE>>' --search for mail sent between some dates (after year start plus one day and before yesterday (i.e. today-1d))
-- Search='after:2024/01/31 before:2024/05/25' --use of static date
--for more examples refer https://support.google.com/mail/answer/7190
-- Search='FREE PRODUCT' --search example for=> has words anywhere in the subject or body
-- Search='"FREE PRODUCT"' --search example for=> has exact phrase anywhere in the subject or body
-- Search='subject:FREE PRODUCT' --search example for=> has subject (contains any word)
-- Search='subject:(FREE PRODUCT)' --search example for => has subject (contains exact phrase)
-- Search='Rfc822msgid:<008601dae391$fda78bf0$f8f6a3d0$@zappysys.com>' --search example for => Search by Rfc822MessageID column (Alternate Message ID for searching - you can find from get_messages or get_message endpoint output
-- Search='to:david' --search example for => to (name)
-- Search='to:david.smith@gmail.com' --search example for => to (email)
-- Search='from:david' --search example for => from (name)
-- Search='from:david.smith@gmail.com' --search example for => from (email)
-- Search='from:(Platform Notifications) OR from:(no-reply@accounts.google.com) ' --search example for => from (name or email)
-- Search='cc:david' --search example for => cc (name)
-- Search='bcc:david' --search example for => bcc (name)
-- Search='is:unread AND subject:(Share request for)' --search example for => multiple conditions (AND)
-- Search='is:unread OR subject:(Share request for)' --search example for => multiple conditions (OR)
-- Search='from:someuser@example.com' --search example for => from sender=from:someuser@example.com
-- Search='from:a@company.com OR from:b@company.com' --search example for => from sender (OR)
-- Search='from:amy' --search example for => from sender by name=from:amy
-- Search='from:amy OR from:bob ' --search example for => from sender by name (OR)
-- Search='has:attachment' --search example for => has attachment=has:attachment
-- Search='larger:10M' --search example for => has attachment (larger than 10MB)
-- Search='smaller:10M' --search example for => has attachment (smaller than 10MB)
-- Search='smaller:10M' --search example for => has attachment (smaller than 10MB)
-- Search='1024' --search example for => size (exact bytes)
-- Search='unread=is:unread' --search example for => is unread=is:unread
-- Search='is:read' --search example for => is read=is:read
-- Search='after:2004/04/16' --search example for => date after=after:2004/04/16
-- Search='before:2004/04/16' --search example for => date before=before:2004/04/16
-- Search='older_than:2d' --search example for => age older than (2days)
-- Search='older_than:4y' --search example for => age older than (4years)
-- Search='newer_than:2d' --search example for => age newer than (2days)
-- Search='newer_than:4m' --search example for => age newer than (4months)
-- Search='from:someuser@example.com rfc822msgid:<008601dae391zzzzzzz> is:unread' --search example for => from specific sender with specific message id(RFC822) and unread emails only=from:someuser@example.com rfc822msgid:<somemsgid@example.com> is:unread
-- Search='in:sent after:2014/01/01 before:2014/02/01' --search example for => sent date range (static dates)
-- Search='in:sent after:<<yearstart-1day+22hour|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1day|~|yyyy/MM/dd,FUN_TO_DATE>>' --search example for => sent date range (dynamic dates). You can use keywords like now,today,yesterday,monthstart,monthend,yearstart,yearend,weekstart,weekend along with operator + / - [hour,minute,second,day,month,year]
)
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_GMAIL_IN_GATEWAY], 'SELECT *
FROM Messages
--Use WITH clause --OR-- Key column(s) in WHERE clause. If you use Id in WHERE clause then Search parameter is ignored
--WHERE [Id] = ''18fb7747b0136726''
WITH(
Search=''after:<<yearstart+1d|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1d|~|yyyy/MM/dd,FUN_TO_DATE>>'' --search for mail sent between some dates (after year start plus one day and before yesterday (i.e. today-1d))
-- Search=''after:2024/01/31 before:2024/05/25'' --use of static date
--for more examples refer https://support.google.com/mail/answer/7190
-- Search=''FREE PRODUCT'' --search example for=> has words anywhere in the subject or body
-- Search=''"FREE PRODUCT"'' --search example for=> has exact phrase anywhere in the subject or body
-- Search=''subject:FREE PRODUCT'' --search example for=> has subject (contains any word)
-- Search=''subject:(FREE PRODUCT)'' --search example for => has subject (contains exact phrase)
-- Search=''Rfc822msgid:<008601dae391$fda78bf0$f8f6a3d0$@zappysys.com>'' --search example for => Search by Rfc822MessageID column (Alternate Message ID for searching - you can find from get_messages or get_message endpoint output
-- Search=''to:david'' --search example for => to (name)
-- Search=''to:david.smith@gmail.com'' --search example for => to (email)
-- Search=''from:david'' --search example for => from (name)
-- Search=''from:david.smith@gmail.com'' --search example for => from (email)
-- Search=''from:(Platform Notifications) OR from:(no-reply@accounts.google.com) '' --search example for => from (name or email)
-- Search=''cc:david'' --search example for => cc (name)
-- Search=''bcc:david'' --search example for => bcc (name)
-- Search=''is:unread AND subject:(Share request for)'' --search example for => multiple conditions (AND)
-- Search=''is:unread OR subject:(Share request for)'' --search example for => multiple conditions (OR)
-- Search=''from:someuser@example.com'' --search example for => from sender=from:someuser@example.com
-- Search=''from:a@company.com OR from:b@company.com'' --search example for => from sender (OR)
-- Search=''from:amy'' --search example for => from sender by name=from:amy
-- Search=''from:amy OR from:bob '' --search example for => from sender by name (OR)
-- Search=''has:attachment'' --search example for => has attachment=has:attachment
-- Search=''larger:10M'' --search example for => has attachment (larger than 10MB)
-- Search=''smaller:10M'' --search example for => has attachment (smaller than 10MB)
-- Search=''smaller:10M'' --search example for => has attachment (smaller than 10MB)
-- Search=''1024'' --search example for => size (exact bytes)
-- Search=''unread=is:unread'' --search example for => is unread=is:unread
-- Search=''is:read'' --search example for => is read=is:read
-- Search=''after:2004/04/16'' --search example for => date after=after:2004/04/16
-- Search=''before:2004/04/16'' --search example for => date before=before:2004/04/16
-- Search=''older_than:2d'' --search example for => age older than (2days)
-- Search=''older_than:4y'' --search example for => age older than (4years)
-- Search=''newer_than:2d'' --search example for => age newer than (2days)
-- Search=''newer_than:4m'' --search example for => age newer than (4months)
-- Search=''from:someuser@example.com rfc822msgid:<008601dae391zzzzzzz> is:unread'' --search example for => from specific sender with specific message id(RFC822) and unread emails only=from:someuser@example.com rfc822msgid:<somemsgid@example.com> is:unread
-- Search=''in:sent after:2014/01/01 before:2014/02/01'' --search example for => sent date range (static dates)
-- Search=''in:sent after:<<yearstart-1day+22hour|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1day|~|yyyy/MM/dd,FUN_TO_DATE>>'' --search example for => sent date range (dynamic dates). You can use keywords like now,today,yesterday,monthstart,monthend,yearstart,yearend,weekstart,weekend along with operator + / - [hour,minute,second,day,month,year]
)')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_GMAIL_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM Messages
--Use WITH clause --OR-- Key column(s) in WHERE clause. If you use Id in WHERE clause then Search parameter is ignored
--WHERE [Id] = ''18fb7747b0136726''
WITH(
Search=''after:<<yearstart+1d|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1d|~|yyyy/MM/dd,FUN_TO_DATE>>'' --search for mail sent between some dates (after year start plus one day and before yesterday (i.e. today-1d))
-- Search=''after:2024/01/31 before:2024/05/25'' --use of static date
--for more examples refer https://support.google.com/mail/answer/7190
-- Search=''FREE PRODUCT'' --search example for=> has words anywhere in the subject or body
-- Search=''"FREE PRODUCT"'' --search example for=> has exact phrase anywhere in the subject or body
-- Search=''subject:FREE PRODUCT'' --search example for=> has subject (contains any word)
-- Search=''subject:(FREE PRODUCT)'' --search example for => has subject (contains exact phrase)
-- Search=''Rfc822msgid:<008601dae391$fda78bf0$f8f6a3d0$@zappysys.com>'' --search example for => Search by Rfc822MessageID column (Alternate Message ID for searching - you can find from get_messages or get_message endpoint output
-- Search=''to:david'' --search example for => to (name)
-- Search=''to:david.smith@gmail.com'' --search example for => to (email)
-- Search=''from:david'' --search example for => from (name)
-- Search=''from:david.smith@gmail.com'' --search example for => from (email)
-- Search=''from:(Platform Notifications) OR from:(no-reply@accounts.google.com) '' --search example for => from (name or email)
-- Search=''cc:david'' --search example for => cc (name)
-- Search=''bcc:david'' --search example for => bcc (name)
-- Search=''is:unread AND subject:(Share request for)'' --search example for => multiple conditions (AND)
-- Search=''is:unread OR subject:(Share request for)'' --search example for => multiple conditions (OR)
-- Search=''from:someuser@example.com'' --search example for => from sender=from:someuser@example.com
-- Search=''from:a@company.com OR from:b@company.com'' --search example for => from sender (OR)
-- Search=''from:amy'' --search example for => from sender by name=from:amy
-- Search=''from:amy OR from:bob '' --search example for => from sender by name (OR)
-- Search=''has:attachment'' --search example for => has attachment=has:attachment
-- Search=''larger:10M'' --search example for => has attachment (larger than 10MB)
-- Search=''smaller:10M'' --search example for => has attachment (smaller than 10MB)
-- Search=''smaller:10M'' --search example for => has attachment (smaller than 10MB)
-- Search=''1024'' --search example for => size (exact bytes)
-- Search=''unread=is:unread'' --search example for => is unread=is:unread
-- Search=''is:read'' --search example for => is read=is:read
-- Search=''after:2004/04/16'' --search example for => date after=after:2004/04/16
-- Search=''before:2004/04/16'' --search example for => date before=before:2004/04/16
-- Search=''older_than:2d'' --search example for => age older than (2days)
-- Search=''older_than:4y'' --search example for => age older than (4years)
-- Search=''newer_than:2d'' --search example for => age newer than (2days)
-- Search=''newer_than:4m'' --search example for => age newer than (4months)
-- Search=''from:someuser@example.com rfc822msgid:<008601dae391zzzzzzz> is:unread'' --search example for => from specific sender with specific message id(RFC822) and unread emails only=from:someuser@example.com rfc822msgid:<somemsgid@example.com> is:unread
-- Search=''in:sent after:2014/01/01 before:2014/02/01'' --search example for => sent date range (static dates)
-- Search=''in:sent after:<<yearstart-1day+22hour|~|yyyy/MM/dd,FUN_TO_DATE>> before:<<today-1day|~|yyyy/MM/dd,FUN_TO_DATE>>'' --search example for => sent date range (dynamic dates). You can use keywords like now,today,yesterday,monthstart,monthend,yearstart,yearend,weekstart,weekend along with operator + / - [hour,minute,second,day,month,year]
)'
EXEC (@MyQuery) AT [LS_TO_GMAIL_IN_GATEWAY]