How to Send Emails from Your React/Next.js App Using EmailJS
Send Emails from React & Next.js Without a Backend
01 What is EmailJS?
EmailJS is a service that lets you send emails directly from client-side JavaScript — no backend required. It bridges your frontend app to your email provider (Gmail, Outlook, custom SMTP) through a clean API, so users can trigger emails from contact forms, feedback widgets, or order requests right from the browser.
02 Why use EmailJS?
Sending email from a frontend is normally painful — you need a server to keep credentials safe. EmailJS removes that friction entirely:
03 Setting up your account
Go to emailjs.com and sign up for free — no credit card needed.
In the dashboard, link Gmail, Outlook, or your own SMTP server as an email service.
Design the email your users will receive — with dynamic variables like {{user_name}} and {{message}}.
Note your Service ID, Template ID, and Public Key — you'll need all three in your code.
04 Install the SDK
Add EmailJS to your project with npm or yarn:
# npm
npm install @emailjs/browser
# yarn
yarn add @emailjs/browser
05 Example React component
Here's a complete, working contact form component using EmailJS:
'use client';
import React, { useRef, useState } from 'react';
import emailjs from '@emailjs/browser';
export default function ContactForm() {
const form = useRef();
const [status, setStatus] = useState('');
const sendEmail = (e) => {
e.preventDefault();
setStatus('Sending...');
emailjs.sendForm(
'YOUR_SERVICE_ID', // from EmailJS dashboard
'YOUR_TEMPLATE_ID', // your email template
form.current,
'YOUR_PUBLIC_KEY' // your public key
)
.then(() => {
setStatus('✓ Message sent!');
form.current.reset();
})
.catch(() => {
setStatus('✕ Failed. Please try again.');
});
};
return (
<form ref={form} onSubmit={sendEmail}>
<input type="text" name="user_name" placeholder="Your name" required />
<input type="email" name="user_email" placeholder="Your email" required />
<textarea name="message" rows={5} placeholder="Your message" required />
<button type="submit">Send Message</button>
<p>{status}</p>
</form>
);
}
06 Important tips
NEXT_PUBLIC_EMAILJS_KEY) instead of hardcoding IDs in production.
Comments
Post a Comment