A Node.js project for sending emails using Nodemailer with support for multiple recipients, HTML content, and attachments.
- Send plain text and HTML emails
- Support for multiple recipients
- File attachments support
- SMTP connection verification
- Environment-based configuration
- Error handling and logging
- Clone or navigate to the project directory
- Install dependencies:
npm install- Copy the example environment file:
cp .env.example .env- Edit
.envfile with your SMTP credentials:
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_SECURE=false
EMAIL_USER=your-email@gmail.com
EMAIL_PASS=your-app-password
EMAIL_FROM_NAME=Your NameIf using Gmail, you need to create an App Password:
- Go to your Google Account settings
- Enable 2-Factor Authentication
- Go to Security > 2-Step Verification > App passwords
- Generate a new app password for "Mail"
- Use this app password in the
EMAIL_PASSfield
For other email providers, update the SMTP settings accordingly:
Outlook/Hotmail:
SMTP_HOST=smtp-mail.outlook.com
SMTP_PORT=587Yahoo:
SMTP_HOST=smtp.mail.yahoo.com
SMTP_PORT=587Custom SMTP:
SMTP_HOST=your-smtp-server.com
SMTP_PORT=587
SMTP_SECURE=falseRun the example script:
node index.jsconst EmailService = require('./emailService');
const emailService = new EmailService();
await emailService.sendEmail({
to: 'recipient@example.com',
subject: 'Hello',
text: 'Plain text message',
html: '<h1>HTML message</h1>'
});await emailService.sendEmail({
to: ['user1@example.com', 'user2@example.com'],
subject: 'Multiple Recipients',
text: 'This goes to multiple people',
html: '<p>This goes to <strong>multiple people</strong></p>'
});await emailService.sendEmail({
to: 'recipient@example.com',
subject: 'Email with Attachment',
text: 'Please find attached',
html: '<p>Please find <strong>attached</strong></p>',
attachments: [
{
filename: 'document.txt',
content: 'File content as string'
},
{
filename: 'image.png',
path: './path/to/image.png'
}
]
});const isConnected = await emailService.verifyConnection();
if (isConnected) {
console.log('SMTP server is ready');
}new EmailService()Creates a new email service instance using environment variables for configuration.
sendEmail(options)
Sends an email with the specified options.
Parameters:
to(string | string[]): Recipient email address(es)subject(string): Email subjecttext(string): Plain text contenthtml(string): HTML contentattachments(array, optional): Array of attachment objects
Returns: Promise with result object containing success, messageId, and response or error
verifyConnection()
Verifies the SMTP connection.
Returns: Promise
email-sender-project/
├── emailService.js # Email service class
├── index.js # Example usage
├── .env.example # Environment variables template
├── .env # Your configuration (not in git)
├── .gitignore # Git ignore file
├── package.json # Dependencies
└── README.md # Documentation
Authentication Error:
- Make sure you're using an app password, not your regular email password
- Verify your SMTP credentials are correct
Connection Timeout:
- Check your SMTP host and port settings
- Ensure your firewall allows outbound connections on the SMTP port
SSL/TLS Errors:
- Try changing
SMTP_SECUREtotruefor port 465 - Use
SMTP_SECURE=falsefor port 587
ISC