Hi everyone !!!
I followed what @tiaan explained here
Here’s the code I used →
import { sendEmail } from 'backend/email';
import wixData from 'wix-data';
export function Videos_afterInsert(item, context) {
var Recipient = [];
const Subject = `${item.name}`;
const body = `name: ${item.name}
\rdescription: ${item.description}`;
wixData.query('Subscribers')
.isNotEmpty('email')
.find()
.then((res) => {
let items = res.items;
for (var i = 0; i < res.items.length; i++) {
var obj = {};
var results = items[i].email;
obj['key'] = results;
Recipient.push(obj);
}
sendEmail(Subject, body, Recipient)
.then(response => console.log(response));
return item;
});
}
import { fetch } from 'wix-fetch';
export function SendInstruction(APIKey, sender, Recipient, Subject, body) {
const url = "https://api.sendgrid.com/v3/mail/send";
const MyHeaders = {
"Authorization": "Bearer " + APIKey,
"Content-Type": "application/json"
};
const MyBody = {
"personalizations": [{
"to": [{
"email": 'xxxxx@xxx.com'
}],
"bcc": Recipient
}],
"from": {
"email": sender
},
"subject": Subject,
"content": [{
"type": "text/html",
"value": body
}],
"template_id": "xxxxxxxxxxx"
};
return fetch(url, {
"method": "POST",
"headers": MyHeaders,
"body": JSON.stringify(MyBody)
})
.then(Response => Response.text);
}
import { SendInstruction } from 'backend/sendGrid';
export function sendEmail(subject, body, Recipient, Subject) {
const APIKey = "xxxxxxx";
const sender = "xxxxxxx";
return SendInstruction(APIKey, sender, Recipient, Subject, body);
}
These are not working !!
Any idea ?
Thanks …