Email String Contruction for Email

I am trying to configure a dynamic email address array fo SG mail.
There is a check in SG that does not allow duplicate emails.

Scenario:
To: email address will be dynamic “{contact.email}”,
I would like to have a static array of cc: email addresses.

arrayCCEmailAddress [email 1, email 2, email 3, etc.]

A portion of the CC: email will be dynamic resulting in.
CC: “{user.email}”, “ccEmail” (not sure how to do this)

The summary problem is that the {user.email} may also be in the static CC array which would in turn throw an error in SG mail.

so I am thinking of some sort of screen to filter it out.

> const arrayCCEmailAddress = ["email1"," email2", "email3"];
> let ccEmail = [];

for (let e = 0; e < arrayCCEmailAddress.length; e++) {
let address = arrayCCEmailAddress[e];
if (address == user.email) {
return;
}
else {
if (address == contact.email {
return;
}
}
else {
ccMail.push(address)
}  

Forgive my “Noobness” but is this even close?

Then in the email fields?

To: "{contact.email}",
cc: "{user.email}","ccEmail",

Thanks,
David

@jdavidpugh Assuming to will always be a single email address, I would do the following.
I would create one big concatenated array for all the potential CC email addresses, then I would make sure there were only unique values in that array and then finally I would remove the to address from it. This way you have a unique to address and an array of additionally unique CC addresses.

You can do this manually, but it would be easier with a library like lodash docs.

Something like this (this will require you to to add Lodash as an NPM package to your task)

const _ = require('lodash');

const alwaysCC = ["name1@example.com", "name2@example.com"];

let toAddress = contact.email;
let dynamicCC = [];
dynamicCC.push(user.email);
// push in any other dynamic CC emails into dynamicCC

// now add the always CC emails
let ccAddresses = dynamicCC.concat(alwaysCC);

// make CC uniq
ccAddresses = _.uniq(ccAddresses);

// pull out the TO address / it will do nothing if it's not there
_.pull(ccAddresses, toAddress);

// send the uniq emails to sendGrid
const email = {
   from: "",
   to: toAddress,
   cc: ccAddresses
   subject: "Test email",
   text: "Plain email text"
};

I hope this helps

@tielman I think my situation is different a bit. My problem lies in the fact that:

To: {contact.email} will always be a unique customer address.
cc: {user.email} will always email a copy for user personal reference. ccAddresses will contain mandatory management copies for internal office use.

The problem comes when one of the mandatory emails belongs to the active user generating the report.

Will this filter that out the duplicate?

 ccAddresses = _.uniq(ccAddresses)`

Thanks,

David

Yes, that is what _.uniq does, where _ = require('lodash')

You assume your contact email will always be unique, but you may as well be sure and just pull it out of the CC addresses to be safe, hence my suggestion.

Good luck

@tielman Thanks for the clarification…