Skip to main content

SEARCH emails

tip

The "SELECT folder" function must be ran at least once before this function.

This function searches the specified 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"
})

Response

The response for this function is an array with number IDs containing each email that matches the search criteria.

[ 3, 6, 12, 75 ]

Searchable fields

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

FieldTypeDescription
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.
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.
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.
uidstringReturns emails whose unique identifier corresponds to the specified unique identifier set. Warning: not tested