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 calls your function’s exported
email()handler. - The parsed email payload is delivered to
email(data)so you can orchestrate any follow-up logic.
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.
Handling Email Data
Section titled “Handling Email Data”Email events call your exported email(data) handler. Emails are no longer provided on main({ input }), so use this dedicated function to work with the raw message. The payload contains the parsed email details and matches the following shape:
interface EmailEvent { email: { body?: string; // Plain text body content from: string; // Sender's email address rawSize: number; // Size of the original message in bytes subject?: string; // Email subject line timestamp: string; // ISO timestamp when received to: string; // Recipient (your trigger address) };}Sample Payload
Section titled “Sample Payload”{ "email": { "body": "hai al", "from": "mail202509@d.pn", "rawSize": 6386, "subject": "herroooo", "timestamp": "2025-09-20T07:46:14.515Z", "to": "fn-cdmixwn7kw7p@mail.microfn.dev" }}Note: Email headers are not included in the payload that reaches
email(data).
Example Handler
Section titled “Example Handler”Export both main (for manual or HTTP-triggered invocations) and email to react to incoming messages. The example below logs every email and returns a greeting from the default main handler.
// Welcome to Microfn!// This is a serverless function that runs in the cloud.// Export a main function that takes { input: data } and returns any value.// Strings/numbers are returned as-is, objects/arrays are JSON serialized.
export async function main({ input }) { return { message: "Hello from Microfn!", timestamp: new Date().toISOString(), receivedInput: input };}
export async function email(data) { // handle email trigger console.log("email received"); console.log(data);}