Overview
The Resend Mail provider extends your application ports with email sending capabilities using Resend. Resend is a modern email API that simplifies sending transactional emails with better deliverability and built-in analytics.Installation
Configuration
The Resend provider reads configuration from environment variables:| Variable | Required | Description | Example |
|---|---|---|---|
RESEND_API_KEY | Yes | Resend API key | re_abc123... |
RESEND_FROM | Yes | Default sender email address (must be from verified domain) | "My App <[email protected]>" |
Getting an API Key
- Sign up at resend.com
- Verify your domain
- Create an API key at resend.com/api-keys
Example .env
Setup
Basic Setup
Type Your Ports
To get proper type inference for the mailer port, extend your ports type:API Reference
The provider extends your ports with amailer property:
sendText(to, subject, text)
Send a plain text email using the default sender address.to: Recipient email addresssubject: Email subjecttext: Plain text content
Promise<void>
sendHtml(to, subject, html)
Send an HTML email using the default sender address.to: Recipient email addresssubject: Email subjecthtml: HTML content
Promise<void>
send(options)
Send an email with full control over options, including the ability to override the sender.options.from: Sender email address (optional, defaults toRESEND_FROM)options.to: Recipient email addressoptions.subject: Email subjectoptions.text: Plain text content (if not usinghtml)options.html: HTML content (if not usingtext)
Promise<void>
client
Access the underlying Resend client for advanced operations.Resend
Usage Examples
Send Welcome Email
Send Password Reset Email
Send Custom Email
Advanced: Bulk Emails with Tags and Attachments
Lifecycle
The Resend Mail provider:- During
register:- Creates Resend client with API key
- Adds the
mailerport
- During
onAppStop: No cleanup needed (Resend client is stateless)
Error Handling
The provider will throw errors in these cases:- Missing required environment variables
- Invalid API key
- Invalid email addresses
- Domain not verified in Resend
Comparing with SMTP Provider
Resend offers several advantages over traditional SMTP:- Simpler setup: Just an API key, no SMTP server configuration
- Better deliverability: Resend handles infrastructure and reputation
- Modern features: Built-in analytics, webhooks, and templates
- No port/firewall issues: Uses HTTPS instead of SMTP ports
@contract-kit/provider-mail-smtp) if you need to:
- Use your existing SMTP server
- Have strict data residency requirements
- Integrate with legacy email systems
Best Practices
Verify your domain
Verify your domain
Make sure your domain is verified in Resend before sending production emails.
Use templates for consistency
Use templates for consistency
Consider using Resend’s template features for consistent email branding.
Handle errors gracefully
Handle errors gracefully
Always handle email sending errors and have a fallback strategy.
Monitor deliverability
Monitor deliverability
Use Resend’s analytics dashboard to monitor email deliverability and engagement.
Use tags for organization
Use tags for organization