Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/utils",
"version": "0.0.57",
"version": "0.0.58",
"description": "Chatwoot utils",
"private": false,
"license": "MIT",
Expand Down
11 changes: 9 additions & 2 deletions src/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function getRecipients(
cc: email?.cc || [],
bcc: email?.bcc || [],
from: email?.from || [],
to: [],
to: email?.to || [],
};
} else {
const {
Expand Down Expand Up @@ -100,11 +100,18 @@ export function getRecipients(
// - Remove inbox and forward-to email to prevent loops
// - Remove emails matching the reply UUID pattern
const replyUUIDPattern = /^reply\+([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/i;
// Senders may write the inbox address in any case, e.g. HELLO@example.com
const inboxEmails = [inboxEmail, forwardToEmail].map(email =>
email?.toLowerCase()
);
cc = cc.filter(email => {
if (email === conversationContact && isLastEmailFromContact) {
return false;
}
if (email === inboxEmail || email === forwardToEmail) {
if (inboxEmails.includes(email.toLowerCase())) {
return false;
}
if (to.includes(email)) {
Comment thread
iamsivin marked this conversation as resolved.
Resolved
return false;
}
if (replyUUIDPattern.test(email)) {
Expand Down
50 changes: 49 additions & 1 deletion test/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import {

const createIncomingEmail = ({
from = [],
to = [],
cc = [],
bcc = [],
}: {
from?: string[];
to?: string[];
cc?: string[];
bcc?: string[];
}) => {
return {
message_type: MessageType.INCOMING,
content_attributes: {
email: { from, cc, bcc },
email: { from, to, cc, bcc },
},
} as IncomingEmailMessage;
};
Expand Down Expand Up @@ -157,6 +159,52 @@ describe('getRecipients', () => {
});
});

describe('To Recipients', () => {
test('should add other "to" recipients to cc, excluding the inbox email', () => {
const lastEmail = createIncomingEmail({
from: [conversationContact],
to: [inboxEmail, 'teammate@example.com'],
});
const result = getRecipients(
lastEmail,
conversationContact,
inboxEmail,
forwardToEmail
);
expect(result.to).toEqual([conversationContact]);
expect(result.cc).toEqual(['teammate@example.com']);
});

test('should exclude the inbox email from cc regardless of case', () => {
const lastEmail = createIncomingEmail({
from: [conversationContact],
to: ['INBOX@EXAMPLE.COM'],
});
const result = getRecipients(
lastEmail,
conversationContact,
inboxEmail,
forwardToEmail
);
expect(result.cc).toEqual([]);
});

test('should not add the sender to cc when they are also in "to"', () => {
const lastEmail = createIncomingEmail({
from: ['sender@example.com'],
to: [inboxEmail, 'sender@example.com'],
});
const result = getRecipients(
lastEmail,
conversationContact,
inboxEmail,
forwardToEmail
);
expect(result.to).toEqual(['sender@example.com']);
expect(result.cc).toEqual([conversationContact]);
});
});

describe('BCC Recipients', () => {
test('should add emails in the "bcc" field to the "bcc" array', () => {
const bccEmails = ['bcc1@example.com', 'bcc2@example.com'];
Expand Down
Loading