Form Submission Email

Hi all,

Relatively new to the coding world and although learning as I go, I’ve bumped into a bit of a dilemma.

I’ve been following the tutorial (- YouTube) to get this set up and completed every step. However, when I go to test this in Preview mode, I get the following at the bottom of the screen:

Cannot find module ‘backend/email’ in ‘public/pages/[NAME].js’

Loading the code for the [NAME] page. To debug this code, open [NAME].js in Developer Tools.

Essentially, the automated email I wish to be send to both myself and the recipient isn’t working and wondered whether there is an easy fix for this?

Any help for this newbie would be greatly appreciated!

Thanks,

Thomas

Try using automations under customer management from your dashboard if this is proving too hard. But you also should know you have to create the backend js files in developer mode. If you open the left side bar it is under the pages section. Hope this helps

Hi elizabethjhay, because the form I’ve created is a custom (Wix Code) form, is there a way to make Automations work, as the options in the dashboard are limited to: Create automated responses for any of these apps. Price Quotes, Invoices, Contact Form, Wix Hotels, Member’s Area

On the other hand, I have created the backend files in developer mode, both .jsw and .js forms - so unsure as to why this isn’t functioning…

Thanks for your reply!

That is not true. You should see custom form if you created it right. Make sure the form has a database.

I do have a database and believe this is set up correctly as when testing the form earlier today and revisiting the database, it captured all the information from the form…

Here’s some screen grabs

Are you on a premium plan? This is what my dashboard looks like.


Yes

It’s strange as I cannot see those options available to me…

The plan is Business Unlimited

I would contact wix to see how you can get access. It might be a glitch. I have the send grid set up as well. I followed this tutorial you listed so it might be your account.

Also post your codes. I think I know what is going on.

Are you based in the UK? Unsure why this would change things, but just wondering. (I’ve sent Wix a message)

@info81994 No I am in the US, but that shouldn’t matter.

Here we are (I’ve removed some details just for data protection, if you need any of them, let me know):

Backend:

Email.jsw:

//email.jsw

import {sendWithService} from ‘backend/sendGrid’;

export function sendEmail(subject, body) {
const key = “KEY HERE”;
const sender = “EMAIL ADDRESS HERE”;
const recipient = “EMAIL ADDRESS HERE”;
return sendWithService(key, sender, recipient, subject, body);
}

export function sendEmailWithRecipient(subject, body, recipient) {
const key = “KEY HERE”;
const sender = “EMAIL ADDRESS HERE”;
return sendWithService(key, sender, recipient, subject, body);
}

sendGrid.js:

//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());
}

Page Code:

// For full API documentation, including code examples, visit Velo API Reference - Wix.com

$w.onReady( function () {
//TODO: write your page related code here…

});

import {sendEmail, sendEmailWithRecipient} from ‘backend/email’;

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

function sendFormData() {
const subject = Thanks **for** your order!;
const body = We've received your order and it'll be **with** you shortly! \rName: ${$w("#input1").value} \rEmail Address: ${$w("#input4").value} \rContact Number: ${$w("#input3").value} \rSuite: ${$w("#dropdown1").value} \rChoose a time: ${$w("#dropdown2").value} \rSpecial Requests: ${$w("#textBox1").value};
const recipient = $w(“#input4”).value;

sendEmailWithRecipient(subject, body, recipient)
.then(response => console.log(response));

sendEmail(subject, body)
.then(response => console.log(response));
}


The error showing on the page in preview is:

Cannot find module ‘backend/email’ in ‘public/pages/np106.js’
Loading the code for the Breakfast page. To debug this code, open np106.js in Developer Tools.


Thanks so much for your help, I really appreciate it!

@elizabethjhay I’ve just cracked this! My site wasn’t published and the database wasn’t live, so I’ve just published to get it up and now my automations show all of those options - thank you! Still curious on the sendgrid side of things though, so if you have any ideas, that’d be grand!

ok so 2 things.

  1. code is case sensitive if I had to guess it is because your file is called Email and not email
    try switching backend/email to backend/Email.
  2. If that doesn’t work, it might be your DB permissions. Try making a public js and jsw file vs a backend one.
    Let me know how it works out

You are fantastic! The first option (case sensitive) was what made it all work.

I’ve now got an option between this and Wix Automation so thank you very much and hope you have a great evening!

@info81994 You are so very welcome. Wix code does make things easier, but sometimes the answers are not so obvious. You have a great night as well! I believe it is quite late where you are. :slight_smile:

@elizabethjhay as you have followed the same tutorial, I have set up everything in the code to display in the emails that get sent, which work! However, there’s one part of the form I haven’t included as I wasn’t sure how to code it.

I have some Checkboxes/Booleans on the form - do you know which code should I use to incorporate these into the email?

Apologies, I assure this is my last question!

:slight_smile:

@info81994 Yes, I do. Still include them like you normally would after the last line.
Example:
\rBoolean answer1 ${$w(" # inputname").value}`;
boolean field names can be found in the properties panel.
these will come up as true for yes checked and false for no checked.
Let me know how it works out.