Emailing to One Recipient and Multiple CC via SendGrid

HI Team,

SendGrid supports the capability to send the single email to one or more recipients in the TO field plus one or more recipients in the CC or BCC fields (reference here: Personalizations | Twilio). Does the WIX SendGrid function support this capability?

Using the code below I have been able to send the single email to the submitter of the form (in the TO field) plus one person in the CC field, but not two addresses CC’d as is my requirement.

Any guidance here very much appreciated!

=========================================
Form Event Handler

import { sendEmail, sendEmailWithRecipient } from ‘backend/email’;

$w.onReady( function () {
$w(“#NominationTestdataset”).onAfterSave(sendFormData);
});

function sendFormData() {
const subject = New Regionals Nomination;
const body = Hi ${$w("#firstnameInput").value}, \r \rThis email confirms your nomination **for** the tournament. \r \rFirst Name: ${$w("#firstnameInput").value} \rSurname: ${$w("#surnameInput").value} \rEmail: ${$w("#emailInput").value} \r \rRegards, \rThe Committee;
const recipient = $w(“#emailInput”).value;
const nomcc = “email1@outlook.com;email2@outlook.com”;

sendEmailWithRecipient(subject, body, recipient, nomcc) 
    .then(response => console.log(response)); 

}

=========================================

//email.jsw

import { sendWithService } from ‘backend/sendGrid’;

export function sendEmail(subject, body) {
const key = “nnnnnnnnnnnMyAPIKeynnnnnnnnnnnnnnnnnnnnn”;
const sender = “from.email@domain.com”;
const recipient = “to.email@domain.com”;
return sendWithService(key, sender, recipient, subject, body);
}

export function sendEmailWithRecipient(subject, body, recipient, nomcc) {
const key = “nnnnnnnnnnnMyAPIKeynnnnnnnnnnnnnnnnnnnnn”;
const sender = “TheSender@outlook.com”;
return sendWithService(key, sender, recipient, nomcc, subject, body);
}

=========================================

//sendGrid.js

import { fetch } from ‘wix-fetch’;

export function sendWithService(key, sender, recipient, nomcc, subject, body) {
const url = “https://api.sendgrid.com/api/mail.send.json”;

const headers = {
“Authorization”: "Bearer " + key,
“Content-Type”: “application/x-www-form-urlencoded”
};

const data = from =${sender}&to=${recipient}&cc=${nomcc}&subject=${subject}&text=${body};

const request = {
“method”: “post”,
“headers”: headers,
“body”: data
};

return fetch(url, request)
.then(response => response.json());
}

@pjvcc

I’ve also struggled with this, but the solution is actually quite simple…

Simply create another person to CC to on your page code, call it something like CC2, parse it through the page code all the way to the sendgrid file, then simply add another cc to the list like so:

`from=${sender}&to=${recipient}&cc=${nomcc}&cc=${CC2}&subject=${subject}&text=${body}`;

Best of luck!
Tiaan

1 Like

Tiaan, thank you very much - just what I needed!

I was trying the PERSONALIZATIONS function in the V3 of the API but could not get it to work. With the solution you suggested, did you get that by trial and error or is the API documented somewhere? It would be great if it was documented - save a lot of frustration! :slight_smile:

Cheers,

Jeremy

Hi Jeremy

Glad I could help!

It is probably documented somewhere amongst the millions of pages of documentation they’ve got, but my frustration searching through it left me to simply figure it out.

Cheers,
Tiaan

1 Like

Well thank you Tiaan, you’ve saved me from (even more) frustration.

Wix team, can we get this available in a document please! :smile:

Cheers,

Jeremy