How to Send an Email with the Python SDK

How to read email inbox data with Nylas Python SDK

5 min read

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.

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

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.

Join our technical demo to see the Nylas Platform in action

Related resources

How to Send Emails Using an API

Key Takeaways This post will provide a complete walkthrough for integrating an email API focused…

How to build a CRM in 3 sprints with Nylas

What is a CRM? CRM stands for Customer Relationship Management, and it’s basically a way…

How to create an appointment scheduler in your React app

Learn how to create an appointment scheduler in your React app using Nylas Scheduler. Streamline bookings and UX with step-by-step guidance.