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.
const nodemailer = require('nodemailer');// Create transporterconst transporter = nodemailer.createTransporter({ host: 'send.ahasend.com', port: 587, requireTLS: true, // Force TLS auth: { user: 'YOUR_SMTP_USERNAME', pass: 'YOUR_SMTP_PASSWORD' }});// Email optionsconst 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 emailtransporter.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.
Use sandbox mode to safely test your Node.js email integration:
Copy
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.