Hello everyone,
Im trying to do a send email function... I did everythig what says in this page.
But when I run the code it gives me an error in the console that says:
'Uncaught (in promise) Error: invalid json response body at https://api.emailjs.com/api/v1.0/email/send reason: Unexpected token O in JSON at position 0'
Can someone please help me?
Yes, that gmail is just a handle that you can pick yourself. I will stress that in the article, it was probably not clear enough. Glad you got it working.
I checked that. But now I went to my EmailJS dashboard and Email History and I found loads of errors, the provider 'gmail' was wrong. I changed it in the code to the right one and tested it. It worked! But still no "Email sent successful" message in the console. But it works and I'm happy and thank so much ;)
console.log´s do work in functions. There is one in the code, the "Email sent succesfully". If you don´t even get your own console.log, then probably the onClick refers to a a non-existing piece of code. Check the button properties and in the Properties Window, look at what is filled out behind the onClick. What is says there must be the function name in your code (for the button).
I checked everything and it stills doesn't work. I tried also writing console.log("hello") just to test, and it didn't execute it. So maybe it's not executing the function or console.log doesn't work in functions.
I just made a new project and copied everything fresh from the blog. Changed the email addresses and key, clicked the button and everything worked, all emails arrived. So it does work. Only thing I can think of now that your mapping is wrong in the template. If you use my example vars:
{
"from_email": "foo1@bar.com",
"to_email": "foo2@bar.com",
"replyto_email": "foo1@bar.com",
"cc_email": "foo3@bar.com",
"Arrival": "25-03-2018",
"Departure": "28-03-2018",
"Remarks": "Could we have coffee instead of tea for breakfast?",
"from_Name": "Giri´s Code Corner"
};
then you must also define all these vars in the template, between double curly braces, exactly as in the screen shot of the example template.
Well... I just tried your new code. But nothing happens when I run it. No error, no nothing.
If you mean that the email is sent to the wrong address, than you better check the template and the calling code where you fill all the vars with the email addresses.
Found it! See the corrected version in blog https://girizano.wixsite.com/codecorner/home/send-email-thru-gmail-and-others-in-wix-code (scroll down to bottom). Thanks for bringing it up.
Yeah, I get an Email but it's not me that should receive the Email but the other person that I wrote the Email in the code.
I checked my version, and I get exactly the same error, but .... the emails are sent anyway. Is that happening at your end too?
Oh, thank you! Here's my code:
// Filename: backend/email.jsw (web modules need to have a .jsw extension) import {fetch} from 'wix-fetch'; export function sendEmail(emailprovider, template, params) { const userid = "user_suN7ugVaGgdU2UShTyamD"; //put your userid here, see Account const url = "https://api.emailjs.com/api/v1.0/email/send"; // if no email provider specified, then use this one if (!emailprovider) { emailprovider="gmail"; } // if no email template specified, then use this one if (!template) { template="inquiry_form"; } //params is not filled in calling example. You should do that in code and then pass the parameter. //Remember to rename loc_params (below) then to params and further down, in request, also let loc_params = { "user_id": userid, "service_id": emailprovider, "template_id": template, "template_params": { "from_email": "******@gmail.com", "to_email": "******@*******.com", } }; const headers = { "Content-type": "application/json" }; const request = { method: 'POST', headers: headers, body: JSON.stringify(loc_params) }; return fetch(url, request) .then(response => response.json()); }
A page from my WebSite:
import {sendEmail} from 'backend/email'; $w.onReady(function () { }); export function button21_click(event, $w) { // sendEmail is called with: email provider name (like:gmail), template id (like "thanks_for_interest") and params. // params holds ALL variables for your email: sender name/email, recipients, cc´s, bcc´s, subject, body and more specific // values to fill the template, like, in our example, Arrival, Departure and Remarks. // Here, we leave the first two blank. sendEmail will substitute default values if so. Params MUST be filled, but in example // we do it in sendMail, so that´s why we just use foo here. sendEmail ("pptgames_crate", "test_tamplate"); }
I wrote that page, so I feel a more than moral obligation to help you. Could you show me your code?