Receive and process incoming emails programmatically with Lettr
Lettr allows you to receive and process incoming emails programmatically. By configuring an inbound domain and setting up webhooks, your application can capture replies to transactional emails, process support requests, parse incoming data, and trigger automated workflows whenever an email arrives.
Lettr’s inbound email processing follows a four-step flow. You configure DNS to route emails to Lettr’s mail servers, and Lettr parses each email into structured data and delivers it to your application via webhook.
1
Configure Inbound Domain
Add an inbound domain in your Lettr dashboard and configure MX records to route incoming emails to Lettr’s mail servers.
2
Set Up Webhook
Create a webhook endpoint in your application to receive parsed email data when emails arrive.
3
Receive and Process
When someone sends an email to your inbound domain, Lettr parses it and sends the structured data to your webhook.
4
Build Your Logic
Route emails, extract data, trigger workflows, and respond programmatically.
Sender → MX Records → Lettr Mail Servers → Parse & Extract → Webhook → Your Application
Lettr handles the low-level SMTP reception, MIME parsing, attachment extraction, and spam scoring. Your application receives clean, structured JSON with the email’s sender, recipients, subject, body (both plain text and HTML), headers, and attachment URLs.
Multiple MX records with equal priority provide load balancing — sending servers distribute email across all three servers. If one is unavailable, the others handle delivery. See the Setup Guide for DNS propagation details.
Always verify webhook credentials before processing incoming emails to ensure requests are genuinely from Lettr. Configure Basic Auth or OAuth 2.0 authentication when creating your webhook in the dashboard.
When an email is received, Lettr sends a relay.relay_delivery webhook event. The payload is delivered as a JSON array of event objects, each containing a relay message with the parsed email:
[ { "msys": { "relay_message": { "msg_from": "sender@example.com", "rcpt_to": "support@mail.example.com", "friendly_from": "John Sender <sender@example.com>", "customer_id": "1234", "webhook_id": "wh_abc123", "content": { "html": "<p>HTML content of the email...</p>", "text": "Plain text content of the email...", "subject": "Help with my order", "headers": [ { "Message-ID": "<abc123@example.com>" }, { "Date": "Mon, 15 Jan 2024 10:30:00 +0000" }, { "In-Reply-To": "<original@example.com>" } ], "to": ["support@mail.example.com"], "email_rfc822": "...raw MIME content..." } } } }]
Lettr automatically handles MIME parsing and delivers both the structured content fields and the raw RFC 822 message. For a complete breakdown of each field and how to work with them, see Email Parsing.
The reply+ pattern above is called variable addressing (or plus addressing). It’s a common way to encode context in the recipient address so you can route replies back to the right record. See Routing for more patterns including routing by subject, sender domain, and content.
Every email includes a spam score, and you can configure automatic filtering:
// Filter based on spam score from your own analysisif (spamScore >= 6) { await quarantineAsSpam(email); return;}
You can also configure spam filtering sensitivity for your inbound domain through the dashboard under Domains → Inbound. Regardless of dashboard settings, you can implement your own filtering logic in your webhook handler. See Spam Filtering for advanced detection patterns and allowlist/blocklist management.
Attachments are automatically extracted and made available via secure URLs:
for (const attachment of email.attachments) { const response = await fetch(attachment.url); const buffer = await response.arrayBuffer(); await saveToStorage(attachment.filename, Buffer.from(buffer));}
Attachment URLs expire after 24 hours. Download and store attachments promptly if you need them longer.
Each attachment includes filename, contentType, size (in bytes), and a download url. See Attachments for storage examples (S3, GCS) and security best practices including file type verification and malware scanning.
Track replies to your transactional emails by using variable reply-to addresses:
// When sending an emailawait lettr.emails.send({ from: 'support@example.com', to: ['customer@example.com'], subject: 'Your support request', html: content, reply_to: `reply+ticket_${ticketId}@mail.example.com`});// When receiving the replyconst match = recipient.match(/^reply\+ticket_([^@]+)@/);if (match) { const ticketId = match[1]; await addReplyToTicket(ticketId, emailData);}
For more advanced threading, use email headers like Message-ID, In-Reply-To, and References to build full conversation threads. See Reply Tracking for header-based threading, auto-reply detection, and a complete support ticket example.
Respond quickly — Return a 200 status from your webhook endpoint within a few seconds. If processing is complex, accept the webhook and process asynchronously via a queue.
Handle duplicates — Webhooks may be delivered more than once. Use the event id to deduplicate.
Store before processing — Save the raw webhook payload before doing any processing, so you can replay events if something fails.
Validate attachments — Verify file types and scan for malware before processing attachments from unknown senders.
See Best Practices for architecture patterns, reliability strategies, and monitoring recommendations.