Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email from my react web application

i have a single email input field and a button in my app. once i enter the email and click send i would like to send an email with the value entered in the input. i know it cant be done in Reactjs because it is a frontend. How can i do? can someone help?

like image 665
Rakesh Rajan Avatar asked Apr 22 '19 13:04

Rakesh Rajan


People also ask

How do I send an email from React app?

Send Emails in a React App Using Mailto It's the simplest way to send an email from a frontend app. Put the email address that you want to receive the email after mailto: That's all it takes to open the user's default email client on their machine and populate the “recipient” field with the intended email.

How do you send an automatic email in ReactJS?

You cant not send email directly from client side however you can try emailjs or NodeMailer. This will allow you to send mail from javascript application without any server.

Can we send email from frontend?

The mailto link is the easiest way to send an email from a front-end Javascript app. Put the email address you're sending to after mailto: as part of the string.

Can I send email from JavaScript?

Can I send emails with JS or not? You can't send emails using JavaScript code alone due to lack of support for server sockets. For this, you need a server-side language that talks to the SMTP server. You can use JS in conjunction with a server script that will send emails from the browser based on your requests.


1 Answers

From a front end client you can send a request to an SMTP server, which will send the email.

This tutorial runs over how to do that with the emailJS SMTP server, (which allows you to send 200 free emails/month in case free is a priority).

I'll summarize because they didn't cover everything I wanted.


Step1

Install emailJS via

npm install emailjs-com

or

yarn add emailjs-com

Source

If you are using create-react-app please view the tutorial I mentioned above or this linked source, where they provide instructions on how to include emailJS with create-react-app

Step 2

This is a code example of a form which sends an email using emailJS, You must replace 'YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', and 'YOUR_USER_ID' with your actual id's(which I will explain where to find in later steps)

import React from 'react';
import emailjs from 'emailjs-com';

export default function ContactUs() {

  function sendEmail(e) {
    e.preventDefault();    //This is important, i'm not sure why, but the email won't send without it

    emailjs.sendForm('YOUR_SERVICE_ID', 'YOUR_TEMPLATE_ID', e.target, 'YOUR_USER_ID')
      .then((result) => {
          window.location.reload()  //This is if you still want the page to reload (since e.preventDefault() cancelled that behavior) 
      }, (error) => {
          console.log(error.text);
      });
  }

  return (
    <form className="contact-form" onSubmit={sendEmail}>
      <input type="hidden" name="contact_number" />
      <label>Name</label>
      <input type="text" name="from_name" />
      <label>Email</label>
      <input type="email" name="from_email" />
      <label>Subject</label>
      <input type="text" name="subject" />
      <label>Message</label>
      <textarea name="html_message" />
      <input type="submit" value="Send" />
    </form>
  );
}

Source (I made some adjustments)

  • If you don't want your api details to be visible from the client, you would have to make a request containing the email message, from, to, etc. to a backend server via something like axios or fetch which would execute the sendEmail function

Step 3

Set up an account at https://www.emailjs.com/

Step 4

Connect an email by adding a service, in this case I used gmail.

'YOUR_SERVICE_ID'(step 2) should be the id beside Service ID

enter image description here

Step 5

Create a a template by clicking Email Templates in the side nav and selecting create new template. This will create an email outline and give you a template id

You should replace 'YOUR_TEMPLATE_ID'(step 2) with this template id

enter image description here

Here is an example Email template

enter image description here

Step 6

You can find your userID by going to Integration -> API

Replace 'YOUR_USER_ID'(step 2) with the id under API Keys -> Public Key

enter image description here


  • If you don't want spam emails, add Captcha
  • if you change your email password, you have to redo step 4
like image 110
Sam Avatar answered Oct 30 '22 00:10

Sam