Tutorial: React + Emailjs
Tags: reactjsdevelopment, beginners, webdev, tutorial
Published: 2024-03-27 20:27:43

## Introduction

In this tutorial, we will go through the process of setting up Emailjs with a React application built using the Vite build tool.
Emailjs is a software application that lets you "send email directly from your code". The benefit is that you do not have to build a complex server on the backend of your web application to handle emails. Let's begin.

### Step 1: Scaffold React Project using Vite

On the Command Line Interface, navigate to where you want to keep your project on your device and run the following command:

```
npm create vite@latest
```
You will be presented with the following:

```
? Project name: > vite_project
```
For this tutorial, we will name our project ContactForm. (You can enter any name relevant to the project you are building) 

Next, we will type the project name at the prompt and press Enter.

```
? Project name: > ContactForm
```
After this, we are presented with a selection of frameworks. Use the arrow keys to select **React** and press Enter. Then we are given a list of variants of **React**. Again use the arrow keys to select **JavaScript** and press Enter.

After you have done the above, Vite will give you the precise commands to follow to complete scaffolding the project. The commands are:

```
cd ContactForm
npm install
npm run dev
```
Run the commands above and wait for Vite to finish scaffolding the project. Once this process has run its course you should have a React project with a file structure similar to what we have below in your favorite code editor. Note that we are using VS Code for this tutorial.


![React File Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pkl1es8p0tvngp4mle35.png)

If you want to learn more about the Vite build tool [here](https://vitejs.dev/) is a link to the documentation.

### Step 2: Build ContactForm

Now let us go ahead and build a simple contact form. The form will have only 3 fields. 

Look for App.jsx in the project structure and delete all the redundant code in the file until you are left with something like this:

![Remove code from App.jsx](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1zfh2lejxy8be03l4la.png)

We can now write the code for our form. 

![React Form Inputs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12sjm171jm0p1e8dqeyk.png)

Things to note from the code above:

1. We have created 3 form inputs: 2 text inputs and a textarea.
2. We have also added an optional placeholder identifying what each input does for our users.
3. We have also created a "name" attribute. Take special note of this attribute because we will need it to set up our Emailjs templates.

Speaking of which let us now navigate to [Emailjs](https://www.emailjs.com/).

### Step 3: Configure Emailjs

On the Emailjs homepage sign up for a free account. Once you have signed in you will be presented with a page like the below below.


![Email js startup page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3eukktp22qr37qxxpsz1.png)

Follow these steps:

a. Click on Add New Service

![Add a service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3oxmnii3j6yj8dcmyt6.png)

b. In this tutorial, we will use Gmail. After selecting the service, you should have the following on your screen:

![Select a service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivh6kits07qhavu29qtt.png)

c. Click "Connect Account" and connect your email to Emailjs. Take note of the service ID. We will need it later. Now click "Create Service". Once the service has been connected successfully, you will get the following screen:

![Create a service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e1auhi1i4fpidt5xwv79.png)

Take note of the **Service ID**.

The next thing to do is create an email template. 

d. Select Email Templates. Click "Create New Template". Note how variables are defined in Emailjs. In this instance "from_name" is a variable

![Default email template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rvnqg44ong56zfqgonhk.png)

e. Remember the name attributes we created in our code earlier? Let's replace the variables in Emailjs with those attributes like this:

![Setting emailjs variable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1joezjv2lcsrydix1fv.png)

Note: to edit the content of the email, click "Edit Content" and select Design Editor from the pop-up menu. Then save the template. 

f. Let us change the name of our template before we return to our code editor. Go to Settings and change the template name to "Contact Form". For this tutorial, we will keep the Template ID as it is but you can change it if you want to. Save the template and go back to the Email Templates page.
The page should now look like this. Take note of the **Template ID**

![New email template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nr0swkvdsqigviy021m.png)

Let us go back to our code editor.

### Step 4: Configure the .env File

We will put our Emailjs environment variables in a dotenv (.env) file. To read more about the purpose of this file click [here](https://www.npmjs.com/package/dotenv).

Navigate to the root of the folder structure in your project and create a .env file.

![Folder structure with dotenv file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d3mnmq5zzxuuvihgenr.png)

Let's add our variables to this file. The variables we'll need from Emailjs are **Service ID**, **Template ID**, and **Public Key**. You can find the Public Key in the Accounts tab of Emailjs. 

Now create these variables in the .env file. 

![Environment variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhfm92us0b0hv9y17pdu.png)

Replace "YOUR SERVICE ID", "YOUR TEMPLATE ID" and "YOUR PUBLIC ID" with your IDs and key from Emailjs.

### Step 5: Write the Send Email Function

We will now go into App.jsx and write the function responsible for getting the data from the form inputs and sending it to Emailjs. 

But before we do this, we need to install the Emailjs dependency or SDK in our project. Open the terminal and make sure you are in the project folder. Run this command in the terminal.

```
npm install emailjs-com
```
This command will install email-js as a dependency in your project. To confirm that it has been installed open the package.json file in your project and check that "emailjs-com" is listed in the dependencies.

After confirming Emailjs has been installed successfully, go ahead and import it into your project and write the function mentioned above.

The function can be written like this:

![Send email function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2v8dv90f4sr59tfaixf.png)

Things to note from the code above:

1. See how we import the environment variables from the .env file into our function.
2. Notice how we prevent the default behavior when the button is submitted.
3. Also notice how the onSubmit event handler is used.

This is a brief tutorial on using Emailjs in a React project using Vite. For a more in-depth look at Emailjs, you can visit the [documentation](https://www.emailjs.com/docs/).

Happy coding!!!