Introduction: Why Parse Email Threads?

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.

String Manipulation Basics in TypeScript

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.

Parsing a Single Email

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:

  1. Split the email at the first blank line to separate the header from the body.
  2. Join the rest of the body in case there are multiple blank lines.

Code:

  • header contains everything before the first blank line.
  • body contains everything after, joined back together.
Parsing Multiple Emails in a Thread

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:

  1. Split the thread into individual emails using the separator line (\n---\n\n).
  2. For each email, split into header and body as before.

Code:

  • This returns an array of objects, each with a header and body for each email in the thread.
Extracting Fields from the Header

To make your agent even smarter, you can extract specific fields (like from, to, and subject) from the header.

How to Parse:

  1. Split the thread into individual emails.
  2. For each email, split into header and body.
  3. Split the header into lines.
  4. Extract the from, to, and subject fields from the header lines.

Code:

  • This returns an array of objects, each with header, from, to, subject, and body fields.
Using Parsed Data in Your Agent

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:

Summary

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal