I am also struggling with sendgrid v3 so it would be nice to see this reviewed. ( had it working in V2 but I decided to use a dynamic template and V3 Sendgrid. 1) I have a bug somewhere - how to better instrument my code to get the most out of debugging would be nice as sendgrid doesn’t work in preview mode and getting site events to be informative would be helpful ( Try/Catch/logging).
Extra points for 2) appropriate method for ensuring “primary keys” are unique in collection and 3) Should I be leveraging BeforeInsert hook to enforce uniqueness for primary? field and cross field validation ( belt and suspenders both at BeforeInsert and on the page) ? Appropriate method messaging back to user on page?
I’ll be working through this over the weekend.
Page Code
$w.onReady(function () {
});
export function PartnershipTypeinpt_change(event) {
let isChecked = false;
$w('#PartnershipTypeinpt').value.forEach((ChkboxValue) => {
if (ChkboxValue === "OEM"){
isChecked = true;
}
})
if (isChecked === true){
$w('#OEMSddwn').show();
} else {
$w('#OEMSddwn').hide();
}
}
SendGrid Dynamic Template
Collection Partner Request Schema
data.js
// data.js
import {sendEmail} from 'backend/email';
import wixData from 'wix-data';
function emailPartnerRequest(item) {
const subject = `WIX Partner Request - ${item.firstName} ${item.lastName} ${item.companyName}`;
const recipient = item.email
const body = `This is a test`
sendEmail(subject, recipient, body)
.then(response => console.log(response));
}
export function PartnerRequest_afterInsert(item, context) {
emailPartnerRequest(item);
}
sendemail.jsw
// sendemail.jsw
// SendGrid V3
import {sendInstruction} from 'backend/sendgrid';
export function sendEmail(subject, body, recipient) {
const key = "obfuscate.mQVO5w7gv0tm7snJBp7oMHdjibbeishSTw";
const sender = "somvalidemail@here.ca";
//return sendInstruction(key, sender, recipient, subject, body);
}
sendgrid.js
//sendgrid.js
// SendGrid V3
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",
"value": "this is a test"
}],
"template_id": "d-obfuscate5a4a6e2bff69493f2"
};
return fetch(url, {
"method": "POST",
"headers": MyHeaders,
"body": JSON.stringify(MyBody)
})
.then(Response => console.log(Response.text));
}