AI Workshop: learn to build apps with AI →
Tips: nodemailer, how to embed an image into an email

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


I needed to embed an image in an email I was sending with nodemailer.

I tried using an attachment, but the image was added as an attachment.

So I embedded the image as base64 into the email body.

First I added some imports:

import fs from 'node:fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

We need to do that __filename and __dirname setup because with ES modules (import syntax) __dirname is not available, and fs.readFileSync() expects absolute paths, not relative ones.

Long story short, do that.

Now read the image:

const imageData = fs.readFileSync(__dirname + '/image.jpg', 'binary')

Transform that into a base64-encoded string:

const src = `data:image/jpg;base64,${Buffer.from(
  imageData,
  'binary'
).toString('base64')}`

Finally you can add that to the email body:

const mailOptions = {
  //...
  html: `<img style="width:800px;" src="${src}">`,
}

Lessons in this unit:

0: Introduction
1: Axios crashes the Node.js process when the request fails
2: How to set up a cron job that runs a Node.js app
3: How to get both parsed body and raw body in Express
4: Interact with the Google Analytics API using Node.js
5: How to bulk convert file names using Node.js
6: How to deep copy JavaScript objects using structuredClone
7: How to handle file uploads in Node.js
8: How to send an email using nodemailer
9: Logging all the requests coming through an Express app
10: How to upload an image to S3 using Node.js
11: How to read a CSV file with Node.js
12: How to set the current working directory of a Node.js program
13: How to upload files to S3 from Node.js
14: How to write a CSV file with Node.js
15: Where to host a Node.js app
16: Parsing JSON with Node.js
17: ▶︎ nodemailer, how to embed an image into an email
18: The Pug Guide
19: Restarting a Node process without file changes
20: How to use Sequelize to interact with PostgreSQL