From fddd8028e03752f811a207232131f832b82b3c25 Mon Sep 17 00:00:00 2001 From: Cesar Ayuso Date: Thu, 18 Jul 2024 13:05:36 -0700 Subject: [PATCH 1/2] fix: remove email unsub link when unsubcribe tag not present --- .../sendEmail/SendEmailPerformer.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts b/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts index 1df0f9f353..b6c66824ba 100644 --- a/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts +++ b/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts @@ -435,6 +435,16 @@ export class SendEmailPerformer extends MessageSendPerformer return $.html() } + if ($(unsubscribeLinkRef).length === 0) { + _this.logger.info(`Unsubscribe tag is missing`) + emailProfile.unsubscribeLink = '' + } + + if ($(preferencesLinkRef).length === 0) { + _this.logger.info(`Preferences tag missing`) + emailProfile.preferencesLink = '' + } + if (groupId) { const group = emailProfile.groups?.find((grp) => grp.id === groupId) const groupUnsubscribeLink = group?.groupUnsubscribeLink From 1515812812b06ae8e72fa0a4b664ae2e36991bde Mon Sep 17 00:00:00 2001 From: Cesar Ayuso Date: Mon, 22 Jul 2024 15:05:00 -0700 Subject: [PATCH 2/2] chore: add unit test and comment --- .../__tests__/send-email.test.ts | 83 +++++++++++++++++++ .../sendEmail/SendEmailPerformer.ts | 3 + 2 files changed, 86 insertions(+) diff --git a/packages/destination-actions/src/destinations/engage-messaging-sendgrid/__tests__/send-email.test.ts b/packages/destination-actions/src/destinations/engage-messaging-sendgrid/__tests__/send-email.test.ts index 819eb88a3f..4d681348d0 100644 --- a/packages/destination-actions/src/destinations/engage-messaging-sendgrid/__tests__/send-email.test.ts +++ b/packages/destination-actions/src/destinations/engage-messaging-sendgrid/__tests__/send-email.test.ts @@ -940,6 +940,89 @@ describe.each([ expect(sendGridRequest.isDone()).toEqual(true) }) + it('Do not add list-unsubscribe headers when the link is empty in the email profile', async () => { + const bodyHtml = + '

Hi First Name, welcome to Segment

Unsubscribe' + const replacedHtmlWithLink = + '

Hi First Name, welcome to Segment

Unsubscribe' + const expectedSendGridRequest = { + personalizations: [ + { + to: [ + { + email: userData.email + } + ], + bcc: [ + { + email: 'test@test.com' + } + ], + custom_args: { + source_id: 'sourceId', + space_id: 'spaceId', + user_id: userData.userId, + __segment_internal_external_id_key__: 'email', + __segment_internal_external_id_value__: userData.email + } + } + ], + from: { + email: 'from@example.com', + name: 'From Name' + }, + reply_to: { + email: 'replyto@example.com', + name: 'Test user' + }, + subject: `Hello ${userData.lastName} ${userData.firstName}.`, + content: [ + { + type: 'text/html', + value: replacedHtmlWithLink + } + ], + tracking_settings: { + subscription_tracking: { + enable: true, + substitution_tag: '[unsubscribe]' + } + } + } + + const sendGridRequest = nock('https://api.sendgrid.com') + .post('/v3/mail/send', expectedSendGridRequest) + .reply(200, {}) + + const responses = await sendgrid.testAction('sendEmail', { + event: createMessagingTestEvent({ + timestamp, + event: 'Audience Entered', + userId: userData.userId, + external_ids: [ + { + collection: 'users', + encoding: 'none', + id: userData.email, + isSubscribed: true, + unsubscribeLink: 'http://unsubscribe_link', + preferencesLink: '', + type: 'email' + } + ] + }), + settings, + mapping: getDefaultMapping({ + body: undefined, + bodyHtml: bodyHtml, + bodyType: 'html' + }) + }) + + expect(responses.length).toBeGreaterThan(0) + expect(sendGridRequest.isDone()).toEqual(true) + }) + it('inserts unsubscribe link in all the places in the html body', async () => { const bodyHtml = '

Hi First Name, welcome to Segment. Here is an Unsubscribe link.

Unsubscribe | Manage Preferences' diff --git a/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts b/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts index b6c66824ba..425575b381 100644 --- a/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts +++ b/packages/destination-actions/src/destinations/engage-messaging-sendgrid/sendEmail/SendEmailPerformer.ts @@ -435,6 +435,9 @@ export class SendEmailPerformer extends MessageSendPerformer return $.html() } + // Remove unsubscribe link from email profile if no substution tag is found this will keep subscription tracking on + // so Sendgrid can keep track of unsubscriptions from the email header. This allows customers that use their own unsubscribe + // links to be able to set up a webhook to track unsubscribe events and sync it with their own subscription management system. if ($(unsubscribeLinkRef).length === 0) { _this.logger.info(`Unsubscribe tag is missing`) emailProfile.unsubscribeLink = ''