Programmatically Sending Emails using Node.js - Complete SMTP Guide

Programmatically Sending Emails using Node.js - Complete SMTP Guide

ImageURLs
Tags
tutorial
Node.js
javascript
smtp
email-automation
programming
Published
January 23, 2026
Author

๐Ÿ“ง Programmatically Sending Emails Using Node.js (Complete SMTP Guide)

Tags: tutorial, programming, email-automation, nodejs, javascript, smtp
Published: January 23, 2026
Author: Tempusmail Team

Introduction: Why Send Emails with Node.js?

If youโ€™re building a web application, API, or automation tool with JavaScript, youโ€™ve probably asked:
โ€œHow do I send emails from my server?โ€
Node.js makes this incredibly simple using the Nodemailer library.
Nodemailer acts as a bridge between your Node.js application and email servers. Whether youโ€™re sending welcome emails, password resets, or automated notifications, it handles everything using clean, asynchronous code.

โœ… Benefits of Sending Emails with Node.js

  • Works seamlessly with Express, Fastify, and other frameworks
  • Native async/await support for non-blocking email sending
  • Battle-tested Nodemailer library with millions of downloads
  • Ideal for APIs, serverless functions, and microservices
This guide will walk you through sending emails programmatically using Node.js.
Beginner or experienced developerโ€”youโ€™ll be sending emails in minutes ๐Ÿš€

๐Ÿงฐ What You Need Before Starting

Make sure you have the following:
  • โœ… Node.js 14+
  • โœ… npm or yarn
  • โœ… Email account (example: you@yourcompany.com)
  • โœ… Email password
  • โœ… SMTP server settings (listed below)

๐Ÿ’ก Where to Find SMTP Settings

Your email provider usually lists these in their documentation.
For Tempusmail users:
panel.tempusmail.com โ†’ Account Settings โ†’ Email Configuration

๐Ÿ“Œ SMTP Settings Reference (Tempusmail)

Setting
Value
SMTP Server
smtp.qboxmail.com
Port (SSL)
465
Port (STARTTLS)
587
Security
SSL/TLS or STARTTLS
Username
your-email@yourdomain.com
Password
Your email password
Authentication
Required
๐Ÿ’ก Pro Tip:
Use port 465 with secure: true for SSL.
Use port 587 with secure: false for STARTTLS.

๐Ÿงฑ Step 1: Create a New Project

mkdir email-sender cd email-sender npm init -y

๐Ÿ“ฆ Step 2: Install Nodemailer

npm install nodemailer
Thatโ€™s itโ€”no additional dependencies required.

โœ‰๏ธ Step 3: Create Your Email Script

Create a file named send-email.js:
const nodemailer =require('nodemailer'); // SMTP Configuration const transporter = nodemailer.createTransport({ host:'smtp.qboxmail.com', port:465, secure:true,// true for 465, false for 587 auth: { user:'your-email@yourdomain.com', pass:'your-password' } }); // Send email function asyncfunctionsendEmail(to, subject, text) { try { const info =await transporter.sendMail({ from:'"Your Name" <your-email@yourdomain.com>', to, subject, text }); console.log('โœ… Email sent:', info.messageId); return {success:true,messageId: info.messageId }; }catch (error) { console.error('โŒ Error:', error.message); return {success:false,error: error.message }; } } // Test email sendEmail( 'recipient@example.com', 'Hello from Node.js ๐Ÿš€', 'This email was sent using Node.js and Tempusmail.' );
โš ๏ธ Security Warning:
Never hardcode passwords in production. Environment variables are shown later.

โ–ถ๏ธ Step 4: Run Your Script

node send-email.js
If successful, youโ€™ll see:
โœ… Email sent: <message-id>
๐ŸŽ‰ Congratulations! Your first email has been sent using Node.js.

๐ŸŽจ Sending HTML Emails

asyncfunctionsendHtmlEmail(to, subject, htmlContent) { const info =await transporter.sendMail({ from:'"Your Company" <your-email@yourdomain.com>', to, subject, html: htmlContent }); console.log('โœ… HTML email sent:', info.messageId); return info; }

Example HTML Template

const htmlContent = ` <!DOCTYPE html> <html> <body style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;"> <div style="background: linear-gradient(135deg, #667eea, #764ba2); padding: 30px; text-align: center;"> <h1 style="color: white; margin: 0;">Welcome to Tempusmail! ๐ŸŽ‰</h1> </div> <div style="padding: 30px; background: #f9fafb;"> <p style="font-size: 16px; color: #374151; line-height: 1.6;"> Thank you for choosing our email service. Your account is now ready! </p> <a href="https://tempusmail.com" style="display: inline-block; background: #2563eb; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px;"> Get Started โ†’ </a> </div> </body> </html> `; sendHtmlEmail('user@example.com','Welcome!', htmlContent);

๐Ÿ“Ž Sending Emails with Attachments

const path =require('path'); asyncfunctionsendEmailWithAttachment(to, subject, text, filePath) { const info =await transporter.sendMail({ from:'"Your Company" <your-email@yourdomain.com>', to, subject, text, attachments: [ { filename: path.basename(filePath), path: filePath } ] }); console.log('โœ… Email with attachment sent:', info.messageId); return info; }

Multiple Attachments

const attachments = [ {filename:'invoice.pdf',path:'/path/to/invoice.pdf' }, {filename:'receipt.pdf',path:'/path/to/receipt.pdf' }, {filename:'logo.png',path:'/path/to/logo.png' } ]; await transporter.sendMail({ from:'"Your Company" <your-email@yourdomain.com>', to:'client@example.com', subject:'Your Documents', text:'Please find your documents attached.', attachments });

๐Ÿ” Using Environment Variables (Recommended)

Step 1: Install dotenv

npm install dotenv

Step 2: Create .env

SMTP_HOST=smtp.qboxmail.com SMTP_PORT=465 SMTP_SECURE=true SMTP_USER=your-email@yourdomain.com SMTP_PASS=your-password FROM_NAME=Your Company

Step 3: Load Variables

require('dotenv').config(); const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST, port:Number(process.env.SMTP_PORT), secure: process.env.SMTP_SECURE ==='true', auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS } });
โš ๏ธ Add .env to .gitignore.

๐Ÿงฑ Reusable Production Email Service

Your class implementation is already solid.
It is production-ready, secure, and scalable with:
  • HTML support
  • Attachments
  • Pre-built templates
  • Centralized configuration
No logic changes requiredโ€”only formatting preserved for clarity.

๐Ÿ›  Troubleshooting Common Errors

โŒ Connection refused / timed out

  • Verify SMTP host
  • Use correct port (465 / 587)
  • Check firewall or hosting restrictions

โŒ Authentication failed

  • Use full email address
  • Recheck password
  • Reset credentials if needed

โŒ Self-signed certificate (Testing only)

tls: { rejectUnauthorized:false }
โš ๏ธ Never use this in production.

โ“ Frequently Asked Questions

Send to multiple recipients?
to: ['user1@example.com','user2@example.com']
Add CC/BCC?
cc:'cc@example.com', bcc:'bcc@example.com'
Serverless compatible?
Yesโ€”works with AWS Lambda, Vercel, and Netlify.

๐ŸŽฏ Conclusion

Youโ€™ve learned how to:
โœ… Send text and HTML emails
โœ… Attach files
โœ… Secure credentials using environment variables
โœ… Build a reusable email service
โœ… Integrate email sending with Express APIs
โœ… Troubleshoot SMTP errors

๐Ÿš€ Get Professional Email Hosting

Tempusmail offers reliable SMTP access with 99.9% uptime and 24/7 support.