MailChimp instead of SendGrid

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):

//email.jsw

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

//sendMailChimp.js

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

2 Likes

I’m having this exact issue myself.

1 Like

Hi Marcel!
In order to adjust the code to Mailchimp you should work closely with the mail chimp API.
Maybe this link can help:

Example of differences I noticed that require code changes:
mailchimp Authorization is “Basic” - so should look something like this :

'Authorization': 'Basic ' + 'anyuser:YOUR_API_KEY'

mailchimp API expect

'content-type': 'application/json'

Since these are two different providers each with its own API there can be many other differences…
Be sure to go through the mailchimp API and also you might want to use console.log to check what you get back from mailchimp as this can tell you what is expected by the API and not being sent.

Hope this will help.
Good luck!

Hi Omer,

Thanks for your advice. Unfortunately I am yet not skilled enough to figure this one out. I find it a bit disappointing that WIX is not providing an example for the all the main mail service providers as there seem to be quite some demand for it. But it is what it is. For now signed up at sendGrid and it works. Hopefully in time there will be multiple solutions in the Forum or provided by WIX.

Guys, if you want to make your life endlessly easier, have a look at emailjs.com. I used it to send emails using gmail. Full doc here: https://girizano.wixsite.com/codecorner/home/send-email-thru-gmail-and-others-in-wix-code