1

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!

GeorGios
  • 91
  • 3
  • 16
  • your newsletter adds the email to db. so just put a link in the email for unsubscription that searches for that mailid and removes it. – Aritra Chakraborty Jul 11 '21 at 10:57
  • 2
    As @AritraChakraborty said, but consider masking the email address with a slug, so hackers won't abuse this API and start unsubscribing others. A slug would be a unique hash to the email (so `mail@mydomain.com` would be hashed to something like `ee26b0dd4af7e749aa1a8ee3c10ae9923f6`) – Narxx Jul 11 '21 at 11:25
  • Hi @Narxx. Yes, I was looking for ideas to implement the hashing functionality which I am not sure how to implement it, nor remember how it works. I was wondering if you could provide me with some guidance on how to approach this? – GeorGios Jul 12 '21 at 15:49
  • @GeorGios It's too complicated to write a solution in the commend section, and I cannot post this as an answer since it's not what you were asking in the post. But in short try to use a hashing function like `SHA512` on the email string, with some constant salt, and then u could trim the result to fit into a shorter slug for URL. – Narxx Jul 14 '21 at 07:46

1 Answers1

0
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: 'abc@example.com',
    },
    to: userEmailId,
    replyTo: 'abc@example.com',
    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
});
Adarsh Madrecha
  • 6,364
  • 11
  • 69
  • 117