Serverless Email Sending
Add to favorites
Use EmailJS to send an email without backend

Advanced React Hooks Handbook
1
Intro to Firebase
6:59
2
Firebase Auth
11:59
3
Firestore
10:51
4
Firebase Storage
6:40
5
Serverless Email Sending
10:02
6
Geocoding with Mapbox
9:24
7
Contentful Part 1
4:59
8
Contentful Part 2
8:52
9
Gatsby and Shopify Part 1
5:20
10
Gatsby and Shopify Part 2
13:21
11
Gatsby and Shopify Part 3
14:32
12
Creating Stripe Account and Product
5:18
13
Adding Stripe.js to React
5:54
14
Stripe Checkout Client Only
18:18
15
PayPal Checkout
31:21
CodeSandbox Link
You can find the full code for this tutorial at https://codesandbox.io/s/send-email-without-backend-ofzf6.
Create an account
Create an EmailJS account. The free tier allows you to send up to 200 emails for free every month. If you wish to send more than that, you can explore their pricing page to discover all their different plans. Once signed up, you'll land on the following screen.
EmailJS Dashboard
Email services
In order to be able to send an email, you need to add a service to your EmailJS account. If you plan on sending only a few emails, the easiest way is to choose your email provider, under Personal Services. If you plan on sending transactional emails, or a lot of them, select your preferred service under the Transactional Services section.
EmailJs Available service integrations
In my case, I plan on sending only a few emails. My email provider is Gmail, so when I click on it, I'll need to configure the service. Click on the Connect Account button in order to link your Gmail account to EmailJS. This will allow EmailJS to send email under your name/email. Copy the Service ID, as we'll need it later.
EmailJS Config Service
Email templates
Next, we'll need to create an email template. Click on the Email Templates tab and on Create new template. The default template will look like this:
EmailJS Default Template
The text in curly brackets, for example the {{from_name}}, is a variable that you'll pass to the function in order to send the email. Let's change a few of them to make it easier in the coding process afterwards.
EmailJS Template
In the template above:
- Replace the {{to_name}} to {{name}}
- Remove the variable after the Hello
- Replace the {{reply_to}}, in the Reply To field on the right side, by {{email}}
Then, let's click on the Settings tab of this template. Let's rename our template by My Template - or any other name you wish. Copy the Template ID, as we'll need it later. Remember to click on Save when you're done.
EmailJs Template Settings
Integrate EmailJS
In order to use EmailJS in our project, we'll need to install the SDK. Run the following command in the Terminal in order to save the EmailJS Package.
$ npm install emailjs-com --save
We have one last ID, which is the User ID, to get. Back in EmailJS's dashboard, click on Integration. Under the NPM tab, you'll see the instructions on how to install and use the SDK. You'll also see your User ID in the second and third blue rectangles. Copy it, as we'll need it in the code. We won't need the access token.
EmailJs Integration
Create your button
Create a button so that we can call the function to send an email on click on that button. This button takes one prop, which is the onClick event. We're also using styled-components in order to style this button.
// Button.js
import React from "react";
import styled from "styled-components";
const MyButton = (props) => {
return <Button onClick={props.onClick}>Send email</Button>;
};
export default MyButton;
const Button = styled.button`
background: linear-gradient(91.4deg, #2fb8ff 0%, #9eecd9 100%);
padding: 12px 0;
width: 200px;
border: none;
border-radius: 30px;
color: white;
font-weight: bold;
font-family: Segoe UI, sans-serif;
display: grid;
justify-self: center;
`;
Create the function
Create a new file where we'll store the function to send an email. We need to import emailjs, from the SDK we installed in our project. We also need to provide the three IDs we copied at the beginning of this tutorial (serviceId, templateId and userId), so paste them in their corresponding variable, between the double quotes.
Note: For the purposes of this tutorial, we're hard-coding the keys in our project. However, never hard-code your keys in your file if you plan to share your project or commit it, as it is a big security flaw. Save them in a .env file to keep them secret.
// sendEmail.js
import emailjs from "emailjs-com";
const serviceId = "";
const templateId = "";
const userId = "";
const sendEmail = async (name, email, message) => {
try {
const response = await emailjs.send(
serviceId,
templateId,
{ name, email, message },
userId
);
if (response.status === 200) {
console.log("Successfully sent message.");
}
} catch (error) {
console.error("Failed to send email. Error: ", error);
}
};
export default sendEmail;
This function takes three arguments: a name, an email and a message. These are the three variables that we added to the template on EmailJS's dashboard. Then, we're calling the send() function from the EmailJS SDK, and if everything works well, we'll get a Successfully sent message printed on the console. If not, we'll see an error. Remember to also export the function at the bottom of this file.
Call the function
In the App.js file, or any file that you wish, import the Button and add it in the return body.
// App.js
import React from "react";
import MyButton from "./Button";
const App = () => {
return (
<div>
<MyButton />
</div>
);
};
export default App;
Then, let's import the sendEmail function from sendEmail.js.
// App.js
import sendEmail from "./sendEmail"
On click on the button, we'll call the sendEmail function. Remember to pass the three required arguments to the function: a name, an email and a message.
<MyButton
onClick={() =>
sendEmail(
"Stephanie",
"stephanie@designcode.io",
"Hello from my React application!"
)
}
/>;
You can learn how to create a form and get each value the user inputs in the UserInput Hook tutorial of this handbook. Once you get the value of each field, you can pass them to the function in order to send an email with dynamic data, instead of hard-coding strings every time.
Test out your function
Now comes the fun part: to test everything! Click on the button, and open the console to see if you get a success or an error message. You can also double-check by going to your email inbox. You should have received an email, like this one below.
Example of email sent from EmailJS
If you ran into an error, make sure that you copied the right IDs and that you enabled EmailJS to send emails on behalf of you.
Learn with videos and source files. Available to Pro subscribers only.
Purchase includes access to 50+ courses, 320+ premium tutorials, 300+ hours of videos, source files and certificates.
Templates and source code
Download source files
Download the videos and assets to refer and learn offline without interuption.
Design template
Source code for all sections
Video files, ePub and subtitles
Browse all downloads
1
Intro to Firebase
Learn about Firebase and set it up in your React project
6:59
2
Firebase Auth
Set up authentication in your React application using Firebase Auth
11:59
3
Firestore
Enable Firestore as your database in your React application
10:51
4
Firebase Storage
Enable Firebase Storage in your application to store photos or videos from users
6:40
5
Serverless Email Sending
Use EmailJS to send an email without backend
10:02
6
Geocoding with Mapbox
Create an address autocomplete with Mapbox's Geocoding API
9:24
7
Contentful Part 1
Create a Contentful account, add a model and content
4:59
8
Contentful Part 2
How to use the Content Delivery API to fetch data from Contentful
8:52
9
Gatsby and Shopify Part 1
Create a Shopify store, generate a password and add products to the store
5:20
10
Gatsby and Shopify Part 2
Connect Shopify to Gatsby and display all products
13:21
11
Gatsby and Shopify Part 3
Create a seamless checkout experience with Shopify and Gatsby
14:32
12
Creating Stripe Account and Product
Start integrating with Stripe and set up an account, product and price
5:18
13
Adding Stripe.js to React
Loading Stripe.js library to your React client application
5:54
14
Stripe Checkout Client Only
Accept payment with Stripe Checkout without backend
18:18
15
PayPal Checkout
Integrate online payment with PayPal
31:21
Meet the instructor
We all try to be consistent with our way of teaching step-by-step, providing source files and prioritizing design in our courses.
Stephanie Diep
iOS and Web developer
Developing web and mobile applications while learning new techniques everyday
7 courses - 36 hours

Build Quick Apps with SwiftUI
Apply your Swift and SwiftUI knowledge by building real, quick and various applications from scratch
11 hrs

Advanced React Hooks Handbook
An extensive series of tutorials covering advanced topics related to React hooks, with a main focus on backend and logic to take your React skills to the next level
3 hrs

SwiftUI Concurrency
Concurrency, swipe actions, search feature, AttributedStrings and accessibility were concepts discussed at WWDC21. This course explores all these topics, in addition to data hosting in Contentful and data fetching using Apollo GraphQL
3 hrs

SwiftUI Combine and Data
Learn about Combine, the MVVM architecture, data, notifications and performance hands-on by creating a beautiful SwiftUI application
3 hrs

SwiftUI Advanced Handbook
An extensive series of tutorials covering advanced topics related to SwiftUI, with a main focus on backend and logic to take your SwiftUI skills to the next level
4 hrs

React Hooks Handbook
An exhaustive catalog of React tutorials covering hooks, styling and some more advanced topics
5 hrs

SwiftUI Handbook
A comprehensive series of tutorials covering Xcode, SwiftUI and all the layout and development techniques
7 hrs