I want simply to send email with wix code. Does the only way is through call of an smtp webservice or is there a more simple way to do it ?
Thanks,
Jef
I want simply to send email with wix code. Does the only way is through call of an smtp webservice or is there a more simple way to do it ?
Thanks,
Jef
Hi Jef,
we currently recommend you use either smtp server or saas provider like mailgun, sendgrid etc.
you can search the documentation for examples, and also a few previous related posts in this forum.
we are currently working on a codeless version of email provider integration and will share once it will be available
Shlomi
Hi Shlomi,
Many thanks for your quick answer.
In fact I have do a backend module:
import {sendWithService} from ‘backend/sendGrid’;
export function sendEmail(subject, body) {
const key = MYKEY;
const sender = MYEMAIL;
const recipient = MYRECIPIENT;
return sendWithService(key, sender, recipient, subject, body);
}
And the sendGrid.js like this:
import {fetch}from ‘wix-fetch’;
export function sendWithService(key, sender, recipient, subject, body) {
const url = “https://api.sendgrid.com/v3/mail/send”;
var params = {“personalizations”: [{“to”: [{“email”: recipient}]}],“from”: {“email”: sender},“subject”: “Sending with SendGrid is Fun”,“content”: [{“type”: “text/plain”, “value”: “and easy to do anywhere, even with cURL”}]};
const headers = {
“Authorization”: "Bearer " + key,
“Content-Type”: “application/json”
};
const request = {
“method”: “post”,
“headers”: headers,
“body”: params
};
return fetch(url, request)
//.then(response => response.json());
.then(response => console.info(response.json()));
}
But the json response is:
Promise { _c: , _a: undefined, _s: 0, _d: false, _v: undefined, _h: 0, _n: false }
I don’t understand what is the problem…?
Any idea ?
Thanks,
Jef
Hi Jef,
two great tools to keep in your arsenal are:
good luck!
Shlomi
Hello, I have a solution for the problem above.
The problem was that the response SendGrid send back has some strange json string. The string shows: Body {something}. It is missing a “colon” Body: {…}. Therefore response.json() will crash. Also, JSON.parse(response) will also crash because it is not a formatted json string.
My solution was to read each property individually inside the response. And then put them in your own object before returning it.
Here is what I did:
return fetch(url, request)
.then(response => {
const result = {
isOk: response.ok,
status: response.status,
statusText: response.statusText
};
return result;
});
Don’t know if you guys still need this. But I thought I just put this here in case someone else has the same problem. I don’t want anyone spending 2 hours like me solving this stupid problem. Hope that helps the WIX code community.
Keawe
Hi Keawe,
thanks for sharing, i am sure future users of this forum will find it helpful!
Shlomi
I use EmailJS.com project and it just saves my life. They have a free plan, and it’s secure, you doesn’t put your secret key or private email for anyone. They have the example for wix code too, I very like it.
Hi guys,
Since this thread began, Wix now also support sending emails using stunning templates. Check it out here - https://www.wix.com/code/home/forum/product-updates/triggered-emails
Shlomi
can we send the mail from the user who is logges in
I am sure that no service will give you the opportunity to “impersonate” another user. You have to send emails on behalf of yourself (company) and use the “Reply To” field to return emails to your “logges in” user.