EmailJS Integration
This portfolio uses EmailJS to handle contact form submissions without requiring a backend server. EmailJS sends emails directly from the client-side application.
Prerequisites
Create an EmailJS Account
Set up Email Service
Connect your email service (Gmail, Outlook, etc.) in the EmailJS dashboard
Create Email Template
Design your email template with the required template variables
Get API Keys
Copy your Service ID, Template ID, and Public Key from the dashboard
Installation
The EmailJS browser package is already included in the project dependencies:
{
"dependencies" : {
"@emailjs/browser" : "^4.4.1"
}
}
If you need to install it manually:
npm install @emailjs/browser
Environment Configuration
Never commit your EmailJS credentials to version control. Always use environment variables.
Create a .env file in your project root:
VITE_SEVICE_ID = your_service_id
VITE_TEMPLATE_ID = your_template_id
VITE_PUBLIC_KEY = your_public_key
The environment variables must be prefixed with VITE_ to be accessible in Vite applications.
Loading Environment Variables
In your component, load the environment variables using Vite’s import.meta.env:
const SERVICE_ID = import . meta . env . VITE_SEVICE_ID ;
const TEMPLATE_ID = import . meta . env . VITE_TEMPLATE_ID ;
const PUBLIC_KEY = import . meta . env . VITE_PUBLIC_KEY ;
Contact Form Implementation
Import EmailJS
import emailjs from "@emailjs/browser" ;
Form State Management
Set up React state to manage form data and loading state:
import { useState , useRef } from "react" ;
const Contact = () => {
const formRef = useRef ();
const [ form , setForm ] = useState ({
name: "" ,
email: "" ,
message: "" ,
});
const [ loading , setLoading ] = useState ( false );
const handleChange = ( e ) => {
const { name , value } = e . target ;
setForm ({ ... form , [name]: value });
};
};
Email Template Variables
The template variables sent to EmailJS must match your template configuration:
const templateParams = {
from_name: form . name , // Sender's name
to_name: "Ritam Saha" , // Recipient's name
from_email: form . email , // Sender's email
to_email: Email , // Your email constant
message: form . message , // Message content
};
Make sure these variable names match exactly with your EmailJS template placeholders (e.g., {{from_name}}, {{message}}).
Form Submission Handler
const handleSubmit = ( e ) => {
e . preventDefault ();
setLoading ( true );
emailjs
. send (
SERVICE_ID ,
TEMPLATE_ID ,
{
from_name: form . name ,
to_name: "Ritam Saha" ,
from_email: form . email ,
to_email: Email ,
message: form . message ,
},
PUBLIC_KEY
)
. then (() => {
setLoading ( false );
alert ( "Thank you. I will get back to you as soon as possible." );
setForm ({
name: "" ,
email: "" ,
message: "" ,
});
})
. catch (( error ) => {
setLoading ( false );
console . log ( error );
alert ( "Something went wrong. Please try again." );
});
};
Form JSX Structure
< form ref = { formRef } onSubmit = { handleSubmit } className = "flex flex-col gap-8" >
< label className = "flex flex-col" >
< span className = "text-navy-900 dark:text-slate-100 font-medium mb-4" >
Your Name
</ span >
< input
type = "text"
name = "name"
value = { form . name }
onChange = { handleChange }
placeholder = "What's your name?"
required
/>
</ label >
< label className = "flex flex-col" >
< span className = "text-navy-900 dark:text-slate-100 font-medium mb-4" >
Your Email
</ span >
< input
type = "email"
name = "email"
value = { form . email }
onChange = { handleChange }
placeholder = "What's your email?"
required
/>
</ label >
< label className = "flex flex-col" >
< span className = "text-navy-900 dark:text-slate-100 font-medium mb-4" >
Your Message
</ span >
< textarea
rows = "7"
name = "message"
value = { form . message }
onChange = { handleChange }
placeholder = "What do you want to say?"
required
/>
</ label >
< button type = "submit" disabled = { loading } >
{ loading ? "Sending..." : "Send Message" }
</ button >
</ form >
EmailJS Template Setup
In your EmailJS dashboard, create a template with these placeholders:
Subject: New Contact from {{from_name}}
From: {{from_name}}
Email: {{from_email}}
Message:
{{message}}
Troubleshooting
Common Issues
Environment variables not loading
Ensure your .env file is in the project root
Restart your development server after adding environment variables
Check that variable names are prefixed with VITE_
Verify the .env file is not ignored by .gitignore
EmailJS returns 'Unauthorized' error
Verify your Public Key is correct
Check that your EmailJS account is active
Ensure your email service is properly connected
Confirm the Service ID and Template ID match your dashboard
Emails not being received
Check your spam folder
Verify the to_email address in your template
Test your template in the EmailJS dashboard
Check EmailJS usage limits for free tier
Template variables not showing in email
Ensure template variable names match exactly (case-sensitive)
Use double curly braces in template: {{variable_name}}
Check that you’re passing the correct object to emailjs.send()
Debug Mode
Add console logs to debug the submission:
emailjs
. send ( SERVICE_ID , TEMPLATE_ID , templateParams , PUBLIC_KEY )
. then (( response ) => {
console . log ( 'SUCCESS!' , response . status , response . text );
})
. catch (( error ) => {
console . log ( 'FAILED...' , error );
});
Security Best Practices
Important Security Considerations
Environment Variables : Never hardcode API keys in your source code
Public Key : The EmailJS public key is safe to expose on the client-side
Rate Limiting : EmailJS automatically rate-limits requests
Email Validation : Always validate email format on the client-side
CAPTCHA : Consider adding reCAPTCHA for production to prevent spam
Testing
Local Testing
Run your development server and submit a test form
Check EmailJS Dashboard
Monitor your EmailJS dashboard for submission history and errors
Verify Email Receipt
Check that you receive the test email in your inbox
Additional Resources