Sending Emails with SMTP in Node.js

Send emails through AhaSend’s SMTP servers using Node.js with Nodemailer, the most popular and feature-rich email library for Node.js. This guide covers everything from basic setup to advanced features like attachments and custom headers.
Why Nodemailer? Node.js doesn’t have built-in SMTP support, but Nodemailer provides a powerful, well-maintained solution with extensive features including HTML emails, attachments, embedded images, and robust error handling.

Prerequisites

Before you begin, ensure you have:
Need SMTP Credentials? If you haven’t created SMTP credentials yet, check out our SMTP Credentials guide for step-by-step instructions.

Installation

Install Nodemailer in your Node.js project:
npm install nodemailer
For TypeScript projects, also install type definitions:
TypeScript Support
npm install --save-dev @types/nodemailer

Connection Settings

Use these settings for all Nodemailer configurations with AhaSend:

Primary Server

Host: send.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS (requireTLS: true)
Authentication: Required

US Server

Host: send-us.ahasend.com
Ports: 587 (recommended), 25, 2525
Security: STARTTLS (requireTLS: true)
Authentication: Required

Basic Email Examples

const nodemailer = require('nodemailer');

// Create transporter
const transporter = nodemailer.createTransporter({
    host: 'send.ahasend.com',
    port: 587,
    requireTLS: true, // Force TLS
    auth: {
        user: 'YOUR_SMTP_USERNAME',
        pass: 'YOUR_SMTP_PASSWORD'
    }
});

// Email options
const mailOptions = {
    from: '"Your Company" <[email protected]>',
    to: '[email protected]',
    subject: 'Welcome to our platform!',
    text: 'Thanks for signing up. We\'re excited to have you!'
};

// Send email
transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log('Error:', error);
    }
    console.log('Email sent successfully!');
    console.log('Message ID:', info.messageId);
});
Pro Tip: Use connection pooling for bulk emails, always provide both HTML and text versions, and implement proper error handling with retry logic. Start with sandbox mode during development and use environment variables for credentials.

Testing with Sandbox Mode

Use sandbox mode to safely test your Node.js email integration:
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransporter({
    host: 'send.ahasend.com',
    port: 587,
    requireTLS: true,
    auth: {
        user: 'YOUR_SMTP_USERNAME',
        pass: 'YOUR_SMTP_PASSWORD'
    }
});

const mailOptions = {
    from: '"Test Sender" <[email protected]>',
    to: '[email protected]',
    subject: 'Sandbox Test Email',
    headers: {
        'AhaSend-Sandbox': 'true',
        'AhaSend-Sandbox-Result': 'deliver'
    },
    text: 'This email is sent in sandbox mode for testing.'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log('Error:', error);
    }
    console.log('Sandbox email sent successfully!');
    console.log('Message ID:', info.messageId);
});
Sandbox Benefits: Emails sent in sandbox mode are free, trigger webhooks normally, and never actually deliver to recipients - perfect for development and testing.

Environment Variables & Best Practices

Using Environment Variables

const nodemailer = require('nodemailer');
require('dotenv').config(); // npm install dotenv

const transporter = nodemailer.createTransporter({
    host: process.env.SMTP_HOST || 'send.ahasend.com',
    port: parseInt(process.env.SMTP_PORT) || 587,
    requireTLS: true,
    auth: {
        user: process.env.SMTP_USERNAME,
        pass: process.env.SMTP_PASSWORD
    }
});

// Validate required environment variables
if (!process.env.SMTP_USERNAME || !process.env.SMTP_PASSWORD) {
    console.error('Missing required environment variables');
    console.error('Set: SMTP_USERNAME, SMTP_PASSWORD');
    process.exit(1);
}

const mailOptions = {
    from: `"${process.env.FROM_NAME}" <${process.env.FROM_EMAIL}>`,
    to: '[email protected]',
    subject: 'Environment Test',
    text: 'This email uses environment variables for configuration.'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log('Error:', error);
    }
    console.log('Email sent using environment variables!');
});

Resources