Last active
November 28, 2023 23:59
-
-
Save rafaelfpviana/1eda85aa477df532e8f00c0721155101 to your computer and use it in GitHub Desktop.
An example of One-Click Unsubscribe header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Resend } from 'resend'; | |
import { createHash } from 'crypto'; | |
const resend = new Resend('RESEND_KEY'); | |
(async function() { | |
try { | |
const emailData = { | |
from: 'Acme <onboarding@resend.dev>', | |
to: ['delivered@resend.dev'], | |
subject: 'hello world', | |
text: 'it works!' | |
}; | |
const unique = `${emailData.from}-${emailData.to}-${emailData.subject}-${Date.now()}`; | |
const key = createHash('sha256').update(unique).digest('hex'); | |
const mechanisms = [ | |
`<mailto:[channel-slack-randomid]@[slackdomain].slack.com?subject=Unsubscribe&body=${key}>`, | |
`<https://example.com/u/${key}>` | |
]; | |
const data = await resend.emails.send({ | |
...emailData, | |
headers: { | |
'List-Unsubscribe': mechanisms.join(', '), | |
'List-Unsubscribe-Post': 'List-Unsubscribe=One-Click' | |
} | |
}); | |
console.log(data); | |
// Save data.id and key to your database | |
// This will allow you to collect the unsubscribe requests and map back to the email address that requested it. | |
} catch (error) { | |
console.error(error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment