1

My app sends email newsletters. I'm using SendGrid. I want to use SendGrid's subscription tracking to provide the user-facing unsubscribe interface, because it authenticates the unsubscribe URL, and it sets a List-Unsubscribe header which includes the mailto: method.

I want to keep track of unsubscriptions in my database. I do this with SendGrid's unsubscribe webhook event: when a user clicks the unsubscribe URL, or sends to the List-Unsubscribe address, SendGrid triggers an unsubscribe webhook for me.

Here's my code so far, which works:

// Sending a newsletter

import * as sgMail from '@sendgrid/mail';
sgMail.send({
  customArgs: {
    newsletter_id: 'cats',
  },
  to: 'foo@bar.com',
  from: 'me@app.com',
  trackingSettings: {
    subscription_tracking: {
      enable: true,
      substitution_tag: '--unsubscribe--',
    },
  },
  html: '<a href="--unsubscribe--">Click here to unsubscribe</a>'
});

// Then, in SendGrid webhook endpoint:

const webhookEvents = await extractSGWebhookBody(req);
for (const ev of webhookEvents) {
  if (ev.event === 'unsubscribe') {
    const newsletterId = ev['newsletter_id'];
    const accountId = ev[EMAIL_TAG_ACCOUNT_ID];
    dbUnsubscribe(ev.email, newsletterId);
  }
}

However, as well as triggering a webhook, SendGrid also adds the email address to its own global unsubscribe/suppression list. This is undesirable, because I then have two places where unsubscribes are tracked. They will get out of sync, for many reasons (e.g. if the user re-subscribes within the app, SendGrid won't know about it).

So, I want to disable SendGrid's unsubscribe lists. I just want to use its unsubscribe feature as a proxy: user clicks unsubscribe, then SendGrid sends me a webhook.

How do I stop SendGrid's unsubscribe URL from adding the user's email address to its own unsubscribe list?

jameshfisher
  • 34,029
  • 31
  • 121
  • 167

2 Answers2

1

It's best to let SendGrid be the source of truth for unsubscribes, that way you will never accidentally send an email to a user who has unsubscribed to that group. However, you can keep your application in sync with SendGrid.

To stop your application going out of sync with SendGrid there are Event Webhooks for resubscribes. So if a user resubscribes through SendGrid, your application can know about it.

And if a user resubscribes through your application, your application needs to delete the suppression (unsubscribe record) so that SendGrid will send to that user again.


To my knowledge, there is no way to stop SendGrid from keeping the information about unsubscribes, when you use the SendGrid methods like the generated unsubscribe link and the List-Unsubscribe header. To keep all that data in your application, you should implement unsubscribes yourself.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Thanks Phil. I strongly distrust synchronization (everything always goes out of sync). So I'd really like to just keep this in my own database. If there's no way to disable the global unsubscribe list, I'll just implement the unsubscribe link myself. Thanks again though. – jameshfisher Sep 06 '22 at 16:35
  • (Also if you could explicitly confirm that there's no way to disable SendGrid from adding to the unsubscribe list, I'll mark this as the accepted answer) – jameshfisher Sep 06 '22 at 16:36
  • I definitely understand not trusting synchronisation, though in this case I'd be more inclined to let SendGrid look after the unsubscribes and have your application depend on it that way, since SendGrid is built to handle mailing lists and subscription states. There is, to my knowledge at least, no way to disable SendGrid storing unsubscribe information when using SendGrid methods and I have updated my answer to say that. – philnash Sep 07 '22 at 00:52
  • > "So if a user resubscribes through SendGrid, your application can know about it." There seems to exist one flaw with this. There are `Group Unsubscribe`, `Group Resubscribe` and (global) `Unsubscribe` events, but a global `Resubscribe` event is missing. Admittedly, it's not likely many users will do this, but it still rules out perfect synchronisation when relying on the webhooks alone. – Thor Galle Apr 03 '23 at 12:35
0

I had faced similar problem, so just FYI - there is no option from Sendgrid side as of now to provide Group Unsubscribe option from unsubscribe preferences without including the button - "Opt out of all Emails". that's sad.

There is an alternative option to use - Bypass_List_Management feature if you wish to overwrite suppressions list. However, they recommend doing it, only during emergencies.

https://docs.sendgrid.com/ui/sending-email/index-suppressions#bypass-suppressions

karan chavan
  • 215
  • 2
  • 12