I need a help with this:
I want to send an Email that was set by a form. The code basically copy from Nayeli. Preview this code it works most of the time, but after Publish it do not work.
I set collection permission to Anyone can submit data to this collection. This is the code:
nayeliEmail.jsw
import {sendWithService} from ‘backend/nayeliSendGrid’;
export function sendEmail(subject, body) {
const key = “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”;
const sender = “laizhihog88@gmail.com”;
const recipient = “laizhihong88@outlook.com”;
return sendWithService(key, sender, recipient, subject, body);
}
export function sendEmailWithRecipient(subject, body, recipient) {
const key = “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”;
const sender = “laizhihong88@gmail.com”;
return sendWithService(key, sender, recipient, subject, body);
}
nayeliSendGrid.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());
}
pagecode
import {sendEmail, sendEmailWithRecipient} from ‘backend/nayeliEmail’;
$w.onReady( function () {
$w(“#dataset1”).onAfterSave(sendFormData);
});
function sendFormData() {
const subject = ${$w("#input4").value}
;
const body = tutorEmail: ${$w("#input1").value} \r studentEmail: ${$w("#input2").value} \r Student Name: ${$w("#input3").value} \r Subject: ${$w("#input4").value}
;
const recipient = $w(“#input1”).value;
//const sender = $w(“#input2”).value;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));
sendEmail(subject, body )
.then(response => console.log(response));
}