Sending multiple emails with SendGrid

Hi All

I’m stuck … any suggestions would be greatly appreciated.

I’m trying to send an email to a list of addresses using SendGrid after performing a database update. My problem is how to call the emailing function, repeatedly, only on successfully saving to the database.

This page should update a table record and then inform on those changes via email.

import {sendEmail} from 'backend/email';
import wixData from 'wix-data';

$w.onReady(function () {
    wixData.query("players")
    .limit(1000)
    .distinct("email")
    .then((results) => {
    if (results.items.length > 0) {
        let items = results.items;
        let totalCount = results.totalCount;
        console.log("*** The total count is ", totalCount);
        for (let i = 0; i < totalCount; i++) {
            console.log("*** The ", i, "item is ", items[i]);
        }
        } else {
            // handle case where no matching items found
            console.log("*** There were no emails returned.")
        }
    })
        .catch((error) => {
        let errorMsg = error.message;
        let code = error.code;
    });

    const thisRecipient = "jc@mediaspark.com"
    $w("#dynamicDataset").onAfterSave(sendFormData);

    function sendFormData() {
        const recipient = thisRecipient;
        const subject = `theSimon - Game #${$w('#textGameNumber').text}`;
        const body = `${$w('#textDivision').text}
        \r ${$w('#textTeam1').text} : ${$w("#input1").value}
        \r ${$w('#textTeam2').text} : ${$w("#input2").value}`;
        sendEmail(subject, body, recipient)
        .then(response => console.log(response));
    }
});

If you search the forum you will find previous forum posts that could help like this one, where you can send the email to yourself and have all the other email addresses setup as bcc (blind carbon copy) so that only their own email and your email is showing.

If you just use cc (carbon copy), then all the email recipients will be able to see each others email address, which is not a suitable option if you are just doing something like a shout out for example.

https://www.wix.com/corvid/forum/community-discussion/emailing-to-one-recipient-and-multiple-cc-via-sendgrid

Other previous posts.
https://www.wix.com/corvid/forum/community-discussion/wix-code-sending-form-submission-to-three-recipients
https://www.wix.com/corvid/forum/community-discussion/sendgrid-emails-not-going-to-multiple-recipients

Thanks GOS - my initial research on sendgrid didn’t return these articles - problem solved!