Skip to content

The open alternative to Salesforce, designed for AI.

fix(messaging): stop syncing unsubscribe requests (#24007)

neo7734ef517d39544
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/constants/auto-generated-body-marker.constant.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/auto-generated-body-marker.constant.ts
new file mode 100644
index 00000000000..aa5b64f6831
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/auto-generated-body-marker.constant.ts
@@ -0,0 +1 @@
+export const AUTO_GENERATED_BODY_MARKER = 'automatically generated';
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/constants/recipient-roles.constant.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/recipient-roles.constant.ts
new file mode 100644
index 00000000000..e50a4bc186f
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/recipient-roles.constant.ts
@@ -0,0 +1,7 @@
+import { MessageParticipantRole } from 'twenty-shared/types';
+
+export const RECIPIENT_ROLES: MessageParticipantRole[] = [
+ MessageParticipantRole.TO,
+ MessageParticipantRole.CC,
+ MessageParticipantRole.BCC,
+];
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/constants/unsubscribe-command-subject.constant.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/unsubscribe-command-subject.constant.ts
new file mode 100644
index 00000000000..29b78baeb1a
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/unsubscribe-command-subject.constant.ts
@@ -0,0 +1 @@
+export const UNSUBSCRIBE_COMMAND_SUBJECT = 'unsubscribe';
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/constants/unsubscribe-keyword-pattern.constant.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/unsubscribe-keyword-pattern.constant.ts
new file mode 100644
index 00000000000..ddb906805d5
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/constants/unsubscribe-keyword-pattern.constant.ts
@@ -0,0 +1,2 @@
+export const UNSUBSCRIBE_KEYWORD_PATTERN =
+ /(?<![a-z])(unsubscribe|unsub|opt[.\-_]?out)(?![a-z])/;
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/filter-emails.util.spec.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/filter-emails.util.spec.ts
index dc5fa58aeac..dbcea8c0c0a 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/filter-emails.util.spec.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/filter-emails.util.spec.ts
@@ -171,6 +171,70 @@ describe('filterEmails', () => {
);
});
+ it('should filter out unsubscribe requests even though the user sent them', () => {
+ const primaryHandle = '[email protected]';
+ const messages: MessageWithParticipants[] = [
+ {
+ externalId: 'gmail-unsubscribe',
+ subject: 'unsubscribe',
+ receivedAt: new Date('2025-09-23T08:28:18.000Z'),
+ text: 'This message was automatically generated by Gmail.',
+ headerMessageId: '<[email protected]>',
+ messageThreadExternalId: 'thread-1',
+ direction: MessageDirection.OUTGOING,
+ participants: [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: '',
+ },
+ ],
+ attachments: [],
+ isDraft: false,
+ },
+ ];
+
+ expect(filterEmails(primaryHandle, [], messages, [])).toEqual([]);
+ expect(filterEmails(primaryHandle, [], messages, [], false)).toEqual([]);
+ });
+
+ it('should filter out the unsubscribe command to a neutral list address, which the sender-is-the-user shortcut would otherwise keep', () => {
+ const primaryHandle = '[email protected]';
+ const messages: MessageWithParticipants[] = [
+ {
+ externalId: 'list-unsubscribe-command',
+ subject: 'unsubscribe',
+ receivedAt: new Date('2025-09-23T08:28:18.000Z'),
+ text: 'This message was automatically generated by Gmail.',
+ headerMessageId: '<[email protected]>',
+ messageThreadExternalId: 'thread-1',
+ direction: MessageDirection.OUTGOING,
+ participants: [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: '',
+ },
+ ],
+ attachments: [],
+ isDraft: false,
+ },
+ ];
+
+ expect(filterEmails(primaryHandle, [], messages, [])).toEqual([]);
+ expect(filterEmails(primaryHandle, [], messages, [], false)).toEqual([]);
+ });
+
it('should not filter out group emails when excludeGroupEmails is false', () => {
const primaryHandle = '[email protected]';
const messages: MessageWithParticipants[] = [
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/filter-out-unsubscribe-requests.util.spec.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/filter-out-unsubscribe-requests.util.spec.ts
new file mode 100644
index 00000000000..38286634adc
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/filter-out-unsubscribe-requests.util.spec.ts
@@ -0,0 +1,238 @@
+import { MessageParticipantRole } from 'twenty-shared/types';
+
+import { MessageDirection } from 'src/modules/messaging/common/enums/message-direction.enum';
+import { type MessageWithParticipants } from 'src/modules/messaging/message-import-manager/types/message';
+import { filterOutUnsubscribeRequests } from 'src/modules/messaging/message-import-manager/utils/filter-out-unsubscribe-requests.util';
+
+const OWN_HANDLES = ['[email protected]'];
+
+const buildMessage = (
+ externalId: string,
+ participants: MessageWithParticipants['participants'],
+ direction: MessageDirection = MessageDirection.OUTGOING,
+ text: string = 'This message was automatically generated by Gmail.',
+): MessageWithParticipants => ({
+ externalId,
+ subject: 'unsubscribe',
+ receivedAt: new Date('2025-09-23T08:28:18.000Z'),
+ text,
+ headerMessageId: `<${externalId}@mail.gmail.com>`,
+ messageThreadExternalId: `thread-${externalId}`,
+ direction,
+ participants,
+ attachments: [],
+ isDraft: false,
+});
+
+describe('filterOutUnsubscribeRequests', () => {
+ it('should filter out the request the mail client sends on the user behalf when clicking unsubscribe', () => {
+ const messages = [
+ buildMessage('gmail-unsubscribe', [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle:
+ '32.MRTVISTNM5XUEQKONZGTO6K7LF5E6ODWIFNGY6STG53FGMKBGVXDMU3TGZGHAUJSN5IT2PI=@unsubscribe2.customer.io',
+ displayName: '',
+ },
+ ]),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual([]);
+ });
+
+ it('should filter out the confirmation the unsubscribe mailbox sends back', () => {
+ const messages = [
+ buildMessage(
+ 'unsubscribe-confirmation',
+ [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'Vendor',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ ],
+ MessageDirection.INCOMING,
+ ),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual([]);
+ });
+
+ it('should filter out the RFC 2369 unsubscribe command sent to a neutral list address', () => {
+ const messages = [
+ buildMessage('list-command', [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: '',
+ },
+ ]),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual([]);
+ });
+
+ it('should filter out the unsubscribe command even when the client adds a reply-to participant', () => {
+ const messages = [
+ buildMessage('list-command-with-reply-to', [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.REPLY_TO,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: '',
+ },
+ ]),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual([]);
+ });
+
+ it('should keep an outgoing message the user wrote even when its subject is unsubscribe', () => {
+ const messages = [
+ buildMessage(
+ 'human-written',
+ [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: '',
+ },
+ ],
+ MessageDirection.OUTGOING,
+ 'Hi John, please take me off the weekly digest, but keep me on the release announcements. Thanks!',
+ ),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual(
+ messages,
+ );
+ });
+
+ it('should filter out an unsubscribe command its headers flag as auto-generated', () => {
+ const messages = [
+ {
+ ...buildMessage(
+ 'auto-submitted-command',
+ [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: '',
+ },
+ ],
+ MessageDirection.OUTGOING,
+ 'Please remove this address from the list.',
+ ),
+ messageHeaders: [{ name: 'Auto-Submitted', value: 'auto-generated' }],
+ },
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual([]);
+ });
+
+ it('should keep an incoming message that merely happens to be about unsubscribing', () => {
+ const messages = [
+ buildMessage(
+ 'human-asking-to-unsubscribe',
+ [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'John',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ ],
+ MessageDirection.INCOMING,
+ ),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual(
+ messages,
+ );
+ });
+
+ it('should keep a message that also reaches a human recipient', () => {
+ const messages = [
+ buildMessage('mixed-recipients', [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: 'Vendor',
+ },
+ {
+ role: MessageParticipantRole.CC,
+ handle: '[email protected]',
+ displayName: 'John',
+ },
+ ]),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual(
+ messages,
+ );
+ });
+
+ it('should keep a message the user only sent to their own handles', () => {
+ const messages = [
+ buildMessage('self-sent', [
+ {
+ role: MessageParticipantRole.FROM,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ {
+ role: MessageParticipantRole.TO,
+ handle: '[email protected]',
+ displayName: 'User',
+ },
+ ]),
+ ];
+
+ expect(filterOutUnsubscribeRequests(OWN_HANDLES, messages)).toEqual(
+ messages,
+ );
+ });
+});
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/is-unsubscribe-email.util.spec.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/is-unsubscribe-email.util.spec.ts
new file mode 100644
index 00000000000..a77eb0241fa
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/is-unsubscribe-email.util.spec.ts
@@ -0,0 +1,31 @@
+import { isUnsubscribeEmail } from 'src/modules/messaging/message-import-manager/utils/is-unsubscribe-email.util';
+
+describe('isUnsubscribeEmail', () => {
+ it.each([
+ '32.MRTVISTNM5XUEQKONZGTO6K7LF5E6ODWIFNGY6STG53FGMKBGVXDMU3TGZGHAUJSN5IT2PI=@unsubscribe2.customer.io',
+ ])('should recognise %s as an unsubscribe mailbox', (email) => {
+ expect(isUnsubscribeEmail(email)).toBe(true);
+ });
+
+ it.each([
+ ])('should not mistake %s for an unsubscribe mailbox', (email) => {
+ expect(isUnsubscribeEmail(email)).toBe(false);
+ });
+});
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/filter-emails.util.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/filter-emails.util.ts
index c6bae229bd7..e9b5e7bfc7f 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/filter-emails.util.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/filter-emails.util.ts
@@ -5,6 +5,7 @@ import { type MessageWithParticipants } from 'src/modules/messaging/message-impo
import { filterOutBlocklistedMessages } from 'src/modules/messaging/message-import-manager/utils/filter-out-blocklisted-messages.util';
import { filterOutIcsAttachments } from 'src/modules/messaging/message-import-manager/utils/filter-out-ics-attachments.util';
import { filterOutInternals } from 'src/modules/messaging/message-import-manager/utils/filter-out-internals.util';
+import { filterOutUnsubscribeRequests } from 'src/modules/messaging/message-import-manager/utils/filter-out-unsubscribe-requests.util';
import { isBulkMail } from 'src/modules/messaging/message-import-manager/utils/is-bulk-mail.util';
import { isGroupEmail } from 'src/modules/messaging/message-import-manager/utils/is-group-email';
import { isMessageSenderMatchingHandles } from 'src/modules/messaging/message-import-manager/utils/is-message-sender-matching-handles.util';
@@ -20,9 +21,14 @@ export const filterEmails = (
) => {
const messagesWithoutIcsAttachments = filterOutIcsAttachments(messages);
- const messagesWithoutBlocklisted = filterOutBlocklistedMessages(
+ const messagesWithoutUnsubscribeRequests = filterOutUnsubscribeRequests(
[primaryHandle, ...handleAliases],
messagesWithoutIcsAttachments,
+ );
+
+ const messagesWithoutBlocklisted = filterOutBlocklistedMessages(
+ [primaryHandle, ...handleAliases],
+ messagesWithoutUnsubscribeRequests,
blocklist,
);
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/filter-out-unsubscribe-requests.util.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/filter-out-unsubscribe-requests.util.ts
new file mode 100644
index 00000000000..27bacc0ef09
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/filter-out-unsubscribe-requests.util.ts
@@ -0,0 +1,67 @@
+import { isNonEmptyArray, isNonEmptyString } from '@sniptt/guards';
+import { isDefined } from 'twenty-shared/utils';
+
+import { MessageDirection } from 'src/modules/messaging/common/enums/message-direction.enum';
+import { AUTO_GENERATED_BODY_MARKER } from 'src/modules/messaging/message-import-manager/constants/auto-generated-body-marker.constant';
+import { RECIPIENT_ROLES } from 'src/modules/messaging/message-import-manager/constants/recipient-roles.constant';
+import { UNSUBSCRIBE_COMMAND_SUBJECT } from 'src/modules/messaging/message-import-manager/constants/unsubscribe-command-subject.constant';
+import { type MessageWithParticipants } from 'src/modules/messaging/message-import-manager/types/message';
+import { isBulkMail } from 'src/modules/messaging/message-import-manager/utils/is-bulk-mail.util';
+import { isUnsubscribeEmail } from 'src/modules/messaging/message-import-manager/utils/is-unsubscribe-email.util';
+
+export const filterOutUnsubscribeRequests = (
+ messageChannelHandles: string[],
+ messages: MessageWithParticipants[],
+) => {
+ const ownHandles = messageChannelHandles.map((handle) =>
+ handle.toLowerCase(),
+ );
+
+ return messages.filter((message) => {
+ if (!isDefined(message.participants)) {
+ return true;
+ }
+
+ const counterpartyHandles = message.participants
+ .map((participant) => participant.handle?.toLowerCase())
+ .filter(isNonEmptyString)
+ .filter((handle) => !ownHandles.includes(handle));
+
+ if (!isNonEmptyArray(counterpartyHandles)) {
+ return true;
+ }
+
+ if (counterpartyHandles.every(isUnsubscribeEmail)) {
+ return false;
+ }
+
+ const counterpartyRecipients = message.participants.filter(
+ (participant) =>
+ RECIPIENT_ROLES.includes(participant.role) &&
+ isNonEmptyString(participant.handle) &&
+ !ownHandles.includes(participant.handle.toLowerCase()),
+ );
+
+ const counterpartyRecipientHandles = new Set(
+ counterpartyRecipients
+ .map((participant) => participant.handle?.toLowerCase())
+ .filter(isNonEmptyString),
+ );
+
+ const messageText = message.text?.trim().toLowerCase() ?? '';
+
+ const isAutomatedMessage =
+ isBulkMail(message.messageHeaders ?? []) ||
+ messageText === '' ||
+ messageText === UNSUBSCRIBE_COMMAND_SUBJECT ||
+ messageText.includes(AUTO_GENERATED_BODY_MARKER);
+
+ const isUnsubscribeCommandToSingleRecipient =
+ message.direction === MessageDirection.OUTGOING &&
+ counterpartyRecipientHandles.size === 1 &&
+ isAutomatedMessage &&
+ message.subject?.trim().toLowerCase() === UNSUBSCRIBE_COMMAND_SUBJECT;
+
+ return !isUnsubscribeCommandToSingleRecipient;
+ });
+};
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/is-unsubscribe-email.util.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/is-unsubscribe-email.util.ts
new file mode 100644
index 00000000000..0b1a05ab7ce
--- /dev/null
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/is-unsubscribe-email.util.ts
@@ -0,0 +1,37 @@
+import { isNonEmptyString } from '@sniptt/guards';
+import psl from 'psl';
+
+import { isParsedDomain } from 'src/modules/contact-creation-manager/types/is-psl-parsed-domain.type';
+import { UNSUBSCRIBE_KEYWORD_PATTERN } from 'src/modules/messaging/message-import-manager/constants/unsubscribe-keyword-pattern.constant';
+
+export const isUnsubscribeEmail = (email: string): boolean => {
+ const normalizedEmail = email.toLowerCase();
+ const atIndex = normalizedEmail.lastIndexOf('@');
+ const localPart =
+ atIndex === -1 ? normalizedEmail : normalizedEmail.slice(0, atIndex);
+
+ if (UNSUBSCRIBE_KEYWORD_PATTERN.test(localPart)) {
+ return true;
+ }
+
+ if (atIndex === -1) {
+ return false;
+ }
+
+ const parsedDomain = psl.parse(normalizedEmail.slice(atIndex + 1));
+
+ if (!isParsedDomain(parsedDomain)) {
+ return false;
+ }
+
+ // dedicated subdomains like unsubscribe2.customer.io count, registrable
+ // domains like unsubscribe-tools.com or unsubscribe.co.uk do not
+ const subdomain = parsedDomain.subdomain;
+
+ return (
+ isNonEmptyString(subdomain) &&
+ subdomain
+ .split('.')
+ .some((label) => UNSUBSCRIBE_KEYWORD_PATTERN.test(label))
+ );
+};