Building an Automatic AI Email Replier

Introduction: Building an Automatic AI Email Replier

In this lesson, you’ll learn how to build an automatic AI replier that reads an incoming email and writes a reply on behalf of an employee—in this case, Emily from Acme Corp. You’ve already learned how to parse emails and use different agents. Now, we’ll focus on how to generate and send a reply using the addReply method.

Quick Recap: Parsing Emails and Using Agents

Before we dive in, let’s quickly review what you’ve already learned:

  • Parsing Emails: You know how to read an email thread from a file and break it into structured objects using parseEmailThread.
  • Agents: You’ve built and used two agents:
    • The IntentAgent classifies the intent of an email (e.g., question, meeting request).
    • The SummaryAgent summarizes the email, decides if a reply is needed, and drafts a reply.

Now, let’s see how to use the addReply method to automatically add a reply to an email thread.

How the addReply Method Works

Example Usage

Here’s a short example showing how you might orchestrate your agents and use addReply to automatically generate and append a reply to an email thread:

async function main() {
  const emailContent = readFileSync("example-email-thread.txt", "utf-8");
  const thread = parseEmailThread(emailContent);

  const summaryAgent = mastra.getAgent("SummaryAgent");
  const intentAgent = mastra.getAgent("IntentAgent");

  const email = thread[0];
  const emailText = email.header + "\n\n" + email.body;

  const intent = (await intentAgent.generate([{ role: "user", content: emailText }])).text;
  const { draftReply, replyNeeded } = await summaryAgent.processEmail(emailText, intent);

  if (replyNeeded) {
    const updatedThread = addReply(emailContent, draftReply, "emily@acme-corp.com");
    console.log("Updated Thread:\n", updatedThread);
  }
}

In this example:

  • The email thread is loaded from a file and parsed.
  • The first email’s content is sent to the IntentAgent to determine the intent.
  • The SummaryAgent uses the email and its intent to decide if a reply is needed and, if so, drafts a reply.
  • If a reply is needed, addReply is called to append the draft reply to the thread as if it came from Emily. The updated thread is then printed.

Example Output

If the original thread is:

From: sarah.jones@acme-corp.com
To: engineering-team@acme-corp.com
Subject: Project Phoenix – Action Items & Confirmation Needed

Hi Team,

Thanks for your work on Project Phoenix so far...

After calling addReply, the thread will look like:

From: sarah.jones@acme-corp.com
To: engineering-team@acme-corp.com
Subject: Project Phoenix – Action Items & Confirmation Needed

Hi Team,

Thanks for your work on Project Phoenix so far...

---
From: emily@acme-corp.com
To: sarah.jones@acme-corp.com
Subject: Re: Project Phoenix – Action Items & Confirmation Needed

Hi Sarah, I’ve received my task and will start drafting test cases from the updated specs. Let me know if there’s anything else you need from me.

Summary

You now know how the addReply method works to append a new reply to an email thread, making it look like a real response from an employee. This is a key step in building an automatic AI replier that can participate in email conversations on behalf of your team.

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