How do I have multiple recipients using SendGrid?

Hi there,

I am struggling a bit in understanding why the follow codes do not work. After reading about personalisation of SendGrid api and reading the v3 blog post on https://www.wix.com/corvid/forum/community-discussion/sending-template-emails-with-sendgrid-v3 , I updated some of the codes I’ve been using for the following:

Backend: sendGrid.js

export function sendWithService(key, sender, recipient, cc, subject, body) {
const url = “https://api.sendgrid.com/v3/mail/send”;

const MyHeaders = {
“Authorization”: "Bearer " + key,
“Content-Type”: “application/json”
};
const MyBody = {
“personalizations”: [{
“to”: recipient,
“from”: {
“email”: sender
},
“subject”: subject,
“content”: [{
“type”: “text/html”,
“value”: body
}]
};

return fetch(url, {
“method”: “POST”,
“headers”: MyHeaders,
“body”: JSON.stringify(MyBody)
})
.then(Response => Response.text);
}

Backend: email.jsw

export function sendEmailToPeople(sender, recipient, cc, subject, body) {
const key = “xxxxxx(mykey)”;
return sendWithService(key, sender, recipient, cc, subject, body);
}

Front end:

$w(‘#send’).onClick(()=>{
let recipient = [{
email":"example1@gmail.com
},
{
email":"example2@gmail.com
}]
let sender = “example@example.co”;
let cc = “”;
let subject = “Hey it’s a test”
let body = “Hey it’s a test to see if I can send email to multiple recipents”
sendEmailToPeople(sender,recipient,cc,subject,body)
.then(response=>{
console.log(response)
})
. catch (error=>{
console.log(error)
})

})

The response I got was undefined and no emails was sent. I have been tweaking different parts of the codes but I don’t know why it’s not working. Would anyone please suggest what to do? Thanks!

Take a look at SendGrid’s own support pages.
https://sendgrid.com/docs/for-developers/sending-email/personalizations/
https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/single-email-multiple-recipients.md

I do also strongly suggest that you read the original tutorial page first and understand that first.
https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission

Before you go looking at changing the code to apply anything else like Tiann has done in his post.
https://www.wix.com/corvid/forum/community-discussion/sending-template-emails-with-sendgrid-v3

finally, have a read of these if you haven’t already done so.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/example-send-email-with-the-sendgrid-npm-interface
https://www.wix.com/corvid/forum/community-discussion/error-when-sending-template-emails-with-sendgrid-v3
https://www.vorbly.com/Vorbly-Code/WIX-SENDGRID-API-V3—AUTO-EMAIL-MESSAGES
https://www.wix.com/corvid/forum/community-discussion/sendgrid-emails-not-going-to-multiple-recipients
https://www.wix.com/corvid/forum/community-discussion/emailing-to-one-recipient-and-multiple-cc-via-sendgrid

Hey Thanks for all the resources! I’ve had SendGrid API setup before and have been having no problem using it. Just I can’t quite get V3 working for me.

For example, the the posts about sending template emails, when I simply copied and pasted what’s posted, I got error message about this bit of the code


ModuleLoadError: Error loading web module backend/email.jsw: backend/sendGrid.js: Unexpected token, expected ; (32:53)

If you can shed some light, that would be great!