A simple email API built with Node.js and Nodemailer, deployed on Vercel. This API supports sending emails in HTML or plain text formats, with options for BCC, CC, and attachments.
- Send Emails: Supports both plain text and HTML email formats.
- BCC and CC: Includes options for BCC and CC recipients.
- Attachments: Allows attaching files to the email.
- Cross-Origin Resource Sharing (CORS): Configured to handle requests from different origins.
- Node.js (v14 or higher)
- A Gmail account for sending emails
- Vercel CLI for deployment
-
Clone the Repository
git clone https://github.com/your-username/email-api.git cd email-api -
Install Dependencies
npm install
-
Setup Environment Variables
Create a
.envfile in the root of your project and add your Gmail credentials:GMAIL_USER=your-email@gmail.com GMAIL_PASS=your-app-password
To run the API locally:
-
Start the Development Server
npm run dev
-
Test the API
Use a tool like Postman or cURL to send a POST request to
http://localhost:3000/api/send-emailwith the required fields.
Deploy the API to Vercel:
-
Install Vercel CLI
npm install -g vercel
-
Deploy the Project
vercel
-
Set Environment Variables on Vercel
Go to your Vercel dashboard, navigate to your project, and set the environment variables:
GMAIL_USER=your-email@gmail.comGMAIL_PASS=your-app-password
POST /api/send-email
to(string, required): Recipient email address.cc(string, optional): CC email address.bcc(string, optional): BCC email address.subject(string, required): Subject of the email.message(string, required): Content of the email.isHtml(boolean, optional): Set totruefor HTML content,falsefor plain text. Default isfalse.attachments(array of objects, optional): List of attachment objects withpathproperty. Example:[ { "path": "http://example.com/file.pdf" } ].
cURL:
curl -X POST https://your-deployment-url.vercel.app/api/send-email \
-H "Content-Type: application/json" \
-d '{
"to": "recipient@example.com",
"cc": "cc@example.com",
"bcc": "bcc@example.com",
"subject": "Test Email",
"message": "<h1>This is an HTML email</h1>",
"isHtml": true,
"attachments": [{ "path": "http://example.com/file.pdf" }]
}'- Success: Returns
{ "success": "Email sent successfully" } - Failure: Returns
{ "error": "Error message" }
Contributions are welcome! Please fork the repository and submit a pull request with your changes. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
For any questions or feedback, please reach out to litnitimounsef@gmail.com.