Liran, Tal, Uval, Yoav, Sam, Enybody!!!

Sorry for summoning all WIX Gurus at once, I know you are very busy answering hundreds of questions in here. But guys, I wouldn’t bother you if I didn’t need you.

Here is my situation:
I have your SendGrid sample up and running, but because of SendGrid have problems processing large HTML files with their API Ver.2 (which is the one used by the WIX sample), SendGrid helpdesk suggested me to migrate to their API Ver.3. So I did.

As they promised, the new API actually can handle large and complex HTML files. I have everything running smoothly… Well, not quite yet.

Here is what I have accomplished so far (please see my function below): it uses the SendGrid API Ver.3, and sends my emails successfully. I actually receive the email instantaneously and fully HTML formatted. Problems is, I never receive any “ok” response from the SendGrid servers; they keep me on waiting for a response permanently, so my code gets stuck on the Promise().

SendGrid Helpdesk says that this is impossible, they do send me an answer but somehow I’m not catching it. To get things weirder, when I send a bad request on purpose, I get an error response from SendGrid. So. I’m catching the bad ones but not the good ones? Hum!

If I send a bad request on purpose, I receive a response immediately, but when I send a good request, I receive no response at all, not even a null or empty string.

Fact is, SendGrid is processing my request successfully because I get the email in my inbox, but I never get any response. So I’m guessing the problem must be in the FETCH Function or in my code… Ok, OK, most likely in my code.

Could you please take a look at my function and let me know if you see anything out of place?

Sorry again for summoning you all :slight_smile:
Thank you very much,
-Luigi

HERE IS MY CODE:

export function SendGridFetch2 (APIKey, Sender, Recipient, Subject, HTMLBody)
{
const MyURL = “https://api.sendgrid.com/v3/mail/send”;
const MyHeaders = {“Content-Type”:“application/json”, “Authorization”:"Bearer " + APIKey};
const MyBody = {“personalizations”:[{“to”: [{“email”:Recipient, “name”:“XXXX”}]}], “from”:{“email”:Sender, “name”:“YYYYY”}, “subject”:Subject, “content”:[{“type”:“text/plain”,“value”:“Your email client does not support HTML”},{“type”:“text/html”,“value”:HTMLBody}]};

return fetch(MyURL, {“method”:“POST”, “headers”:MyHeaders, “body”:JSON.stringify(MyBody)}).then(Response => Response.json());
}

Hi Luigino,

Can you share the frontend code, the one that calls this function?

Liran.

Hi Liran!
Thanks so much for answering my post!.. Absolutely, please see below:

=====================================================
FRONTEND

import {SendEmail} from ‘backend/Email’;
SendEmail(SenderEmail, SenderName, RecipientEmail, RecipientName, Subject, HTMLBody).then(Response =>
{
console.log (“For some reason, this is never executed when the email is sent successfully”);
});

=====================================================
BACKEND (Email.JSW)

import {SendGridFetch2} from ‘backend/SendGrid’;
export function SendEmail(SenderEmail, SenderName, RecipientEmail, RecipientName, Subject, HTMLBody)
{
const APIKey = “xxxxxxxxxxxxxxxxxxxxxxx”;
return SendGridFetch2(APIKey, SenderEmail, SenderName, RecipientEmail, RecipientName, Subject, HTMLBody);
}

=====================================================
BACKEND (SendGrid.JS) Here goes the code again, I tweaked it a bit

import {fetch} from ‘wix-fetch’;
export function SendGridFetch2(APIKey, SenderEmail, SenderName, RecipientEmail, RecipientName, Subject, HTMLBody)
{
const MyURL = “https://api.sendgrid.com/v3/mail/send”;
const MyHeaders = {“Content-Type”:“application/json”, “Authorization”:"Bearer " + APIKey};
const MyBody = {“personalizations”:[{“to”: [{“email”:RecipientEmail, “name”:RecipientName}]}],
“from”:{“email”:SenderEmail, “name”:SenderName},
“subject”:Subject,
“content”:[{“type”:“text/plain”,“value”:“The content of this email cannot be displayed because HTML in not enabled”},{“type”:“text/html”,“value”:HTMLBody}]};

return fetch(MyURL, {“method”:“post”, “headers”:MyHeaders, “body”:JSON.stringify(MyBody)}).then(Response => Response.json());
}

Liran,
I’m still puzzled about why I receive an instant response from SendGrid when I send a bad request on purpose. But when I send a good request, even though my email is successfully sent and received, I never receive any response, so the “.THEN” in the FRONTEND Promise() is never executed (only on bad requests… go figure!). I checked the email status in my SendGrid Dashboard, and in fact it’s being received, processed and delivered successfully.

NOTE: The structure in MyURL, MyHeaders, MyBody and in the Fetch Call was verified by SendGrid helpdesk, the syntax is correct. So what is happening!?

I hope you can shed me some light here my friend because at this point I’m clueless.

Best Regards
-Luigi

Might not be a Wix Code issue, since you do get a bad request response.
But, it does interest me, so I’d like to have a look. Please share the link to your site.

Liran.

Sure,
https://www.itcgolive.com/coupons

Just click on the first item on the list and then click RE-SEND, that button triggers the whole process.
The linked function to the re-send button is “CouponSend_Click()”

Thank you very much,
-Luigi

Hi Luigi,

So I dug deep into this one…
Seems like the response from Sendgrid has a body that cannot be interpreted JSON, that why calling Response.json() fails.

You can use Response.text() instead.
When I try this with correct credentials, I get an empty text.
When I try it with bad ones, I get an error:

"{"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]}"

Can that work for you?
All you need to do is just change to this:

return fetch(MyURL, {"method":"post", "headers":MyHeaders, "body":JSON.stringify(MyBody)}).then(Response => Response.text

And deal accordingly after you get the response.

Hope this will be enough for you.

Liran.

Hello Liran,
Absolutely! that will definitely works. It looks like an empty response is my “success” status, and since a bad request always returns something, I’m more than glad with it.

Thank you so very much for looking into this, I knew you guys would figure this out. As far as my experience in all of these years working with WIX, I have always received an incredible support from you, I have nothing to say but good things about you.

You guys did an amazing job by releasing Wix Code, that’s the future right there. A platform that combines both worlds (Programmers and Designers) is definitely the next standard in website developing, way to go guys! I have an idea where you guys are heading with all of this, and the future looks exciting.

Thank you once again for all of your support,
I can go on now and finish this project thanks to you.

Liran, receive my best regards from California,
-Luigi

I am really happy to hear that!
Also, I must say that your code and site looks really good, and I admire you’re will to learn and fully understand Wix Code and Javascript.

Liran.

Hi Liran,
Just for future reference, I just received this response from SendGrid Help Desk:

If there is an error in your request, we return a JSON Non-2XX response “IN THE BODY”.
If you request is accepted, we return a JSON 202 response “IN THE HEADERS, NOT IN THE BODY”
202 Means: “Your message is both valid, and queued to be delivered”

So, that’s why we get an empty string.
But, why they would even do that? that’s an inconsistency.
It’s like bullets coming from different angles.

Anyways,
Thanks for your help and kind words, I hope this thread helps other WIX coders.
-Luigi