In this tutorial, you'll learn how to set up a complete email system that allows site visitors to send emails through a custom form on your Wix site. The solution uses SendGrid's API to handle email delivery while maintaining security best practices by storing sensitive API keys in Wix's Secrets Manager.
SendGrid is a cloud-based email delivery platform that provides reliable email infrastructure for businesses and developers. By integrating SendGrid with your Wix site, you can send emails such as contact form submissions, order confirmations, password resets, or custom notifications to your users. This approach gives you more control over email delivery, better tracking capabilities, and higher deliverability rates compared to basic email services.
To set up your site to send emails with the SendGrid service, you'll need to perform the following steps:
The code in this article was written using the following module versions:
Learn how to install npm packages in the editor or using the CLI.
To use the SendGrid service on your Wix site, you'll need to create a SendGrid account. The email address you verify in the account will be the sender address for the emails you send from your Wix site.
Once you've created an account, create an API key. Copy the key from your dashboard and store it in the Secrets Manager in your Wix site (see the next step).
In this step, you'll configure your Wix site by securely storing your SendGrid credentials and installing the required npm package.
For security purposes, it's best to store sensitive content such as API keys in the Secrets Manager:
Navigate to Developer Tools in the Code sidebar.
Under the Security section, select Secrets Manager.
In the top right, click Add Secret.
Set the Secret Name to sendGridSecret.
Store both your SendGrid API key and the verified email address associated with your SendGrid account in the secret value. Your secret value will look like this:
Click Save.
Go back to the site editor and install the npm package.
On your Home page, create a custom form and add the following elements:
#toEmail and #subject.#emailContent.#sendButton.#messageText.In this step, you'll securely retrieve your SendGrid credentials and handle the email sending process using SendGrid's API.
Add a web module to your site code and call it sendEmail.web.js.
Import the modules you'll need:
In the sendEmail function, get the SendGrid API key and sender email address that you saved in the Secrets Manager, and set the API key to enable usage of the SendGrid service. For this, use the Secrets API:
Set the data for the email to be sent, such as the recipient's email address and the text of the email body. The sender email was extracted from the Secrets Manager and the rest of the information is entered by site visitors on the frontend by using input elements. Call the send() function to send the email.
In this step, you'll handle user interactions with the email form, validate input fields, and communicate with the backend function to send emails.
Open the page code file and import the function from the backend:
Add the checkFormFields() function to make sure all form field input values are valid before sending an email:
Add the clearFields() function to reset the form fields following a successful email transmission. The resetValidityIndication() function resets the inputs' visual validity indications. Some elements have a visual cue, such as a red outline, that indicates when the current value isn't valid. This function clears any indications.
Add the displayMessage() function to temporarily show a success or error message following an attempt to send an email:
When a site visitor clicks the Send button, do the following:
Here is the complete code for this example: