Overview
The SMTP Mail provider extends your application ports with email sending capabilities using nodemailer. It supports any SMTP server, making it ideal for using existing email infrastructure or legacy systems.Installation
Configuration
The Mail provider reads configuration from environment variables:| Variable | Required | Description | Example |
|---|---|---|---|
MAIL_HOST | Yes | SMTP server hostname | smtp.gmail.com |
MAIL_PORT | Yes | SMTP server port | 587 (TLS) or 465 (SSL) |
MAIL_USER | Yes | SMTP username | [email protected] |
MAIL_PASS | Yes | SMTP password | your-password |
MAIL_FROM | Yes | Default sender email address | [email protected] |
Port 465 automatically uses SSL, while other ports use TLS.
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 toMAIL_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 nodemailer transporter for advanced operations.Transporter (from nodemailer)
Usage Examples
Send Welcome Email
Send Password Reset Email
Send Custom Email
Advanced: Email with Attachments
Popular SMTP Providers
Gmail
For Gmail, you need to use an App Password, not your regular password.
SendGrid
AWS SES
Lifecycle
The SMTP Mail provider:- During
register:- Creates nodemailer transporter
- Verifies connection to SMTP server
- Adds the
mailerport
- During
onAppStop: Closes the transporter connection
Error Handling
The provider will throw errors in these cases:- Missing required environment variables
- Failed connection to SMTP server
- Invalid email addresses
Comparing with Resend Provider
While the SMTP provider works with any SMTP server, the Resend provider offers:- 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
- Need to use your existing SMTP server
- Have strict data residency requirements
- Want to integrate with legacy email systems
- Prefer more control over email infrastructure
Best Practices
Use app-specific passwords
Use app-specific passwords
For services like Gmail, use app-specific passwords instead of your main account password.
Test SMTP connection
Test SMTP connection
Verify your SMTP connection during application startup to catch configuration issues early.
Handle connection failures
Handle connection failures
Implement retry logic for transient SMTP connection failures.
Monitor email deliverability
Monitor email deliverability
Keep track of bounce rates and delivery failures to maintain sender reputation.
Use connection pooling
Use connection pooling
nodemailer automatically handles connection pooling for better performance.