[Solved] SendGrid - "&" Conflicts With Form Submission/Email

I have solved my issue by doing the following:

Instead of “rejecting” an input which contains an ampersand (&) I have used the code below to change any instance of “&” to “and” before a user submits the form.
Example below.

$w.onReady(function () {
 $w('#input2').onFocus((event)=>{
    let str = $w('#input1').value;
    $w('#input1').value = str.replace(/&/g,'and');  
  });
});

Form Title
[ input1 ] **will change any instance of “&” to “and” once user focuses on Input2)
[ input2 ]
[ Submit Button ]


Original

Hi guys,
I set up a few custom forms using input fields. When a user fills in a form a copy of the submission is emailed to both the user and I.

The issue:
When a user inputs an ampersand “&” the email triggered when they click submit is incomplete. (i.e. any info input after the ampersand is cut off.)

I see the full info logged in the dataset. So I know Wix has logged it correctly. It just doesn’t email correctly. I’m assuming & is the issue. Is there a work-around?

Form ex. https://www.otodatatankmonitors.com/call-back

Thank you for your help,
B

import {sendEmail, sendEmailWithRecipient} from 'backend/email';

$w.onReady(function () {
  $w("#dataset1").onAfterSave(sendFormData);
});

function sendFormData() {
 const subject = `Thank You For Your Request, ${$w("#input3").value}`;
 const body = `Hello, ${$w("#input3").value}. So, you're interested in testing our product free of charge? We're happy to hear that! The sales team will be in touch soon. In the meantime, please review the contact information you provided below:
    \rName: ${$w("#input3").value}
    \rCompany: ${$w("#input6").value}
    \rPhone Number: ${$w("#input4").value}
    \rEmail: ${$w("#input5").value}
    \rCity: ${$w("#input8").value}
    \rState or Province: ${$w("#input7").value}
    \rNotes: ${$w("#textBox1").value}
    If all the above information is correct, no action is required from you.`;

 const recipient = $w("#input5").value;
 
  sendEmailWithRecipient(subject, body, recipient)
    .then(response => console.log(response)); 
 
  sendEmail(subject, body)
    .then(response => console.log(response));
}

---

//sendGrid.js

import {fetch} from 'wix-fetch';  

export function sendWithService(key, sender, recipient, subject, body) {
 const url = "https://api.sendgrid.com/api/mail.send.json";
 
 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());
}

---

///email.jsw

import {sendWithService} from 'backend/sendGrid';

export function sendEmail(subject, body) {
 const key = "Hidden";
 const sender = "marketing@myemail.com";
 const recipient = "marketing@myemail.com";
 return sendWithService(key, sender, recipient, subject, body);
}

export function sendEmailWithRecipient(subject, body, recipient) {
 const key = "Hidden";
 const sender = "marketing@myemail.com";
 return sendWithService(key, sender, recipient, subject, body);
}

1 Like

So you are using the Wix tutorial for SendGrid.
https://support.wix.com/en/article/corvid-tutorial-sending-an-email-on-form-submission

I myself have used this on my own website before I moved over to triggered emails instead and I never experienced the issue of not getting anything afterwards when a user used ‘&’.

I also made sure that I completed the SendGrid Server Authentication so that everything was fine with them sending on my behalf and not having the sent by SendGrid tagline either.
https://sendgrid.com/docs/ui/account-and-settings/how-to-set-up-domain-authentication/

As you state that Wix gets everything clearly and it is only on the email that is done through SendGrid, then to me it indicates that it is something in SendGrid workings that is causing this and making the email cutoff.

I would go back to SendGrid and contact their own support and ask them if they have any occurence of this happening previously and if they could recommend a fix or solution to you.

It might just be something simple like this is an invalid character and can’t be used.

Otherwise, I do hope that somebody else from this forum can help you towards a solution that fixes this, or you could be stuck with having to validate your user inputs so that users can’t put in ‘&’ to begin with.

Thank you for your reply, givemewiskey!

I don’t know whether or not I will get any help from SendGrid as I am using their service free. Although I did open a ticket with them and hope they get back to me.

In the meantime, I’ve been trying to figure out how to validate my fields:

$w.onReady(function () {
  $w("#input6").onCustomValidation( (value, reject) => {
 if(value === "&") {
    reject("Please remove ampersand"); //where is this message supposed to show?
    $w("#text138").show(); //msg: "Ampersand is an invalid character"
    $w("#vectorImage2").show(); //msg container
    $w("#vectorImage3").show(); //msg arrow
  }
 else {
    $w("#text138").hide(); //msg: "Ampersand is an invalid character"
    $w("#vectorImage2").hide(); //msg container
    $w("#vectorImage3").hide(); //msg arrow
  } 
  } );
});

I’m obviously a novice and my code above does not seem to be doing it’s job.

In situations where an user might input “Otodata & Sons” it wont stop a user from submitting the form. Is there a way of using a parameter which would work like this: If contains & = reject ?

Can you offer any insight?

Thank you

@givemeawhisky , as someone who has used both SendGrid and Wix triggered emails, would you mind posting the pros & cons (I am using SendGrid, and have never used Wix triggered emails, and would like to know more about them from someone who experienced both.) Thanks!

@bmormina

I can’t see any reason why SendGrid won’t help you as you are using their api for the sending of the email etc and the error does seem to be on their end of the functions, plus regardless of the free or paid option you are their customer and they should help all, could be more custom for them so silly not to reply.

Anyways, as for validation, have a good read of the many pages already out there in the Wix Support sections, plus you can also utilise the search function of this forum too and simply search for validation for previous posts about it here.

https://support.wix.com/en/article/corvid-about-validating-user-input-with-code
https://support.wix.com/en/article/working-with-user-input-validation-in-the-settings-panel
https://www.wix.com/corvid/example/custom-validations - this one you can open up straight in your editor and view al the elements and code already setup.

API Reference for you to read.
https://www.wix.com/corvid/reference/$w.ValidatableMixin.html
https://www.wix.com/corvid/reference/$w.TextInput.html
https://www.wix.com/corvid/reference/$w.TextInputMixin.html

Wix Corvid Forum searches that you can try.
https://www.wix.com/corvid/forum/search/validation
https://www.wix.com/corvid/forum/search/custom-validation
https://www.wix.com/corvid/forum/search/validate-user-input

@givemeawhisky
Thank you for taking the time to respond.
I have actually been circling around these articles all morning.

From what I have read there doesn’t seem to be a way to specify:
If contains/includes “&” = reject

Unfortunately, I have no formal training and just not good enough at coding to put these articles to good use. I’ll keep looking…

Hopefully SendGrid will get back to me.

Thank you

While I await SendGrid’s reply I did the following:

I abandoned the idea of “rejecting” an input which contains an “&” symbol and instead opted to “replace” any instance of “&” with “and”:

Example below.

$w.onReady(function () {
 $w('#input2').onFocus((event)=>{
    let str = $w('#input1').value;
    $w('#input1').value = str.replace(/&/g,'and');  
  });
});

Form Title
[ input1 ] **will change any instance of “&” to “and” once user focuses on Input2)
[ input2 ]
[ Submit Button ]

Quick reply, basically it is just much easier for me to keep everything in-house so to speak, you can do the page and triggered email design and code for it all at the same time and without having to leave Wix etc.

Yes I could have looked at doing more myself through SendGrid with regards to the emails that they send out and using SendGrid itself more, although I was only using them for the free option for the emails to be sent out.

However, it made more sense to just use triggered emails and not have to worry with having to work out code for Wix and for SendGrid too and also not have to work through SendGrid support if there was any issues on their side.

Yes there was the already supplied tutorial which sort of helped and edged me towards using SendGrid, however as I was only after the free email I decided it wasn’t worth the hassle having the SendGrid account when I could do it all through Wix itself.

You have probably delved deeper into SendGrid and utilised more of what SendGrid can do and it’s different functions, however for me and what I needed it to do it wasn’t necessary. So, now I just use user input saved to a dataset and have the triggered email run with the aftersave call.

Hope that makes sense!

Probably a much better idea so you are not enforcing users to not use the & when they might have the actual need to use it, instead just replacing it with a working fix for yourself.

If this works for you now then great, although I do hope that SendGrid can come back to you and explain why it does cutoff if a & is used.

@givemeawhisky Thanks!

https://www.wix.com/corvid/forum/community-discussion/problem-with-and-in-form-inputs-in-sendgrid

hello guys i’m intermediate to wix code and i could do the following email code, but I need some help in sending to multiple emails at the same time so i wanted to know if anyone here could help

I would really appreciate :slight_smile:

like this:

///email.jsw

import {sendWithService} from 'backend/sendGrid';

export function sendEmail(subject, body) {
 const key = "Hidden";
 const sender = "marketing@otodatatankmonitors.com";
 const recipient = "marketing@otodatatankmonitors.com,okeyiwobimusic@gmail.com"; //this doesn't work,it only sends to the first one :(
 
 return sendWithService(key, sender, recipient, subject, body);
}

export function sendEmailWithRecipient(subject, body, recipient) {
 const key = "Hidden";
 const sender = "marketing@otodatatankmonitors.com";
 return sendWithService(key, sender, recipient, subject, body);
}

i would really appreciate

Hi there,
Although I unfortunately don’t know the answer to your question:
Can you please remove “marketing@otodatatankmonitors.com” from your code?
Thank you