SendGrid V3

Hi everyone !!!
I followed what @tiaan explained here
Here’s the code I used →

//data.js

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;
        });
}

//sendGrid.js

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);
}

//email.jsw

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 …

I haven’t gone over the entire code details, but anyway I think it’ll be easier to install @sendgrid/mail npm and work with it.

A few comment s:

  1. You’re missing a return here: return wixData.query(‘Subscribers’)

  2. You’re missing a return here: return sendEmail(Subject, body, Recipient)

Thanks @jonatandor35 !!
I will try what you described in your comments…
If it is not working, I will dig up about the @sendgrid/mail npm

Very thanks @jonatandor35
I used the @sendGrid/mail npm …
It was lot simple than that method

//data.js

export function Videos_afterInsert(item, context) {
    send();
 return item;
}

function send() {
 const recipient = "xxxx@xxxx.com"
 const subject = "Test";
 const sender = "xxxx@xxxx.com"
 return sendEmail(recipient, subject, sender)
}

//sendGrid.jsw

import sgMail from "@sendgrid/mail";

const apiKey = "xxxxxx";

export function sendEmail(recipient, subject, sender) {
    sgMail.setApiKey(apiKey);
 const msg = {
        to: recipient,
 from: sender,
        subject: subject,
        template_id: 'xxxxxxxxxx',
        dynamic_template_data: {
            subject : "test",
            name: "test",
            description: "test"
        },
    };

    sgMail.send(msg)
        .then((response) => {
            console.log(response);
        })
        .catch((err) => {
            console.log(err);
        });
}

Thank for your tip @J. D.

You’re welcome.
By the way, instead of having the API key hard-coded, you may consider storing it through the secret manager.
See:

Thanks!!!