Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add email unsubscribe header #493

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/platform/src/providers/email/Email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Email {
text: string
html: string
headers?: Record<string, any>
list?: { unsubscribe: string }
}
8 changes: 8 additions & 0 deletions apps/platform/src/providers/email/EmailChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import App from '../../app'
import { Variables, Wrap } from '../../render'
import { EmailTemplate } from '../../render/Template'
import { unsubscribeEmailLink } from '../../subscriptions/SubscriptionService'
import { encodeHashid, pick } from '../../utilities'
import { Email } from './Email'
import EmailProvider from './EmailProvider'
Expand Down Expand Up @@ -34,6 +35,13 @@ export default class EmailChannel {
'X-Campaign-Id': encodeHashid(variables.context.campaign_id),
'X-Subscription-Id': encodeHashid(variables.context.subscription_id),
},
list: {
unsubscribe: unsubscribeEmailLink({
userId: variables.user.id,
campaignId: variables.context.campaign_id,
referenceId: variables.context.reference_id,
}),
},
}
const result = await this.provider.send(email)
return {
Expand Down
9 changes: 1 addition & 8 deletions apps/platform/src/providers/email/EmailProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ export default abstract class EmailProvider extends Provider {
static group = 'email' as ProviderGroup

async send(message: Email): Promise<any> {
const list = this.unsubscribe
? { unsubscribe: [this.unsubscribe] }
: undefined

return await this.transport?.sendMail({
...message,
list,
})
return await this.transport?.sendMail(message)
}

async verify(): Promise<boolean> {
Expand Down
2 changes: 1 addition & 1 deletion apps/platform/src/render/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const Render = (template: string, { user, event, journey, context }: Vari
context,
unsubscribeEmailUrl: new Handlebars.SafeString(unsubscribeEmailLink({
userId: user.id,
campaignId: context?.campaign_id,
campaignId: context.campaign_id,
referenceId: context.reference_id,
})),
preferencesUrl: new Handlebars.SafeString(preferencesLink(user.id)),
Expand Down
10 changes: 10 additions & 0 deletions apps/platform/src/subscriptions/SubscriptionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ publicRouter.get('/email', async ctx => {
ctx.body = '<html><body><h3>You have been unsubscribed!</h3></body></html>'
})

publicRouter.post('/email', async ctx => {
const { user, campaign } = await encodedLinkToParts(ctx.URL)

if (!user) throw new RequestError(SubscriptionError.UnsubscribeInvalidUser)
if (!campaign) throw new RequestError(SubscriptionError.UnsubscribeInvalidCampaign)

await unsubscribe(user.id, campaign.subscription_id)
ctx.status = 200
})

/**
***
* User-facing subscription preferences page
Expand Down
Loading