Sending Email with sendGrid not work after Public

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));
}

I found the problem, but still with a problem that is CAN’T SEND Email to outlook.com

I found the problem: the sendGrid API Key got three months ago is too old. I change a new API Key, then the code basically worked well.

I changed the .jsw file in order to pass both sender and recipient to the function sendEmailWithSenderRecipient. Here is the code.

import {sendWithService} from ‘backend/sendGrid’;

export function sendEmailWithSenderRecipient(subject, body,sender, recipient ) {
const key =“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”;
return sendWithService(key, sender, recipient, subject, body);
}
}

I finished related change to the page code.

import {sendEmailWithSenderRecipient} from ‘backend/email1’

$w.onReady(function () {

$w(“#dataset1”).onAfterSave(sendFormData);

});

function sendFormData() {

const subject = Test;

const body = `Tutor Email: ${$w(“#input1”).value}

\rStudent Email : ${$w(“#input2”).value}

\rStudent Name: ${$w(“#input3”).value}

\rSubject: ${$w(“#input4”).value}`;

const recipient = $w(“#input1”).value;

const sender = $w(‘#input2’).value;

sendEmailWithSenderRecipient(subject, body, sender, recipient)

.then(response => console.log(response));

}

I ran the code with @gmail.com, @yahoo.com and @outlook.com. The code is working good except sending emails to outlook.com. The following is my result.

It is working as recipient is gmail.com and sender is gmail.com

It is working as recipient is yahoo.com and sender is not yahoo.com

It is not working as recipient is yahoo.com and sender is yahoo.com (maybe yahoo block that condition).

It is not working as recipient is outlook.com and sender is any.

Finally I still hope someone can tell me how to fix the problem:

can’t sending emails to outlook.com

I’ve used SendGrid and have no troubles with it going to any email.

I would have just changed the SendGrid API key in your original code from Nayeli (Code Queen), plus taking out the Nayeli in the jsw and js backend file names.

I use very similar setup to yourself and everything runs just fine.

All I did extra was to make sure that I had completed all of the three sender authentication options:
https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/
https://vimeo.com/267486083 - video showing you how to do it.