Hi,
I have created a form that is linked to a database. Now I want to receive an email every time someone submits a form. I found detailed coding on how to do this with SendGrid, but I am using MailChimp. Tried to change the relevant items, but does not get it to work. Anyone got a working example? This is what I have so far (changed from the example in code tutorials):
import {sendWithService} from ‘backend/sendMailChimp’;
export function sendEmail(subject, body) {
const key = “anystring:xxxxa568e4ad6dbbabd714c4cd4ad8a5-us17”;
const sender = “from.mxxxxxp@hotmail.com”;
const recipient = “to.mxxxxxp@hotmail.com”;
return sendWithService(key, sender, recipient, subject, body);
}
export function sendEmailWithRecipient(subject, body, recipient) {
const key = “anystring:bbc7a568e4ad6dbbabd714c4cd4ad8a5-us17”;
const sender = “from.mxxxxxp@hotmail.com”;
return sendWithService(key, sender, recipient, subject, body);
}
import {fetch} from ‘wix-fetch’;
export function sendWithService(key, sender, recipient, subject, body) {
const url = “https://us17.api.mailchimp.com/3.0”;
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());
}
$w.onReady(function () {
//TODO: write your page related code here…
$w(“#dataset1”).onAfterSave(sendFormData);
});
function sendFormData() {
const subject = New Submission from ${$w("#nameInput").value}
;
const body = Name: ${$w("#nameInput").value} \rEmail: ${$w("#emailInput").value}
;
const recipient = $w(“#emailInput”).value;
const recipient = “mcschaap@hotmail.com”;
sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));
}
I assume the sendMailChimp.js needs to be different, question is though, what needs to be different? And the const Key in the email.jsw?
Thank you in advance for any help.
Kind regards,
Marcel