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

feat: add helper for email recipients #41

Merged
merged 11 commits into from
Feb 6, 2025
Merged

feat: add helper for email recipients #41

merged 11 commits into from
Feb 6, 2025

Conversation

scmmishra
Copy link
Member

@scmmishra scmmishra commented Feb 6, 2025

This PR adds a getRecipients which takes an email message and returns what will be the to, cc and bcc email to reply to. The following are the docs for this


getRecipients

The getRecipients function determines the recipients for an email reply based on the last email message's details, the conversation contact, and system-specific email addresses. This utility is designed to simplify the process of building recipient lists for outgoing emails.

Overview

Given a last email (either incoming or outgoing), a conversation contact, and system emails (inbox and forward-to), getRecipients constructs and returns three lists:

  • To: The primary recipient.

    • For an incoming message, this is the sender's address (i.e., the from field).
    • For an outgoing message, this is the last recipient (i.e., the to field).
  • CC: The carbon copy list containing:

    • The email addresses from the last email's CC.
    • All addresses from the last email's to (in addition to the primary recipient for proper context).
    • The conversation contact if it wasn't the sender of the last email.
    • Any addresses that would cause an email loop (e.g., inbox email, forward-to email, or reply-generated email addresses) are removed.
  • BCC: The blind carbon copy list is typically used for internal purposes. Any instance of the conversation contact is removed from this list because they are already added in the CC.

Additionally, all recipient lists are deduplicated to ensure that each email address appears only once.

Parameters

  • lastEmail (EmailMessage):
    The last email message of the conversation. It should contain:

    • A message_type property (indicating whether the message was incoming or outgoing).
    • A nested content_attributes.email object containing:
      • from: An array of sender email addresses.
      • to: An array of recipient email addresses.
      • cc: An optional array of carbon copy email addresses.
      • bcc: An optional array of blind carbon copy email addresses.
  • conversationContact (string):
    The email address of the contact with whom the conversation is taking place. This is used to determine which email addresses to add (or remove) from the recipient lists.

  • inboxEmail (string):
    The email address of the system's inbox. This is removed from the CC list to avoid email loops.

  • forwardToEmail (string):
    The system's forwarding email address. This is also removed from the CC list to prevent email loops.

Return Value

Returns an object with the following properties:

  • to (string[]):
    An array of email addresses for the primary recipient.

  • cc (string[]):
    An array of email addresses for the CC recipients, excluding system emails and duplicate values.

  • bcc (string[]):
    An array of email addresses for the BCC recipients, with the conversation contact removed if present.

Usage Example

import { getRecipients } from 'your-utils-repo';
import { MessageType } from './types/message';

// Assume we have a lastEmail object from our system
const lastEmail = {
  message_type: MessageType.INCOMING,
  content_attributes: {
    email: {
      from: ['[email protected]'],
      to: ['[email protected]'],
      cc: ['[email protected]'],
      bcc: ['[email protected]'],
    },
  },
};

// Conversation contact and system emails
const conversationContact = '[email protected]';
const inboxEmail = '[email protected]';
const forwardToEmail = '[email protected]';

const { to, cc, bcc } = getRecipients(
  lastEmail,
  conversationContact,
  inboxEmail,
  forwardToEmail
);

console.log('To:', to);
console.log('CC:', cc);
console.log('BCC:', bcc);

Copy link

github-actions bot commented Feb 6, 2025

size-limit report 📦

Path Size
dist/utils.cjs.production.min.js 6.69 KB (+6.01% 🔺)
dist/utils.esm.js 6.13 KB (+6.81% 🔺)

@scmmishra scmmishra merged commit 0bd2242 into main Feb 6, 2025
4 checks passed
scmmishra added a commit to chatwoot/chatwoot that referenced this pull request Feb 11, 2025
This PR target two issues

### CC & BCC not updated correctly

When moving from one conversation to another, the store may not have the
list of all the messages. A fetch is subsequently made to get the
messages. However, this update does not trigger the `currentChat`
watcher. This PR fixes it by adding a new watcher on
`currentChat.messages`.

We also update the `setCCAndToEmailsFromLastChat` method to reset the
`cc`, `bcc` and `to` fields if the last email is not found. This ensures
that the data is not carried forward from a previous email

Fixes: #10477

### To address are not added correctly to the `CC`

If the `to` address of a previous email has multiple recipient, there
was no case to add them to the CC.

Fixes: #8925

---

Depends on: chatwoot/utils#41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants