This is a simple Express.js-based API designed to send emails via a contact form submission on my website/portfolio. The API uses Nodemailer to send emails and is configured with CORS and dotenv to handle cross-origin requests and environment variables securely
- Handle POST requests to send emails from the contact form
- Validate input fields (name, email, and message) to ensure all fields are filled
- Send emails using Gmail as the SMTP service (configured via environment variables)
- Respond with success or error messages based on the outcome of the email sending process
- reCAPTCHA verification to protect the contact form from spam and bots
- Rate Limiting to limit the number of requests per user and prevent abuse
- Node.js
- Nodemailer: For email sending
- dotenv: For environment variables
- CORS: To handle cross-origin requests
- express-rate-limit: To implement rate limiting
- node-fetch: To make the HTTP request for reCAPTCHA verification
-
Clone the repository:
git clone https://github.com/Sah2Sah2/API_ContactFormMyWebsite.git
-
Navigate to the project directory:
cd email-api -
Install dependencies:
npm install
-
Create a
.envfile at the root of the project to store your Gmail credentials:EMAIL_USER=your-email@gmail.com EMAIL_PASS=your-email-password RECAPTCHA_SECRET=your-recaptcha-secret
Note: For security, consider using an app-specific password for Gmail rather than your actual Gmail password
-
Run the server:
npm start
This will start the server on port
5000(or any port defined in your environment)
Returns a simple message indicating the API is running
GET http://localhost:5000/{
"message": "Email API is running!"
}Handles contact form submissions and sends an email
{
"name": "Your Name",
"email": "your-email@example.com",
"message": "Hello, I would like to get in touch.",
"captchaResponse": "recaptcha-response-token"
}POST http://localhost:5000/send-email
Content-Type: application/json{
"name": "John Doe",
"email": "john.doe@example.com",
"message": "Hi, I’m interested in your profile. Please get back to me.",
"captchaResponse": "recaptcha-response-token"
}{
"success": "Email sent successfully!"
}{
"error": "Failed to send email"
}- Create an account on Render - free version available
- Create a new web service and link it to your GitHub repository
- Set environment variables (e.g.,
EMAIL_USEREMAIL_PASSandRECAPTCHA_SECRET) in the Render dashboard - Deploy the service, and your API will be available at a URL like
https://your-api-name.onrender.com/
- Make sure your frontend points to the correct backend URL when sending POST requests
- Example:
https://your-api-name.onrender.com/send-email
-Rate Limiting: To prevent abuse, the /send-email route is rate-limited to 2 requests per IP every 30 minutes
-reCAPTCHA: To protect the form from bots, the API requires a valid reCAPTCHA response. Obtain your reCAPTCHA secret key from Google reCAPTCHA.
If the email sending fails, check the following:
- Ensure your Gmail credentials are correct and that you have enabled access for less secure apps if you're using Gmail's SMTP service
- If you're using a service like Render, make sure the environment variables (
EMAIL_USER,EMAIL_PASSandRECAPTCHA_SECRET) are correctly set in the Render dashboard
Before you can use Gmail to send emails via this API, you need to set up your Gmail account to allow access to the service
-
Enable Two-Factor Authentication (2FA) for Better Security:
- It is highly recommended to enable 2FA (two-factor authentication) for your Gmail account to enhance security
- If you have 2FA enabled, you will need to create an app-specific password for use with the API. Follow the next step to generate one
-
Use App-Specific Passwords (Recommended for 2FA users):
- If you have 2-step verification enabled on your Gmail account, you will need to generate an app-specific password to use with the API
- To generate an app-specific password:
- Go to Google Account Settings
- Under Security, find the App passwords section
- Choose Mail as the app and Windows Computer (or any device you're using)
- Google will generate a 16-character password. Copy this password
-
Enable Less Secure Apps (If You Don't Have 2FA Enabled):
- If you do not have 2FA enabled, Gmail's security settings may block access to the SMTP service unless you allow "Less Secure Apps." You can enable this by:
- Go to Less Secure Apps
- Turn on access for less secure apps
Note: It's highly recommended to enable 2FA to increase the security of your account. Using a regular password and allowing less secure apps makes your Gmail account more vulnerable
- If you do not have 2FA enabled, Gmail's security settings may block access to the SMTP service unless you allow "Less Secure Apps." You can enable this by:
-
Update
.envwith your Gmail Credentials:- In your
.envfile, use the generated password (for 2FA) or your regular password if you're not using 2FA
EMAIL_USER=your-email@gmail.com EMAIL_PASS=your-app-specific-password
- In your
-
Ensure IMAP is Enabled (for Gmail):
- In Gmail, go to Settings > Forwarding and POP/IMAP
- Make sure IMAP is enabled, as it is required for sending emails through the SMTP server
- If you're using your regular Gmail password (not recommended), make sure that you are aware of the security risks involved
- Using app-specific passwords is safer and preferred when 2FA is enabled
This project is licensed under the MIT License Copyright (c) [2025] [Sara Battistella]