Skip to main content
Complete guide to sending emails with Nodemailer and MailDiver SMTP.

Basic Setup

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  host: 'smtp.maildiver.com',
  port: 587,
  secure: false, // true for port 465, false for 587
  auth: {
    user: 'maildiver',
    pass: 'MAILDIVER_API_KEY',
  },
});

await transporter.sendMail({
  from: 'hello@yourdomain.com',
  to: 'customer@example.com',
  subject: 'Welcome to our service!',
  html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
  text: 'Welcome! Thanks for signing up.',
});

Multiple Recipients

Send emails to multiple recipients using CC and BCC:
await transporter.sendMail({
  from: 'hello@yourdomain.com',
  to: ['customer1@example.com', 'customer2@example.com'],
  cc: 'manager@example.com',
  bcc: 'archive@yourdomain.com',
  replyTo: 'support@yourdomain.com',
  subject: 'Team Update',
  html: '<p>Hello team!</p>',
});
Limits:
  • Maximum 50 recipients per field (to, cc, bcc)
  • Each field is validated separately

File Attachments

Attach files to your emails:
await transporter.sendMail({
  from: 'hello@yourdomain.com',
  to: 'customer@example.com',
  subject: 'Your Invoice',
  html: '<p>Please find your invoice attached.</p>',
  attachments: [
    {
      filename: 'invoice.pdf',
      path: '/path/to/invoice.pdf',
    },
    {
      filename: 'receipt.txt',
      content: Buffer.from('Thank you for your purchase!'),
    },
  ],
});
Limits:
  • Maximum 10 attachments per email
  • Maximum 40MB total attachment size

Connection Pooling

For bulk email sending, use connection pooling to improve performance:

Without Pooling (Single Connection)

// Opens new connection for each email (slower)
const transporter = nodemailer.createTransport({
  host: 'smtp.maildiver.com',
  port: 587,
  secure: false,
  pool: false, // No pooling
  auth: {
    user: 'maildiver',
    pass: 'MAILDIVER_API_KEY',
  },
});

for (const email of emails) {
  await transporter.sendMail(email);
}
// Reuses connections for better performance
const transporter = nodemailer.createTransport({
  host: 'smtp.maildiver.com',
  port: 587,
  secure: false,
  pool: true, // Enable pooling
  maxConnections: 5, // Max 5 concurrent connections
  maxMessages: 100, // Max 100 emails per connection
  rateDelta: 1000, // 1 second window
  rateLimit: 5, // Max 5 emails/second
  auth: {
    user: 'maildiver',
    pass: 'MAILDIVER_API_KEY',
  },
});

// Send multiple emails
for (const email of emails) {
  await transporter.sendMail(email);
}

// Close pool when done
transporter.close();
Benefits of Connection Pooling:
  • Authenticates once, reuses connections
  • Reduces TLS handshake overhead
  • Much faster for bulk sending (10-20x improvement)
  • Automatic connection rotation

Next Steps

Back to Overview

Return to SMTP overview, advanced features, validation rules, and troubleshooting