Overview
The contact form uses EmailJS to send emails directly from the client-side without requiring a backend server. This guide walks through the complete setup process.How It Works
The contact form insrc/components/Contact/Contact.jsx uses three EmailJS credentials:
Contact.jsx
EmailJS Setup Guide
Create EmailJS Account
Visit EmailJS and sign up for a free account.The free tier includes:
- 200 emails per month
- Unlimited templates
- All features
Add Email Service
- Go to the Email Services section in your dashboard
- Click Add New Service
- Choose your email provider (Gmail, Outlook, etc.)
- Follow the provider-specific instructions to connect your email
- Copy your Service ID (you’ll need this later)
For Gmail, you may need to create an App Password if you have 2FA enabled.
Create Email Template
- Navigate to Email Templates
- Click Create New Template
- Use this template structure:
- Save the template and copy your Template ID
Environment Variables Setup
EmailJS credentials are stored in a.env file for security.
Create .env File
In the root of your project (same level as
package.json), create a file named .env:Add EmailJS Credentials
Open Replace with your actual values from EmailJS.
.env and add your credentials:.env
Vite requires environment variables to be prefixed with
VITE_ to be accessible in the frontend.Add .env to .gitignore
Ensure
.env is in your .gitignore file to keep credentials private:.gitignore
Testing the Contact Form
Fill Out the Form
Enter test data:
- Name: Test User
- Email: [email protected]
- Message: This is a test message
If you don’t receive an email, check:
- EmailJS dashboard for sent emails
- Your spam folder
- That your email service is properly connected
- Browser console for any error messages
Contact Component Code
Here’s the full contact form implementation:Contact.jsx
Customizing Form Fields
Adding New Fields
To add a new field (e.g., phone number):Removing Fields
To remove a field:- Delete the state variable
- Remove the input JSX
- Remove it from the EmailJS send payload
- Update your EmailJS template
Error Handling
The contact form includes validation and error handling:Form Validation
Contact.jsx
EmailJS Error Handling
Contact.jsx
Custom Error Messages
Enhance error handling with specific messages:Email Validation
The form uses HTML5 email validation viatype="email":
Changing Success/Error Messages
Customize user feedback:Consider using a toast notification library like
react-hot-toast or react-toastify for better UX instead of browser alerts.Production Deployment
When deploying to production:Set Environment Variables on Host
Add your EmailJS credentials to your hosting platform’s environment variables:Vercel:
- Go to Project Settings → Environment Variables
- Add
VITE_EMAILJS_SERVICE_ID,VITE_EMAILJS_TEMPLATE_ID,VITE_EMAILJS_PUBLIC_KEY
- Go to Site Settings → Build & Deploy → Environment
- Add the same variables
Troubleshooting
| Issue | Solution |
|---|---|
| ”Message sent!” but no email received | Check EmailJS dashboard for logs. Verify email service connection. |
undefined error for env variables | Ensure variables start with VITE_. Restart dev server. |
| Form submits but shows error | Check browser console for detailed error. Verify all three credentials are correct. |
| ”403 Forbidden” error | Public Key is incorrect. Verify in EmailJS account settings. |
| Email goes to spam | Add a “Reply-To” header in EmailJS template using {{email}}. |
Security Best Practices
Never Commit .env
Always keep
.env in .gitignore to prevent credential exposureUse Public Key Only
Never expose your EmailJS Private Key in frontend code
Rate Limiting
EmailJS has built-in rate limiting to prevent abuse
Input Sanitization
EmailJS automatically sanitizes inputs to prevent injection attacks
Alternative: Custom Backend
If you prefer not to use EmailJS, you can build a custom backend:Next Steps
Deployment Guide
Deploy your portfolio to production
Content Customization
Personalize your portfolio content