How to send emails with Nodemailer

Sending emails is crucial for Node.js applications that handle user notifications, password resets, and transactional messaging. As the creator of CoreUI with over 11 years of Node.js development experience since 2014, I’ve implemented email functionality in countless enterprise applications. The most effective solution is to use Nodemailer with SMTP transport for reliable email delivery. This approach supports all major email providers and provides full control over email content and formatting.

Use Nodemailer to send emails in Node.js applications.

const express = require('express')
const nodemailer = require('nodemailer')

const app = express()
app.use(express.json())

// Create transporter
const transporter = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 587,
  secure: false,
  auth: {
    user: process.env.EMAIL_USER,
    pass: process.env.EMAIL_PASSWORD
  }
})

// Verify connection
transporter.verify((error, success) => {
  if (error) {
    console.error('SMTP connection error:', error)
  } else {
    console.log('SMTP server ready')
  }
})

app.post('/send-email', async (req, res) => {
  const { to, subject, text, html } = req.body

  const mailOptions = {
    from: '"My App" <[email protected]>',
    to: to,
    subject: subject,
    text: text,
    html: html || `<p>${text}</p>`
  }

  try {
    const info = await transporter.sendMail(mailOptions)
    console.log('Email sent:', info.messageId)
    res.json({
      success: true,
      messageId: info.messageId
    })
  } catch (error) {
    console.error('Email send error:', error)
    res.status(500).json({
      success: false,
      error: error.message
    })
  }
})

// Send welcome email example
const sendWelcomeEmail = async (userEmail, userName) => {
  const mailOptions = {
    from: '"Welcome Team" <[email protected]>',
    to: userEmail,
    subject: 'Welcome to Our App!',
    html: `
      <h1>Welcome ${userName}!</h1>
      <p>Thank you for joining our platform.</p>
      <p>Get started by exploring our features.</p>
    `
  }

  await transporter.sendMail(mailOptions)
}

First, install Nodemailer with npm install nodemailer. Create a transporter with your SMTP credentials stored in environment variables. The sendMail() method sends emails with specified options including recipient, subject, and content. Support both plain text and HTML formats. Use async/await for proper error handling. Always use environment variables for credentials, never hardcode them.

Best Practice Note

This is the same email implementation we use in CoreUI backend systems for user notifications. For Gmail, use app-specific passwords rather than your regular password. For production, use dedicated email services like SendGrid or AWS SES for better deliverability and analytics. Implement email queues for high-volume sending to avoid blocking requests.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.

Answers by CoreUI Core Team