How to Build a Contact Form with React and Nodemailer

Victoria Lo
May 21, 2020 · 4 min read

What’s a Contact Form?

A contact form is essential for any business to provide a convenient way for their customers to reach them. It is typically found in a business’ contact us page. In this tutorial, I will show you how to create a contact form with React and Nodemailer.

Source: https://bootstrapstudio.io/assets/img/tutorials/contact-form/contact-form-screenshot.png

Front-End: Build the Form

You can be creative in how you want to make it. You can use Bootstrap or CSS and it’s all up to you! Usually, a contact form has 3 input fields: name, email and message. But for my example, I added a Subject field too. Here’s what mine looks like:

Adding Handler Functions

Right now, the contact form is nothing but a static page that cannot send emails or anything. We will have to add functions to prepare for the back-end stuff coming later.

class Contact extends React.Component {    constructor(props) {
super(props);
this.state = {
name: '',
email: '',
subject:'',
message: ''
}
}
onNameChange(event) {
this.setState({name: event.target.value})
}
onEmailChange(event) {
this.setState({email: event.target.value})
}
onSubjectChange(event) {
this.setState({subject: event.target.value})
}
onMsgChange(event) {
this.setState({message: event.target.value})
}

Submit Email Function

So with the handler functions done, we now have to create a function that executes when the user clicks on the Submit button.

submitEmail(e){
e.preventDefault();
axios({
method: "POST",
url:"/send",
data: this.state
}).then((response)=>{
if (response.data.status === 'success'){
alert("Message Sent.");
this.resetForm()
}else if(response.data.status === 'fail'){
alert("Message failed to send.")
}
})
}
resetForm(){
this.setState({name: '', email: '',subject:'', message: ''})
}

Back-End: Nodemailer

Install npm install express, npm install cors and npm install nodemailer. Then, create a sendMail.js to handle the back-end.

var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
var cors = require('cors');
nodemailer.createTransport({
host: "smtp.example.com", //replace with your email provider
port: 587,
auth: {
user: "username", //replace with the email address
pass: "password" //replace with the password
}
});
// verify connection configuration
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
router.post('/send', (req, res, next) => {
var name = req.body.name
var email = req.body.email
var subject = req.body.subject
var message = req.body.message
var content = `name: ${name} \n email: ${email} \n subject: ${subject} \n message: ${message} `
var mail = {
from: name,
to: // receiver email,
subject: subject,
text: content
}
transporter.sendMail(mail, (err, data) => {
if (err) {
res.json({
status: 'fail'
})
} else {
res.json({
status: 'success'
})
}
})
})

verclaire nine

Tutorials, devlogs and all things “elegant”

Sign up for verclaire nine daily

By verclaire nine

A dose of verclaire nine's stories just for you! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Victoria Lo

Written by

A nerd in books, tea and programming.

verclaire nine

At verclaire nine, we aim to provide short daily doses of content to brighten your mind.

Victoria Lo

Written by

A nerd in books, tea and programming.

verclaire nine

At verclaire nine, we aim to provide short daily doses of content to brighten your mind.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store