In my website, I am having a trouble in sending gmail through SendGrid
Here is my code ;
email.jsw
//email.jsw
import {sendWithService} from 'backend/sendGrid';
export function sendEmail(subject, body) {
const key = "xxxxxxxxxxxxxx";
const sender = "xxxxxxxxx";
const recipient = "to.email@domain.com";
return sendWithService(key, sender, recipient, subject, body);
}
export function sendEmailWithRecipient(subject, body, recipient) {
const key = "xxxxxxxxxxxxxxxxxx";
const sender = "xxxxxxxxxx";
return sendWithService(key, sender, recipient, subject, body);
}
sendGrid.js
//sendGrid.js
import {fetch} from 'wix-fetch';
export function SendInstruction(APIKey, Sender, RecipientEmail, 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": RecipientEmail
}]
}],
"from": {
"email": Sender
},
"subject": Subject,
"content": [{
"type": "text/html",
"value": body
}],
"template_id" : "xxxxxxxxxxxx"
};
return fetch(url, {
"method": "POST",
"headers": MyHeaders,
"body": JSON.stringify(MyBody)
})
.then(Response => Response.text);
}
Page code
import {sendEmail} from 'backend/email';
export function button6_click(event) {
if(food1 == 1){
wixData.query('Contact').ne('email', '').limit(1000).find()
.then(async(results) => {
if (results.length > 0) {
for (let i = 0; i < results.length; i++) {
let item = results.items[i];
let emailid = item.email;
function SendClientEmail() {
const Subject = `New Submission from ${"artt"}`;
const body = `Name: ${"body"}
\rname: ${"fodd"}
\ritem: ${"food"}`;
const RecipientEmail = `emailid`;
}
}
}
})
}
let toInsert = {
"name": $w('#input1').value,
"foodPhoto" : $w("#image1").src,
"description" : $w('#input2').value,
"all" : "all"
};
wixData.insert("FoodItems", toInsert)
.then( (results) => {
let item = results;
} )
.catch( (err) => {
let errorMsg = err;
} );
wixLocation.to(`/home`);
}
All help appreciated !
Ajith