How to add an unsubscribe functionality in Nodemailer?

1.2k Views Asked by At

After looking into the docs for Nodemailer, I couldn't find anything about unsubscribing to an email.

I am trying to create a Newsletter for my web-app, and I don't want to use emailing services such as Mailchimp or any other due to the fact they are not free forever.

Do you have any ideas on what I should do?

Help will be very appreciated!

1

There are 1 best solutions below

0
On
const htmlEmailBody = `generate the HTML Body of the email`
const emailHash = `abc123` // Best way is to get this from your database. 

await transporter.sendMail({
    from: {
      name: 'Your Name',
      address: '[email protected]',
    },
    to: userEmailId,
    replyTo: '[email protected]',
    subject: `Hello World`,
    html: htmlEmailBody,
    list: {
      unsubscribe: {
        url: `https://example.com/unsubscribe/${emailHash}`,
        comment: 'Unsubscribe from Daily Updates',
      },
    }
  });

My other js file which initialises transporter. Sharing this for sake of completeness.

import { createTransport } from 'nodemailer';
import * as aws from "@aws-sdk/client-ses";

// Create SES instance
const sesClient = new aws.SES({
  region: 'ap-south-2',
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  },
});

export const transporter = createTransport({
  SES: {
    ses: sesClient,
    aws,
  },
  // https://nodemailer.com/transports/ses/#example-2
  sendingRate: 12, // max 12 emails per second
});