How to Read Email Inbox Data With Nylas Python SDK

Learn how you can use the Nylas Python SDK to read email inbox data and apply different filters with just a few lines of code.

Blag aka Alvaro Tejada Galindo | March 25, 2022

By reading email data using the Nylas Connectivity APIs we can apply different kinds of filters and read threads like they are email messages.

To make things even easier for Python developers, we offer the Nylas Python SDK. Today we’re going to review how to read email inbox data using Python and the Nylas SDK.

Want a PDF of this article?

Share it with a friend or save it for later reading.

Is your system ready?

If you don’t have the Nylas Python SDK installed or your environment isn’t configured, I would recommend you to read the post How to Send Emails with the Nylas Python SDK where everything is clearly explained.

Reading the Inbox using the Nylas Python SDK

Now we get to write some code! For my code editor, I’m going to use Geany, however you can use the IDE of your choice. I called this file ReadInbox.py

# Load your env variables
from dotenv import load_dotenv
load_dotenv()

# Import your dependencies
import os
from nylas import APIClient

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN"),
)

# Read your inbox and display the results
messages = nylas.messages
for message in messages:
	print("Subject: {} | From: {}".format(message.subject, message.from_))
	print("\n")

We can run this script from the terminal by using:

$ python3 ReadInbox.py

If everything works fine, we will see all our emails (There’s a limit of 100).

Read inbox

Now, we might notice a little problem with this. All emails in the box are separated into “folders” using “labels”, and as we’re not specifying anything, we’re getting Inbox but also Sent, Spam and Trash messages. We can easily fix this.

I called this file ReadOnlyInbox.py

# Load your env variables
from dotenv import load_dotenv
load_dotenv()

# Import your dependencies
import os
from nylas import APIClient

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN"),
)

# Read your inbox and nothing more than your inbox and display the results
messages = nylas.messages.where(in_="inbox")
for message in messages:
	print("Subject: {} | From: {}".format(message.subject, message.from_))
	print("\n")

We can run this script from the terminal by using:

$ python3 ReadOnlyInbox.py

Now, we’re only reading messages from the Inbox folder

Read just inbox messages

If we want to only read the first 3 emails, we can just add .limit(3). I called this file ReadOnlyThreeInbox.py

# Load your env variables
from dotenv import load_dotenv
load_dotenv()

# Import your dependencies
import os
from nylas import APIClient

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN"),
)

# Read the first 3 messages in inbox folder and display the results
messages = nylas.messages.where(in_="inbox",limit=3)
for message in messages:
	print("Subject: {} | From: {}".format(message.subject, message.from_))
	print("\n")

We can run this script from the terminal by using:

$ python3 ReadOnlyThreeInbox.py
Read just the first three messages

Reading threads using the Nylas Python SDK

A thread is a message shared between 2 or more people. It consists of the original message with its corresponding replies.

For this example, we’re going to limit the results to just one thread. I called this file EmailThreads.py

# Load your env variables
from dotenv import load_dotenv
load_dotenv()

# Import your dependencies
import os
from nylas import APIClient

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN"),
)

# Read the first 3 messages in inbox folder and display the results
threads = nylas.threads.all(limit=1) 
for thread in threads:
	participants = threads[0].participants
	for participant in participants:
		print("Subject: {} | Participant: {} | Email: {}".format(
			thread.subject, participant["name"], participant["email"] 
			)
		)

We can run this script from the terminal by using:

$ python3 EmailThreads.py
Read email threads

Of course, all messages are shared between my emails, so they all share the same name, but you can see that emails are different.

Now, let’s say we want to search the latest message sent by a particular email account.

I called this file SearchEmail.py

# Load your env variables
from dotenv import load_dotenv
load_dotenv()

# Import your dependencies
import os
from nylas import APIClient

# Initialize your Nylas API client
nylas = APIClient(
    os.environ.get("CLIENT_ID"),
    os.environ.get("CLIENT_SECRET"),
    os.environ.get("ACCESS_TOKEN"),
)

#Search for messages, grab the first one, looks for attachments
attachs = ""
messages = nylas.messages.search("from:[email protected]")
first_message = messages[0] 
if len(first_message.files) > 0:
	for message in first_message.files:
		attachs += message["filename"] 
else:
	attachs = "None"
print("Subject: {} | Attachments: {}".format(
	first_message.subject, attachs
	)
)

We can run this script from the terminal by using:

$ python3 SearchEmail.py
Search for an email

We can easily perform the same search on threads by changing messages with threads.

And that’s it. As we can see, by using the Nylas Python SDK, reading email inbox data becomes an easy task. If you want to learn more, visit our Documentation Page.

Blag aka Alvaro Tejada Galindo

Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.