Overview
Nodemailer is a flexible SMTP client that allows you to send emails through any SMTP server. Use Nodemailer when you:- Have an existing SMTP server
- Use a provider not natively supported (Gmail, SendGrid SMTP, Amazon SES, etc.)
- Need full control over email transport
- Want to use your company’s email infrastructure
- Need to send emails through custom mail servers
Nodemailer is the third provider ShipFree checks during auto-discovery. It will be used if Resend and Postmark aren’t configured.
Prerequisites
- Access to an SMTP server
- SMTP credentials (host, port, username, password)
Setup Instructions
Step 1: Gather SMTP Credentials
You’ll need the following from your email provider:- SMTP Host - Server address (e.g.,
smtp.gmail.com) - SMTP Port - Usually
587(STARTTLS) or465(TLS/SSL) - Username - Your email address or SMTP username
- Password - Your email password or app-specific password
- Secure -
truefor port 465,falsefor port 587
Step 2: Configure Environment Variables
Add these to your.env file:
Step 3: Install Nodemailer
Nodemailer is already included in ShipFree:Step 4: Test Your Configuration
Common SMTP Providers
Gmail
Gmail
Setup:
- Enable 2-factor authentication on your Google account
- Go to Google Account → Security → App passwords
- Create a new app password for “Mail”
- Use the generated password (not your account password)
Amazon SES
Amazon SES
Setup:
- Sign up for Amazon SES
- Verify your domain or email address
- Create SMTP credentials in the SES console
- Note your SMTP endpoint (region-specific)
SES starts in sandbox mode. Request production access to send to any email address.
SendGrid SMTP
SendGrid SMTP
Setup:
- Create a SendGrid account
- Go to Settings → API Keys
- Create an API key with “Mail Send” permissions
- Use “apikey” as username and the API key as password
Microsoft 365 / Outlook
Microsoft 365 / Outlook
Setup:
- Ensure SMTP authentication is enabled for your account
- Use your Microsoft 365 email and password
- May require app password if 2FA is enabled
Mailgun SMTP
Mailgun SMTP
Setup:
- Create a Mailgun account
- Verify your domain
- Get SMTP credentials from Mailgun dashboard
Custom/Self-Hosted SMTP
Custom/Self-Hosted SMTP
Configuration:
Features
Email Attachments
Nodemailer has excellent attachment support:Custom Headers
Full control over email headers:Reply-To Address
Plain Text Alternative
Batch Email Sending
Nodemailer sends batch emails sequentially:While functional, batch sending via SMTP is slower than providers with native batch APIs (like Resend or Postmark).
Environment Variables Reference
TLS vs. STARTTLS
Understanding SMTP security:- Port 587 (STARTTLS)
- Port 465 (TLS/SSL)
- Port 25 (Unencrypted)
Most common and recommended
- Connection starts unencrypted
- Upgrades to TLS via STARTTLS command
- Widely supported
- Recommended by most providers
Development vs. Production
Development Setup
For development, consider using:-
Log Provider (easiest):
-
Ethereal Email (test SMTP service):
Emails are caught, not sent - view them at ethereal.email
-
Gmail (for quick testing):
Production Setup
For production, use a reliable SMTP provider:Troubleshooting
Error: 'Nodemailer not configured'
Error: 'Nodemailer not configured'
Missing or incomplete SMTP configuration.Solution:
- Verify all required env vars are set:
SMTP_HOST,SMTP_PORT,SMTP_USER,SMTP_PASS - Check for typos in variable names
- Restart your application
Error: 'Invalid login' or '535 Authentication failed'
Error: 'Invalid login' or '535 Authentication failed'
Incorrect SMTP credentials.Solution:
- Verify
SMTP_USERandSMTP_PASSare correct - For Gmail/Google: Use app-specific password, not account password
- Check if 2FA requires app password
- Verify account hasn’t been locked
Error: 'Connection timeout' or 'ECONNREFUSED'
Error: 'Connection timeout' or 'ECONNREFUSED'
Cannot connect to SMTP server.Solution:
- Verify
SMTP_HOSTis correct - Check
SMTP_PORT(587 or 465) - Ensure firewall allows SMTP connections
- Try alternative port if blocked
- Check if your ISP blocks SMTP
Error: 'Self-signed certificate'
Error: 'Self-signed certificate'
SSL/TLS certificate issue.Solution:
For development only, you can bypass (not recommended for production):For production, fix the certificate issue on your SMTP server.
Emails going to spam
Emails going to spam
Common deliverability issues:
- Set up SPF record: Add SMTP server to domain’s SPF
- Configure DKIM: Set up DKIM signing
- Add DMARC policy: Configure DMARC for your domain
- Use authenticated domain: Send from verified domain
- Warm up IP: Gradually increase sending volume
- Avoid spam triggers: Check email content for spam words
Slow email sending
Slow email sending
SMTP is inherently slower than API-based providers.Improvements:
- Use connection pooling (Nodemailer does this automatically)
- Switch to API-based provider (Resend, Postmark) for batches
- Use background jobs for large sends
- Reduce batch size
Code Reference
The Nodemailer provider implementation:createNodemailerProvider()- Initialize provider (line 130)getTransporter()- Create SMTP transport (line 37)send()- Send single email (line 67)sendBatch()- Send batch emails (line 96)
When to Use Nodemailer
Nodemailer is ideal when you:Have existing SMTP infrastructure - Company email server, self-hosted solution
Need maximum flexibility - Full control over transport and configuration
Use unsupported provider - Provider doesn’t have a native ShipFree integration
Require offline capability - Local SMTP server for air-gapped systems
Additional Resources
Nodemailer Documentation
Official Nodemailer docs and guides
SMTP Guide
SMTP configuration reference
Ethereal Email
Test SMTP service for development
Email Testing
Testing strategies with Nodemailer
Next Steps
1
Set up SPF/DKIM
Configure DNS records for better email deliverability
2
Test with Ethereal
Use Ethereal Email to test without sending real emails
3
Monitor sending
Implement logging to track email delivery and failures