Email Triggers
Email Triggers
Section titled “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.
How Email Triggers Work
Section titled “How Email Triggers Work”- Enable the email trigger for your workspace to get a unique email address.
- Send an email to this address.
- Microfn receives the email and triggers your function.
- The parsed email content is passed as the
email
property in your function’s input.
Setting Up Email Triggers
Section titled “Setting Up Email Triggers”You can enable the email trigger for your workspace in the dashboard:
- Navigate to your workspace.
- Go to Settings → Triggers.
- Click Enable Email Trigger.
- 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.
Accessing Email Data
Section titled “Accessing Email Data”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;}
Examples
Section titled “Examples”Here are a few simple examples of how to process incoming emails.
1. Log Email Subject and Sender
Section titled “1. Log Email Subject and Sender”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, };}
2. Log the Email Body
Section titled “2. Log the Email Body”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, };}
3. Log All Email Headers
Section titled “3. Log All Email Headers”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, };}