Welcome back! In the previous lesson, you learned how to set up your Mastra agent and run a simple task. Now, let's make your agent smarter by teaching it how to handle real-world data — specifically, email threads.
Emails are a common way people communicate, but they are not always easy for a computer to understand. They often come as long blocks of text, with headers, signatures, and sometimes even multiple replies in a single message. If you want your agent to help with emails, you first need to break down this unstructured text into parts that your agent can use.
In this lesson, you’ll learn how to take a raw email thread and turn it into something your agent can work with. We’ll do this step by step.
Before we start parsing emails, let’s quickly review two important string methods in TypeScript:
.split(separator): Breaks a string into an array, using the separator..join(separator): Combines an array of strings into a single string, using the separator.
Example:
We’ll use these methods to break up and reassemble email text.
Let’s start with a single email. Here’s what a raw email might look like:
Notice the blank line (\n\n) between the header and the body.
How to Parse:
- Split the email at the first blank line to separate the header from the body.
- Join the rest of the body in case there are multiple blank lines.
Code:
headercontains everything before the first blank line.bodycontains everything after, joined back together.
Sometimes, an email thread contains several emails, separated by a special line (for example, \n---\n\n). Here’s how you can handle that:
Example of a Raw Email Thread:
In this example, there are two emails in the thread, separated by a line with ---.
How to Parse:
- Split the thread into individual emails using the separator line (
\n---\n\n). - For each email, split into header and body as before.
Code:
- This returns an array of objects, each with a
headerandbodyfor each email in the thread.
To make your agent even smarter, you can extract specific fields (like from, to, and subject) from the header.
How to Parse:
- Split the thread into individual emails.
- For each email, split into header and body.
- Split the header into lines.
- Extract the
from,to, andsubjectfields from the header lines.
Code:
- This returns an array of objects, each with
header,from,to,subject, andbodyfields.
Once you have the parsed emails, you can use the fields as input for your Mastra agent. For example, you might pass only the body for summarization, or use the sender’s name for a reply.
Example usage:
In this lesson, you learned how to take a raw email thread and split it into a header and a body using simple string operations in TypeScript. You built a utility function, parseEmailThread, that makes this process easy and reusable. This is an important step in preparing real-world data for your Mastra agent.
Next, you’ll get a chance to practice parsing email threads and using the parsed data in your own code. This hands-on practice will help you get comfortable with the process and prepare you for building even smarter email assistants. Good luck!
