Hi! I’m trying to get my submission form in WIX to work with SendGrid but I haven’t figured out how. Basically, when someone submits a form, I need to send 2 emails; one for me, one for the user. This is the code I’ve done for now.
import {sendWithService} from ‘backend/sendGrid’;
export function sendEmail(subject, body) {
const key = “SG.KEY”;
const sender = “myemail”;
const recipient = “myemail”;
return sendWithService(key, sender, recipient, subject, body);
}
export function sendEmailWithRecipient(subject, body, recipient) {
const key = “SG.KEY”;
const sender = “myemail”;
return sendWithService(key, sender, recipient, subject, body);
}
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());
}
//PageCode
import {sendEmail, sendEmailWithRecipient} from ‘backend/email’;
$w.onReady( function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});
function sendFormData() {
const subject = ‘Submission successful’
const body = \rFullName: ${$w("#fullname").value} \rEmail: ${$w("#email").value} \rPhone: ${$w("#phone").value} \rNumber: ${$w("#number").value} \rLocation: ${$w("#location").value}
;
const recipient = $w(“#email”).value;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));
sendEmail(subject, body)
.then(response => console.log(response));
}
I’d appreciate any assistance! Thank you in advance!