Can someone give me a hand? I’m trying to send an email from my site. I merged the examples provided by Wix and those of the third party email provider, but it doesn’t seem to work.
import {fetch}from ‘wix-fetch’;
$w.onReady(function () { sendEmail(); });
function sendEmail() {
const subject = “Test”;
const body = “Bla bla bla”;
const key = “h8MaswjhsdghJKLm0YPN4yg”;
const sender = "myemail@gmail.com ";
const recipient = "theiremail@gmail.com ";
const url = " https://api.sendinblue.com/v2.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
};
console.log(request);
return fetch(url, request)
.then(response => response.json())
.catch(response => response.json());
}
Hay Broadway,
You cannot send an email from the browser as browser cross domain security policy will block you. You will need to move the fetch code to a backend module and use web modules .
Hi Yoah,
I did what you suggested and it seems to work (I get a regular third party service server response). Unfortunately though, no mail is ever received. Any ideas?
Here is the code:
FRONT END:
import {sendEmail} from ‘backend/sendEmail.jsw’;
$w.onReady(function () {
sendEmail().then(product => {
console.log("OK: "+product);
})
.catch(error => {
console.log("ERROR: " +error);
});
});
BACK END (sendEmail.jsw):
import {fetch} from ‘wix-fetch’;
export function sendEmail () {
address = “destination@hotmail.com”;
subject = “TEST”;
body = “TEST”;
return fetch(“https://api.sendinblue.com/v2.0?APIKey=” + “h8MwjJKLmasdf9as723540YPasdfN4yg”, {
method: ‘post’,
body: JSON.stringify({address, subject, body})
}).then(function(response) {
if (response.status >= 200 && response.status < 300)
return response.text();
else
throw new Error(response.statusText);
});
}
I don’t know how to fix your problem, but I think you should update the code listed above, as you are exposing your API key to the whole forum, which is not a good idea from security point of view.
Hey there Broadway,
I don’t know the API for SendInBlue. Is there any way to determine if an email was sent? Is there some sort of verification?
Looking at your code I see a destination address, but I don’t see a from address. Is that supplied by the API? Or maybe that’s the cause of the problem?
You might need to work out API issues with the people at SendInBlue. Alternately, you can use SendGrid which is described in the tutorial How to Send an Email on Form Submission .
Wix Code also has two email systems available which might suit your needs: Triggered Emails and Automated Emails .
Yisrael
I need urgent help, I am trying to send SMS, I managed to make it excellent to tell one.
But I want to send it to all my contacts, now I built it badly, he flips from item to item until he finishes them all. I realized there was a possibility by using a post, get the whole column of cell phones. How do you do that.