DEV Community

Cover image for Integrating EmailJs with ReactJs
ektaarora3501
ektaarora3501

Posted on

4 4 1

Integrating EmailJs with ReactJs

Almost all the web applications we create, we need to send emails to users, whether it be a support email or verification one. In the below article we will go through all the steps of sending emails directly with ReactJs.
All we need is a basic knowledge of React and EmailJs account.

So developers fasten up your seat belt! Here we go !! 🚀

1. Creating EmailJs account

  • Click on the link and create a free account on EmailJs
  • Select the service you want to start with. I am using Gmail Service.
  • Connect your account and click on add service.
  • You will see Gmail service in added services .

2. Creating mail template

  • In your EmailJs dashboard click on Email Templates -> Create New template. Setup your template name and id.
  • You can configure the template as per your requirement.
  • You can use curly braces for the fields that will receive data from react code as shown below. I have made "To email" field dynamic.

Alt Text
Woo! Our template is set 🎉

3. Time for some code .. 😎

  • Create a new React App
  • Install the EmailJs package using command $ npm install emailjs-com --save
  • Import Email js in your App.js file import * as emailjs from "emailjs-com";
  • Grab the service Id and template id from your template-> Copy Code

id

  • Copy your user id from Account-> API Keys -> User ID
  • Modify your App.js file
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Enter your email here
<input type="email" onChange={event => setEmail(event.target.value)}/>
<button type="submit"
onClick={handleClick}
>Send mail</button>
</p>
</header>
</div>
  • Create the handleChange function
function handleClick() {
console.log(email)
var data = {
to_email:email,
};
emailjs.send(SERVICE_ID, TEMPLATE_ID, data, USER_ID).then(
function (response) {
console.log(response.status, response.text);
},
function (err) {
console.log(err);
}
);
}
view raw handleChange.js hosted with ❤ by GitHub

And you are done!!!
Woo! now you can send emails directly from react . 🎉

You can find the complete code on my github repository React-Emailjs

Top comments (2)

Collapse
 
thewizardofwikacy profile image
The Wizard of Wikacy
Comment hidden by post author
Collapse
 
naveenkolambage profile image
Naveen Dinushka

Hey Mate this is not working anymore

Some comments have been hidden by the post's author - find out more

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay