Skip to content

Email Triggers

Email triggers allow you to execute your Microfn functions by sending emails to a unique, auto-generated email address for your workspace. This is perfect for email-based automation, processing notifications, and converting emails into actionable tasks.

  1. Enable the email trigger for your workspace to get a unique email address.
  2. Send an email to this address.
  3. Microfn receives the email and triggers your function.
  4. The parsed email content is passed as the email property in your function’s input.

You can enable the email trigger for your workspace in the dashboard:

  1. Navigate to your workspace.
  2. Go to Settings → Triggers.
  3. Click Enable Email Trigger.
  4. Copy the generated email address (e.g., ws-abc123xyz@mail.microfn.dev).

Your function will now be triggered by any email sent to this address.

When an email is received, your function is executed with an input object that contains an email property. This property holds the parsed email data.

interface EmailData {
from: string; // Sender's email address
to: string; // Recipient (your trigger address)
subject?: string; // Email subject line
body?: string; // Plain text body content
headers: Record<string, string>; // All email headers
timestamp: string; // ISO timestamp when received
}
interface Input {
email: EmailData;
}

Here are a few simple examples of how to process incoming emails.

This basic function logs the sender and subject of every email received.

export default async function main(input) {
const { from, subject } = input.email;
console.log(`Received email from: ${from}`);
console.log(`Subject: ${subject}`);
return {
processed: true,
from,
subject,
};
}

This example logs the plain text body of the email.

export default async function main(input) {
const { body } = input.email;
console.log("Email body:");
console.log(body);
return {
processed: true,
bodyLength: body?.length || 0,
};
}

This function logs all the headers from the received email.

export default async function main(input) {
const { headers } = input.email;
console.log("Email headers:");
console.log(JSON.stringify(headers, null, 2));
return {
processed: true,
headerCount: Object.keys(headers).length,
};
}