How can I configure the SendGrid unsubscribe link to not add the email to any unsubscribe lists? - sendgrid

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: 'Click here to unsubscribe'
});
// 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?

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.

Related

Gmail automated unsubscribe emails - How to stop them?

When a user unsubscribes from an email in gmail, it seems gmail sends an email to the sender with:
`subject`: "unsubscribe"
`body`: "This message was automatically generated by Gmail."
`to`: u+(somesuperlongrandomstring)#domain.tld
`from`: (Email of user who unsubscribed)
Similarly, when a user unsubscribes from an email in Apple Mail, it seems Apple Mail sends an email to the sender with:
`subject`: (blank)
`body`: Apple Mail sent this email to unsubscribe from the message "(Subject of email unsubscribed from)".
`to`: u+(somesuperlongrandomstring)#domain.tld
`from`: Email of user who unsubscribed
We use mailgun to handle unsubscribes already, but as of 28th March 2022, these emails from both providers now make it through our email routing.
Does anyone know how to tell gmail or Apple Mail to not send these emails? They are redundant because mailgun is already handling the unsubscribe using the list-unsubscribe header as you'd expect.
Alternatively, does anyone know how to set up Mailgun so these messages aren't passed through routes?
So Mailgun didn't provide any help, sadly. But looking through their documentation, and the headers of these emails, it seems possible to use their Routes to hide the emails completely.
The three clues are:
Emails always coming in the format u+(...)#domain.tld
Gmail unsubscribes always have the subject "unsubscribe"
Apple Mail unsubscribes always have the header: X-Apple-Unsubscribe:true
As such, you can create a route as such to capture and discard these emails using a custom route with the raw expression set to:
match_recipient("u\+(.*)#domain.tld") and (match_header("X-Apple-Unsubscribe", "true") or match_header('subject', 'unsubscribe'))
It's sad that Mailgun stopped doing this themselves, but at least there is a solution with their tooling!

How does SendGrid notify (delivered-open-click) events when multiple email recipients are specified

When you send an email through sendgrid, and the recipient opens the email, SendGrid will notify my server about that using webhooks (open) event.
https://docs.sendgrid.com/for-developers/tracking-events/event
But what happens if I specify multiple recipients for the same email with personalizations, do I get the same (open) event every time one of the recipients opens that email? If so how can my server figure which recipient?
What about the (delivered) event? Same questions.
Twilio SendGrid developer evangelist here.
The best way I found to achieve this is to send custom_args with the email, you can add custom_args to each individual personalisation object. Parameters set in custom_args are also sent along in the event webhook.
I wrote a post about how to use custom_args to track email statuses. The post uses Ruby on Rails as the framework, but covers using custom_args and reading them back in the event webhook to update the email in your own database.

SendGrid - How to hide the "View Opt Out Preferences" button from Opt-Out Preferences page

We only enable unsubscribe links for all the marketing emails.(we have unsubscribe groups for that) So we don't provide unsubscribe links for other types of email, for example, a payment request email.
However, we discovered that on the Preferences page, users can still choose to Opt-Out of all Emails (global unsubscribe) thus they will no longer receive any email from us including the payment request. So wondering how could I disable this "View Opt Out Preferences" button so that users can only Opt-out one single group?
Edit Sep 20:
there is no such setting for custom unsubscribe link. Offical docu is out of date.
https://docs.sendgrid.com/ui/sending-email/create-and-manage-unsubscribe-groups#using-a-custom-unsubscribe-link
Twilio SendGrid developer evangelist here.
Sorry it's taken me a while to get to this. I've been chasing down what can be done here. There are a few options:
Subusers
If you are on a Pro account or higher, you can set up subusers within your account. The recommendation is to set up a subuser for your marketing emails and a different subuser for your transactional emails. That way unsubscribes from the marketing list won't affect the transactional email side of things. You can read about setting up subuser accounts here.
Create your own unsubscribe page
Rather than using the SendGrid provided unsubscribe page, that allows users to get to the "Opt out of all emails" button, you can create your own unsubscribe page. You can add a custom unsubscribe link to your emails. You can then use the API to add the email address to one of your unsubscribe groups.
Bypass suppressions
You can set the bypass_list_management filter to true when sending your mail to ignore all unsubscribe groups/suppressions. This seems like a last resort sort of fix, rather than something you should use for all transactional emails. The docs are fairly strongly worded on this:
It is important to respect unsubscribes, and these filters should be used only when it is absolutely necessary to deliver a message to recipients who have unsubscribed from your emails. For example, you may use these filters to deliver messages that you are legally required to send to all recipients or important security messages like a password reset.
Check out more on bypass list management.

How do I add unsubscribe link to emails sent via Mandrill

How do I add unsubscribe link to emails sent via Mandrill automatically. Specifically, I need a feature in Mandrill not something I would have to develop at my end.
Just add a link like the following:
Click here to unsubscribe.
to your emails and Mandrill will put an unsubscribe link and will also provide the backend mechanism to automatically unsubscribe the user. This is done by adding the email address to your Rejection Balcklist in your account.
For more details, please see the original documentation

How can I setup an inbound action on an email incoming

I am developing an application, where I have a use case where I advertise a particular email id on a web site. Now, I need people to send me some information and an attachment to that email id . When I receive that email, it should trigger some action in my web application, such that I have both this document and the body of the email available to be in my web application. Then I can parse this information and take appropriate action. Let me know if it is possible to do this kind of email processing using Mandrill or Mailgun or SendGrid, and if yes, can you provide details/documention links on how this can be done.
Thanks
kabir
All three of those services support "inbound" email parsing, which should allow you to process and react to received emails.
Essentially they'll receive and process the email on your behalf, and send you a webhook with normalized data. Docs links below.
SendGrid Inbound Parse Webhooks
Mailgun Inbound Routes API
Mandrill Inbound Email Parsing