Using email attributes and filters¶
As shown earlier in the fetching some email messages section, we can use different attributes in a filter when retrieving some emails.
The search method¶
A way to search for emails using filters is through the search
method, in this case a complete string referring to the criterion to be used must be passed. This way makes it the user's responsibility to use the attributes and syntax correctly.
Advanced Searching¶
With the possibility to build a search manually, we can create an advanced search using a single attribute or combining different attributes.
Using a single attribute¶
Basically the structure of the criteria string is composed by the attribute name in capital letters followed by the attribute value in quotes. You can also use the string between ()
, to make it more organized.
In general, for each attribute, the structure used will be something like this: '(ATTRIBUTE_NAME "value_for_this_attribute")'.
# search using email subject only
subject_search = '(SUBJECT "Test Message")'
# Searches for all emails that have "Test Message" as subject
messages_search_1 = email.search(subject_search)
# search considering only the address that sent the email
from_search = '(FROM "test@email.com")'
# Searches for all emails sent from this address
messages_search_2 = email.search(from_search)
Using multiple attributes¶
In addition to using attributes individually, it is also possible to combine multiple attributes into the same criteria string.
Basically, this would be equivalent to an AND operation. Only messages that match all criteria will be returned.
It is also possible to do OR operations to search messages by some attribute.
In this case, just insert OR
at the beginning of the string and keep the criteria the same way.
Table: search method attributes¶
See the table for the most common attributes used in the search
method to create search strings:
Attribute | Description | Type of value |
---|---|---|
SEEN/UNSEEN |
with/without the Seen flag | str |
FROM |
contain specified str in envelope struct’s FROM field | str |
TO |
contain specified str in envelope struct’s TO field | str |
CC |
contain specified str in envelope struct’s CC field | str |
BCC |
contain specified str in envelope struct’s BCC field | str |
SUBJECT |
contain specified str in envelope struct’s SUBJECT field | str |
BODY |
contain specified str in body of the message | str |
ON |
internal date is within specified date | str |
SINCE |
internal date is within or later than the specified date | str |
BEFORE |
internal date is earlier than the specified date | str |
Tip
See more details about the attributes that can be used in this link. Remember that all attributes are of type str
in the search
method.
The filter by method¶
In some cases, the way above can get a little confusing or generate doubts when building the filter string. Therefore, it is possible to use the filter_by
method as a "shortcut" to make the filters.
from botcity.plugins.email import MailFilters
from datetime import datetime
# You can view and select an attribute using: MailFilters.<ATTRIBUTE_NAME>
# Searches for all emails sending by the address: joao@email.com
messages = email.filter_by(MailFilters.FROM, "joao@email.com")
# Searches for all emails that have not yet been read
messages = email.filter_by(MailFilters.SEEN, False)
# Searches for all emails that were received on the date: 15/02/2022
messages = email.filter_by(MailFilters.ON_DATE, datetime(2022, 2, 15).date())
In this case, it is not necessary to worry about the syntax of the filter string, just pass the desired attribute in the filter and the value that this attribute should have.
Table: filter_by method attributes¶
The attributes that can be used are defined in the MailFilters
class as a Enum, the following table shows the attributes that are defined and what types of values they can receive:
Attribute | Description | Type of value |
---|---|---|
SEEN |
with/without the Seen flag | bool |
FROM |
address from email sender | str |
TO |
address receiver as TO | str |
CC |
address receiver as CC | str |
BCC |
address receiver as BCC | str |
SUBJECT |
the email subject | str |
TEXT_CONTENT |
contains the text in the email body | str |
ON_DATE |
email date is within specified date | date |
DATE_GREATER_THAN |
email date is within or later than the specified date | date |
DATE_LESS_THAN |
email date is earlier than the specified date | date |