I see in this thread how you can send an individual email to 3 recipients using sendGrid with the forEach((recipient) command:
https://www.wix.com/corvid/forum/community-discussion/sendgrid-emails-not-going-to-multiple-recipients
However, I need to send the same email to 3 people. One to the email address entered by the user of the form, and the other 2 are fixed email addresses.
Anyone manage to do something similar with sendGrid in Corvid?
TIA!
Thanks, Whiskey! Great help! I am studying the links you provided to apply to my situation. I am a novice with Corvid, so will take me a while to digest and test. I’ll circle back to this thread with how it went and if there are any follow up questions.
Thanks again, @givemeawhisky ! With the post you linked, I was able to get the email working exactly the way I needed.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/sending-an-email-on-form-submission
I used the code that @Sam (Wix) posted to modify my sendGrid.js file with the following:
const recipients = recipient.split(“;”).map( (x) => “to=” + x + “&” ).join(“”);
const data = from=${sender}&${recipients}&subject=${subject}&text=${body}
;
But then I have a slightly different use case than @Santiago did. I need to send the email to 2 fixed email addresses and 1 user-entered email address. So here’s my syntax in case anyone needs it to combine everything together:
const recipient = “email1@gmail.com;email2@gmail.com;” + $w(“#adminemail”).value;
I’m posting this example for sending the same email with 2 or more jpg images attached to a list of recipients.
For documentation refer to: Personalizations | Twilio
/*****************************************************************************************
-
import { sendEmailWithFiles } from ‘backend/xxxxxx.jsw’
-
Send mail with attachments files (Sending the same email to multiple recipients)
-
Parameter: mailParam
-
mailParam.name_sendGridSecret = reference to the secret key
-
mailParam.mail_body = mail_body (type: 'text/html')
-
ie. '<p><b>Good morning</b></p><br>';
-
mailParam.personalize = array example [{ "to": arrayMailTO, "subject": mailParam.subject }]
-
where subject = subject of the message to be sent
-
where arrayMailTo = { "email": mailParam.To[index]
-
mailParam.attach = array attachment files to send
-
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
Example code to Send more mail (called from front-end)
-
/************************************************************************************************
-
-
*/
-
async function ConvertURLfile(imageSRC) {
-
let strReturnImage = “”;
-
if (imageSRC.startsWith(“wix:image:”)) {
-
let wixImageURL = "";
-
wixImageURL = "https://static.wixstatic.com/media/";
-
let wixLocalURL = "";
-
wixLocalURL = imageSRC.replace('wix:image://v1/', '');
-
wixLocalURL = wixLocalURL.substr(0, wixLocalURL.lastIndexOf('/'));
-
strReturnImage = wixImageURL + wixLocalURL;
-
} else {
-
strReturnImage = imageSRC;
-
}
-
return strReturnImage;
-
}
-
/*****************************************************************************************
-
- Function that, given the URL of the file, returns a buffer ready to be written/sent
-
-
*/
-
export async function returnBufferEncoded(urlScr) {
-
const axios = require(“axios”);
-
let urlToSend = await ConvertURLfile(urlScr);
-
const responseArrayBuf = await axios.get(urlToSend, { responseType: ‘arraybuffer’ });
-
return await responseArrayBuf.data.toString(‘base64’);
-
}
-
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
// sequence url files (jpg) to send
-
for (let index = 0; index < arrayAttach.length; index++) {
-
let bufferEncodedBuf = await returnBufferEncoded(arrayAttach[index].urlScr);
-
let struct_attachments = {
-
content: bufferEncodedBuf,
-
filename: arrayAttach[index].filename,
-
type: 'text/plain',
-
disposition: 'attachment'
-
};
-
arrayAttachments.push(struct_attachments);
-
}
-
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
let mailParam = {
-
"name_sendGridSecret": "name of secret sendGrid",
-
"mail_body": mail_body,
-
"personalize": arrayPersonalizations,
-
"attach": arrayAttachments
-
};
-
const emailResult = await sendEmailWithFiles(mailParam);
-
if (emailResult[0].statusCode === SUCCESS_CODE) {…it’s ok…}
*/
export async function sendEmailWithFiles(mailParam) {
const sendGridSecret = JSON.parse(await wixSecretsBackend.getSecret(mailParam.name_sendGridSecret));
const senderEmail = sendGridSecret.senderEmail;
const client = require(‘@sendgrid/mail’);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
await client.setApiKey(sendGridSecret.key);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
const message = {
personalizations: mailParam.personalize,
from: { “email”: senderEmail },
content: [{
type: ‘text/html’,
value: mailParam.mail_body
}],
attachments: mailParam.attach,
isMultiple: true
};
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try {
return await client.send(message);
} catch (error) {
console.error('Error sending the email: ’ + error.message);
return false;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
it is OK,
Sendgrig documentation is quite intuitive even for someone like me who doesn’t have much experience.
see you soon and good work