Skip to content

Parse Email Attachments Using AI

In this tutorial, you’ll combine incoming email processing with AI to extract structured data from email attachments.

You’ll receive an email, store its attachments, send them to an AI model, and use the extracted data inside a workflow.

This pattern is commonly used for document intake systems such as order processing, invoice handling, or technical drawing analysis.


By the end of this tutorial, you will:

  • Receive an email with attachments
  • Store the email and its files
  • Send attachments to an AI model
  • Extract structured data from the files
  • Use the extracted data in a workflow

  • A configured mail server connection (emailConfig)
  • An incoming email trigger
  • An AI connection configured
  • A workflow process to handle the data

Use an incoming email trigger to capture emails.

Inside the trigger, map attachments and store the email:

const attachments = mail.attachments.map(a => ({
fileName: a.fileName,
data: a.data
}));
const emailRecord = await IncomingEmail.create({
from: mail.from,
subject: mail.subject,
body: mail.body,
attachments
});

Start a workflow process using the stored email:

await this.process.start("processIncomingEmail", {
emailId: emailRecord.id
});

In your workflow, load the stored email:

const email = await IncomingEmail.getById(input.emailId);

This gives you access to the attachments.


Create an AI instance using your configuration:

const ai = new SimpleAI($config.aiConnection);

Step 5: Convert attachments for AI processing

Section titled “Step 5: Convert attachments for AI processing”

If your attachment is a PDF, convert it into images:

const file = email.attachments[0];
const images = await this.pdf.toImages(file.data);

Note

This example processes the first attachment only.
In real applications, you may need to loop through all attachments.

Define the structure you want the AI to return:

const schema = {
type: "object",
properties: {
materials: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
confidence: { type: "number" }
}
}
}
}
};

Send the attachment content to the AI model:

const result = await ai.queryStructured(
"What materials are used in this document?",
schema,
{ images }
);

Save the extracted data:

email.parsedData = result;
await email.save();

You can now:

  • Route the workflow based on extracted data
  • Send responses
  • Trigger further automation

You now have a fully automated pipeline:

  • Email arrives with attachments
  • Attachments are processed by AI
  • Structured data is extracted
  • A workflow uses the result

This pattern combines three core capabilities:

  1. Email triggers receive external input
  2. AI extracts structured data from files
  3. Workflows orchestrate the next steps

This enables end-to-end automation from unstructured inputs.


  • Send automated replies with extracted data
  • Use tool calling to enrich results
  • Route to different workflows based on AI output
  • Store extracted data in your database

  • AI Structured Extraction
  • Email Triggers
  • Sending Emails
  • Workflow Fundamentals