For documentation refer to: Personalizations | SendGrid Docs | 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)
-
/************************************************************************************************
-
- Change the reference format of the file from wix:image to https://static.wixstatic.com/media/
-
*/
-
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
-
- NPM REQUIRED: AXIOS
-
*/
-
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