Version:
Only show these results:

Read email messages and threads with Python

This page explains how to use the Nylas Python SDK and Email API to read email messages and threads.

Messages versus Threads

Messages are the fundamental object of the Nylas platform and the core building block for most email applications. Messages contain several pieces of information, such as when the message was sent, the sender's address, to whom it was sent, and the message body. They can also contain files (attachments), calendar event invitations, and more.

Threads are first-class objects that represent collections of messages which are related to each other, and result from people replying to an email conversation. For Gmail and Microsoft Exchange accounts, Nylas threads messages together so that they are as similar as possible to their representation in those environments. For all other providers, including generic IMAP, Nylas threads messages using a custom JWZ-inspired algorithm.

For more information, see the Messages and Threads reference documentation.

Before you begin

Before you start, you must have done the following tasks:

Read email messages from an inbox

To read individual email messages from an end user's inbox, you must fetch one or more messages from their account. To do so, use the query_params property to filter the email messages, and the limit property to return only a specific number of results, as in the example below.

messages = nylas.messages.list(
'<NYLAS_GRANT_ID>',
query_params={
"limit": 5
}
)

print(messages)

You can also return the body content of the email message, its snippet, its recipients' email addresses, and any associated folders or labels. For more information, see the Messages reference documentation.

Read threads from an inbox

Along with individual email messages, you can read threads from an end user's email inbox. Each Thread object contains a list of messages which are part of the thread. Use the threads.list() function to filter and paginate your results. By default, Nylas returns the 100 most recent threads in the end user's inbox.

The following example lists the five most recent unread threads in the end user's inbox.

threads = nylas.threads.list(
'<NYLAS_GRANT_ID>',
query_params={
"limit": 5
}
)

print(threads)

You can also retrieve the senders' email addresses, the recipients' email addresses, the email addresses in the CC and BCC fields, and the unread and starred parameters. See the Threads reference documentation for more information.

Explore the Email API

If you've made it to this point, congratulations! You've learned how to read email messages and threads with the Nylas Python SDK and Email API! 🎉

There's plenty more that you can do with Nylas. Take a look at the following resources to learn more: