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?