Installation

Install nodemailer via your package manager:

npm install nodemailer

Basic Configuration

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransporter({
  host: 'smtp.lettermint.co',
  port: 587,
  secure: false, // true for 465, false for other ports
  auth: {
    user: 'lettermint',
    pass: 'your-api-token',
  },
});

async function sendEmail() {
  try {
    const info = await transporter.sendMail({
      from: 'sender@yourdomain.com',
      to: 'recipient@example.com',
      subject: 'Test Email',
      text: 'Hello! This is a test email.',
      html: '<h1>Hello!</h1><p>This is a test email.</p>',
    });

    console.log('Message sent:', info.messageId);
  } catch (error) {
    console.error('Error sending email:', error);
  }
}

sendEmail();

Advanced Features

Multiple Recipients

const mailOptions = {
  from: 'sender@yourdomain.com',
  to: ['user1@example.com', 'user2@example.com'],
  cc: 'manager@yourdomain.com',
  bcc: 'archive@yourdomain.com',
  subject: 'Newsletter',
  html: '<h1>Monthly Update</h1>',
};

Attachments

const mailOptions = {
  from: 'sender@yourdomain.com',
  to: 'recipient@example.com',
  subject: 'Document Attached',
  html: '<p>Please find the document attached.</p>',
  attachments: [
    {
      filename: 'document.pdf',
      path: './files/document.pdf',
    },
    {
      filename: 'data.json',
      content: JSON.stringify({ key: 'value' }),
    },
  ],
};

Custom Headers

const mailOptions = {
  from: 'sender@yourdomain.com',
  to: 'recipient@example.com',
  subject: 'Custom Headers',
  html: '<p>Email with custom headers.</p>',
  headers: {
    'X-Priority': '1',
    'X-Custom-Header': 'Custom Value',
  },
};