I think I’m making progress, but still no emails via SendGrid. Can someone check my code and see if I’m missing something? The first two code snippets are the back end parts, the final is attached to the form itself. Thank you!
//sendGrid.js
import {fetch} from ‘wix-fetch’;
export function sendWithService(key, sender, recipient, subject, body) {
const url = “https://api.sendgrid.com/api/mail.send.json”;
const headers = {
“Authorization”: "Bearer " + key,
“Content-Type”: “application/x-www-form-urlencoded”
};
const data = from=${sender}&to=${recipient}&subject=${subject}&text=${body}
;
const request = {
“method”: “post”,
“headers”: headers,
“body”: data
};
return fetch(url, request)
.then(response => response.json());
}
// Filename: backend/email.jsw (web modules need to have a .jsw extension)
import {sendWithService} from ‘backend/sendGrid’;
export function sendEmail(subject, body) {
const key = “I INSERTED MY SENDGRID API KEY HERE”;
const sender = “from.librarian@anamosa.lib.ia.us”;
const recipient = “to.swendl@anamosa.lib.ia.us”;
return sendWithService(key, sender, recipient, subject, body);
}
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import {sendEmail} from ‘backend/email’;
$w.onReady(function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});
function sendFormData() {
const subject = New Volunteer Application from ${$w("First & Last Name").value}
;
const body = Name: ${$w("#First & Last Name").value}
;
sendEmail(subject, body)
.then(response => console.log(response));
}