Nodemailer Deployment and Usage Guide
Prerequisites
- Node.js: Version 6.0.0 or higher (required for spread operator support)
- Package Manager: npm (comes with Node.js)
- Email Service: Access to an SMTP server (e.g., Gmail, Outlook, custom SMTP)
- Network: Ability to make outbound connections on SMTP ports (25, 465, 587)
Installation
Using npm (Recommended)
npm install nodemailer
Manual Installation
- Clone the repository:
git clone https://github.com/nodemailer/nodemailer.git
cd nodemailer
- Install dependencies:
npm install
Configuration
Basic SMTP Configuration
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
secure: false, // true for port 465, false for port 587
auth: {
user: 'username@example.com',
pass: 'password'
}
});
Environment Variables
For production, use environment variables:
export SMTP_HOST=smtp.example.com
export SMTP_PORT=587
export SMTP_USER=username@example.com
export SMTP_PASS=password
TLS Configuration
let transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
secure: false,
tls: {
rejectUnauthorized: true,
minVersion: 'TLSv1.2'
},
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
}
});
Build & Run
Development
# Run tests
npm test
# Run linting
npm run lint
# Start development server
npm run dev
Sending Emails
const nodemailer = require('nodemailer');
// Create transporter
let transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
secure: false,
auth: {
user: 'username@example.com',
pass: 'password'
}
});
// Send email
transporter.sendMail({
from: 'sender@example.com',
to: 'receiver@example.com',
subject: 'Hello',
text: 'Hello world',
html: '<b>Hello world</b>'
}).then(info => {
console.log('Message sent: %s', info.messageId);
}).catch(err => {
console.error('Error sending email:', err);
});
Production
# Build for production
npm run build
# Start production server
npm start
Deployment
Platform Options
1. Heroku
# Deploy to Heroku
git push heroku main
2. AWS Lambda
// Lambda handler example
exports.handler = async (event) => {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
}
});
// Send email logic
};
3. Docker
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Environment Variables for Production
# SMTP Configuration
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password
# Security
NODE_ENV=production
Troubleshooting
Common Issues and Solutions
1. ETIMEDOUT Errors
# Check firewall settings
# Allow outbound connections on SMTP ports
2. Gmail Issues
// Use App Password instead of regular password
// Enable "Less secure app access" in Gmail settings
3. TLS Errors
// Update Node.js to latest version
// Set correct TLS version
let transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
secure: false,
tls: {
rejectUnauthorized: true,
minVersion: 'TLSv1.2'
}
});
4. DNS Resolution Issues
// Use IP address instead of hostname
let transporter = nodemailer.createTransport({
host: '1.2.3.4', // IP address
port: 465,
secure: true,
tls: {
servername: 'example.com'
}
});
5. SyntaxError for "..."
# Upgrade Node.js to version 6.0.0 or higher
6. Rate Limiting
// Configure rate limits
let transporter = nodemailer.createTransport({
rateDelta: 1000, // 1 second
rateLimit: 5 // 5 emails per second
});
Debug Mode
// Enable debug logging
process.env.DEBUG = 'nodemailer:*';
// Or in code
nodemailer.createTestAccount((err, account) => {
console.log('Credentials obtained, sending message...');
});
Performance Tips
- Use Connection Pooling:
let transporter = nodemailer.createTransport({
pool: true,
maxConnections: 5,
maxMessages: 100
});
- Enable Compression:
let transporter = nodemailer.createTransport({
compression: true
});
- Use Environment Variables for sensitive data in production.