473,657 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to send emails with ReactJS using EmailJS?

sofiatarhonska
1 New Member
Probably everyone knows that ReactJS is a wonderful library for building web-applications. However, it doesn't look so easy to add some backend features to it. For example, how would you build in a contact form?

In this post I'm going to cover some methods on how to do it simply.

First of all, you cannot send emails with just React, you have to use 3rd party tools to do it. EmailJS lets you set up sending without touching the backend at all. You just have to connect the email client and create templates. As for email clients, you can use whichever you like: Gmail, Outlook or more pricey ones like Mailgun.

Let's proceed with the most common one - Gmail.

First step - sign up at https://www.emailjs.com . Then, build your email template. There is a ready template builder for it. You can add attachments, auto-reply or dynamic variables.


Image credit: Mailtraps tutorial on sending emails using ReactJS

Next step is connecting EmailJS to the React project. You can do it with npm.
Now, let us send the email itself. You can send simple emails with emailjs.send and forms with emailjs.send.Fo rm.

As for forms, you should put the following code into the src/Form.js file of your project:
Expand|Select|Wrap|Line Numbers
  1. import React from 'react';
  2. export default class extends React.Component {
  3.   constructor(props) {
  4.     super(props);
  5.     this.state = { feedback: '', name: 'Name', email: 'email@example.com' };
  6.     this.handleChange = this.handleChange.bind(this);
  7.     this.handleSubmit = this.handleSubmit.bind(this);
  8.   }
  9.   render() {
  10.     return (
  11.       <form className="test-mailing">
  12.         <h1>Let's see if it works</h1>
  13.         <div>
  14.           <textarea
  15.             id="test-mailing"
  16.             name="test-mailing"
  17.             onChange={this.handleChange}
  18.             placeholder="Post some lorem ipsum here"
  19.             required
  20.             value={this.state.feedback}
  21.             style={{width: '100%', height: '150px'}}
  22.           />
  23.         </div>
  24.         <input type="button" value="Submit" className="btn btn--submit" onClick={this.handleSubmit} />
  25.       </form>
  26.     )
  27.   }
  28.   handleChange(event) {
  29.     this.setState({feedback: event.target.value})
  30.   }
  31.   handleSubmit() {
  32.   }
  33. }
Then, add the handleSubmit() to sendFeedback() to start sending:
Expand|Select|Wrap|Line Numbers
  1.   handleSubmit (event) {
  2.     const templateId = 'template_id';
  3.     this.sendFeedback(templateId, {message_html: this.state.feedback, from_name: this.state.name, reply_to: this.state.email})
  4.   }
  5.   sendFeedback (templateId, variables) {
  6.     window.emailjs.send(
  7.       'gmail', templateId,
  8.       variables
  9.       ).then(res => {
  10.         console.log('Email successfully sent!')
  11.       })
  12.       // Handle errors here however you like, or use a React error boundary
  13.       .catch(err => console.error('Oh well, you failed. Here some thoughts on the error that occured:', err))
  14.   }
  15.  
Just don't forget to import Form component from the main component in src/App.js. And you are ready to customize the function with your own parameters.
And you are ready to go! See, simple?
Sep 22 '22 #1
1 8763
Lord123
1 New Member
With EmailJS, you can send emails with ReactJS using the EmailJS ReactJS SDK.
Oct 22 '22 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

40
11850
by: ian | last post by:
Hi, I'm a newbie (oh no I can here you say.... another one...) How can I get Python to send emails using the default windows email client (eg outlook express)? I thought I could just do the following import win32com.client
6
2671
by: slider | last post by:
Hey all i am using Access 2003 on Win Xp and i have been trying to make this code send emails to the users stored on each record. So far is works, except in the body of the email i need it to say a number of things, such as Dear (user), your current quota is (quota), and so on. So far i can send the emails to multiple users but when it comes down to writing the message i cannot enter in more than one "rec("field)". In my database i have...
3
2958
by: drnorbert | last post by:
What is the best way to receive and send emails in MsAccess? Could this be done without Outlook automatation?
3
1454
by: Thomas Weise | last post by:
My app attempts to send eMails via SmtpClient. This works with some addresses, but for instance a mail server hosted by godaddy always rejects the mails. The reply is "Relay access denied". Does anybody have an idea what the cause of the problem is, and how I can work around? Thanks in advance, Thomas
0
1177
by: manontheedge | last post by:
I'm trying to automatically send emails through VB, through Microsoft Outlook 2003, but get the security message ... "A program is trying to automatically send e-mail on your behalf. Do you want to allow this?" which means, I have to be sitting at my computer at the time I want the email to "automatically" send. I've searched the Internet, and all I found was software to install to fix this problem. I don't want that. I'm wondering...
0
858
by: gauravbhati | last post by:
Hello, The task is to send emails in batch at specific times and not each time if user clicks on submit. Details: I have an InfoPath form which employees will use to putup request to update their desktop software, request to get a new cartridge etc etc. Upon submitting form, the workflow is handled by a C# web service. In this web service, after doing all MOSS 2007 related work, an email notification gets sent to IT support team. To avoid...
1
1029
by: nazzzzz | last post by:
can anyone help me to send emails to ids in database using vb.net and microsoft outlook thanks in advance..
2
1452
by: =?Utf-8?B?U3VzYW4=?= | last post by:
I can't send emails on my Microsoft outlook express 6. HELP?
1
1843
by: Fuzz13 | last post by:
I am writing a program that will periodically scan for changes in a file, and if that file changes I need it to send out an email based on the email field that the user fills in. My question is, can I have my program send emails automatically based on tests, and if so do I have it done thru an account like myprogram@gmail.com? Is there a way to tell my program how to send an email without some established webmail server like yahoo or gmail? ...
1
4775
by: MHutcheson81 | last post by:
I have a MS Access 2010 db taht I want to send emails from automatically. I have the query setup but am getting stuck with the CDO VBA. They query is call 'qryEmails' and contains the following 4 fields: ReturnCode\SalesOrderNumber\Name\EmailAddress How do I get access to: 1. Loop through each record and send an email to each email address listed 2. In each email, have a message that will contain reference to the first 3 fields, so each...
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.