Skip to main content

SEARCH emails

tip

A folder must be selected (see "SELECT folder") before running this function.

This function searches the selected folder for emails that match the specified criteria.

Usage

The function accepts an object with optional searchable fields. At least one needs to be provided for it to not error out. A full list of searchable fields is available below.

let searchedEmails = await imap.searchEmails({
new: true,
deleted: false,
header: {
key: "Content-Type",
value: "text/plain"
},
body: "meeting"
})

// Search by UID instead of sequence number
let uids = await imap.searchEmails({ all: true, useUid: true })

Response

The response for this function is an array with number IDs containing each email that matches the search criteria. These are sequence numbers by default, or UIDs with useUid: true.

[ 3, 6, 12, 75 ]

The returned IDs can be passed straight into fetchEmails() (with useUid: true when searching by UID).

info

Both the IMAP4rev1 * SEARCH and the IMAP4rev2 * ESEARCH (RFC 9051 §6.4.4) response formats are parsed, including ESEARCH sequence sets with ranges (e.g. 1:3,5). Non-ASCII search values automatically get a CHARSET UTF-8 argument.

Searchable fields

All fields are optional, however at least one needs to be provided.

FieldTypeDescription
useUidbooleanRuns UID SEARCH and returns UIDs instead of sequence numbers.
allbooleanReturns all the emails in a folder. When true, ignores all of the other filters.
bccstringReturns emails that have the specified string in the BCC header.
ccstringReturns emails that have the specified string in the CC header.
answeredbooleanReturns emails that have the \Answered flag if true and if false then those which don't.
deletedbooleanReturns emails that have the \Deleted flag if true, and if false then those which don't.
draftbooleanReturns emails that have the \Draft flag if true, and if false then those which don't.
flaggedbooleanReturns emails that have the \Flagged flag if true, and if false then those which don't.
seenbooleanReturns emails that have the \Seen flag if true, and if false then those which don't.
fromstringReturns emails that have the specified string in the FROM header.
tostringReturns emails that have the specified string in the TO header.
subjectstringReturns emails whose subject contains the specified string.
bodystringReturns emails whose body contains the specified string.
textstringReturns emails whose headers and body contains the specified string.
keywordstringReturns emails that have the specified flag set.
unkeywordstringReturns emails that don't have the specified flag set.
newbooleanReturns emails that have the \Recent flag set, but not the \Seen flag.
recentbooleanReturns emails that have the \Recent flag set.
oldbooleanReturns emails that do not have the \Recent flag set.
header{ key: string, value: string }Returns emails where the specified header exists and has the specified value.
largerThannumberReturns emails whose size is larger than the specified number of octets.
smallerThannumberReturns emails whose size is smaller than the specified number of octets.
onDateReturns emails whose internal date (disregarding time and timezone) is within the specified date.
sentOnDateReturns emails whose DATE header (disregarding time and timezone) is within the specified date.
sinceDateReturns emails whose internal date (disregarding time and timezone) is within or later than the specified date.
sentSinceDateReturns emails whose DATE header (disregarding time and timezone) is within or later than the specified date.
beforeDateReturns emails whose internal date (disregarding time and timezone) is before the specified date.
sentBeforeDateReturns emails whose DATE header (disregarding time and timezone) is before the specified date.
notstringReturns emails whose text does not contain the specified string (NOT TEXT ...).
or[string, string][]Returns emails matching any of the given [searchKey, value] pairs (combined with OR).
uidstringReturns emails whose unique identifier corresponds to the specified UID set, e.g. "1:10" or "42".