Parse Email Attachments Using AI
Parse Email Attachments Using AI
Section titled “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.
What you’ll build
Section titled “What you’ll build”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
Prerequisites
Section titled “Prerequisites”- A configured mail server connection (
emailConfig) - An incoming email trigger
- An AI connection configured
- A workflow process to handle the data
Step 1: Receive and store the email
Section titled “Step 1: Receive and store the email”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});Step 2: Trigger a workflow
Section titled “Step 2: Trigger a workflow”Start a workflow process using the stored email:
await this.process.start("processIncomingEmail", { emailId: emailRecord.id});Step 3: Load the email in the workflow
Section titled “Step 3: Load the email in the workflow”In your workflow, load the stored email:
const email = await IncomingEmail.getById(input.emailId);This gives you access to the attachments.
Step 4: Initialize the AI helper
Section titled “Step 4: Initialize the AI helper”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.
Step 6: Define the output schema
Section titled “Step 6: Define the output schema”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" } } } } }};Step 7: Call the AI model
Section titled “Step 7: Call the AI model”Send the attachment content to the AI model:
const result = await ai.queryStructured( "What materials are used in this document?", schema, { images });Step 8: Store and use the result
Section titled “Step 8: Store and use the result”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
Result
Section titled “Result”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
What’s happening
Section titled “What’s happening”This pattern combines three core capabilities:
- Email triggers receive external input
- AI extracts structured data from files
- Workflows orchestrate the next steps
This enables end-to-end automation from unstructured inputs.
Next steps
Section titled “Next steps”- 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
Related guides
Section titled “Related guides”- AI Structured Extraction
- Email Triggers
- Sending Emails
- Workflow Fundamentals