DEV Community

Cover image for How to send email using ReactJS?
Kyaw Min Tun
Kyaw Min Tun

Posted on

How to send email using ReactJS?

To send an email using React.js, you typically need to involve a backend server or use a third-party service. Here's a general approach using a third-party service like EmailJS, which allows you to send emails directly from your React application without needing a backend server.

Step-by-Step Guide to Send Email Using EmailJS in React

1. Set Up EmailJS Account

  • Go to EmailJS and create an account.
  • Once logged in, create an Email Service, which will be used to send emails.
  • Create an Email Template, which defines the structure of the email.
  • Note down the Service ID, Template ID, and User ID from the EmailJS dashboard.

2. Install EmailJS SDK

In your React project, install the emailjs-com package using npm or yarn.

   npm install emailjs-com
Enter fullscreen mode Exit fullscreen mode

or

   yarn add emailjs-com
Enter fullscreen mode Exit fullscreen mode

3. Create an Email Form in Your React Component

Create a form that users can fill out to send an email. For example:

   import React, { useRef } from 'react';
   import emailjs from 'emailjs-com';

   const ContactForm = () => {
       const form = useRef();

       const sendEmail = (e) => {
           e.preventDefault();

           emailjs.sendForm('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', form.current, 'YOUR_USER_ID')
               .then((result) => {
                   console.log(result.text);
                   alert('Email sent successfully!');
               }, (error) => {
                   console.log(error.text);
                   alert('Failed to send email.');
               });
       };

       return (
           <form ref={form} onSubmit={sendEmail}>
               <label>Name</label>
               <input type="text" name="user_name" required />
               <label>Email</label>
               <input type="email" name="user_email" required />
               <label>Message</label>
               <textarea name="message" required />
               <input type="submit" value="Send" />
           </form>
       );
   };

   export default ContactForm;
Enter fullscreen mode Exit fullscreen mode
  • Replace 'YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', and 'YOUR_USER_ID' with the actual IDs from your EmailJS account.

4. Customize Email Template

In the EmailJS dashboard, you can customize the email template to include variables like user_name, user_email, and message, which you defined in your form.

5. Deploy and Test

Deploy your React application and test the form to ensure that emails are being sent as expected.

Important Considerations

  • Security: Since you're using the User ID from EmailJS in your frontend code, make sure to secure your application. Avoid exposing sensitive information.
  • Validation: Implement form validation to ensure that users provide valid input before submitting the form.

This method is straightforward and allows you to send emails without needing a backend server.

Top comments (0)