This repository contains a SvelteKit application deployed on Vercel, providing an API endpoint to send emails using Nodemailer and Gmail's SMTP server.
This repository does not work on Cloudflare due does not support Nodemailer, that's why we are using Vercel instead, but you can use Netlify or other hosting services compatible with Nodemailer. To use this repository in other hosting services, you need to change the
adapter
insvelte.config.js
to the one that supports your hosting service.
- Send Emails: Send emails by making a
POST
request to the/send
endpoint. - Authorization: Secures the endpoint with a Bearer token.
- JSON Payload: Accepts email details (
from
,to
,subject
,html
) in JSON format.
- Node.js and npm: Install Node.js
- Gmail Account: A Gmail account with App Passwords enabled (if using two-factor authentication)
- Vercel Account: Sign up for Vercel
AUTH_TOKEN=your_authorization_token // Set a random auth token for use it later on your requests
SMTP_USERNAME[email protected]
SMTP_PASSWORD=your_gmail_app_password
git clone https://github.com/your-username/your-repo.git
cd your-repo
npm install
Create a .env
file in the root directory:
AUTH_TOKEN=your_authorization_token
SMTP_USERNAME[email protected]
SMTP_PASSWORD=your_gmail_app_password
Note: Do not commit the .env
file to version control to keep your credentials secure.
If you have two-factor authentication enabled on your Gmail account, you'll need to generate an App Password.
-
Enable Two-Factor Authentication (if not already enabled):
- Go to your Google Account security settings: Google Account Security
- Under "Signing in to Google," select 2-Step Verification and follow the prompts to enable it.
-
Generate an App Password:
- Return to the Google Account Security page.
- Under "Signing in to Google," select App Passwords or type in the search bar App Passwords
- Follow the prompts to enable it
- Google will generate a 16-character app password. Copy this password; you'll need it for the
SMTP_PASSWORD
environment variable.
Important: Keep this app password secure and do not share it.
npm run dev
The application will be available at http://localhost:5173
.
Make a POST
request to http://localhost:5173/send
with the appropriate headers and body.
curl -X POST http://localhost:5173/send \
-H "Authorization: Bearer your_authorization_token" \
-H "Content-Type: application/json" \
-d '{
"from": "[email protected]",
"to": "[email protected]",
"subject": "Hello World",
"html": "<h1>It works!</h1>"
}'
-
Log In to Vercel: Go to Vercel Dashboard and log in.
-
Import Project:
- Click on "New Project".
- Select the Git repository containing your SvelteKit application.
-
Configure Project Settings:
- Framework Preset: Vercel should auto-detect SvelteKit.
- Build Command:
npm run build
- Output Directory:
build
In the "Environment Variables" section, add the following variables:
AUTH_TOKEN
SMTP_USERNAME
SMTP_PASSWORD
Note: Vercel supports different environment variable configurations for development, preview, and production environments.
- Click "Deploy" to start the deployment process.
-
URL:
https://your-pages-domain/send
-
Method:
POST
-
Headers:
Authorization: Bearer your_authorization_token
Content-Type: application/json
-
Body:
{ "from": "[email protected]", "to": "[email protected]", "subject": "Hello World", "html": "<h1>It works!</h1>" }
- 200 OK: Email sent successfully.
- 400 Bad Request: Missing required fields or invalid JSON.
- 401 Unauthorized: Missing or malformed
Authorization
header. - 403 Forbidden: Invalid authorization token.
- 500 Internal Server Error: An error occurred on the server.
- Gmail SMTP: Ensure that you have enabled App Passwords if you have two-factor authentication enabled.
- Security: Never expose your SMTP credentials in the code or commit them to version control.
- Multiple Recipients: The
to
field can be a single email address or an array of email addresses. - Email Limits: Gmail has sending limits; for higher volumes, consider using a professional email service.
- Authentication Issues: Verify your SMTP credentials and ensure App Passwords are used if necessary.
- Email Delivery Problems: Check your Gmail account for any security alerts or issues.
- Environment Variables: Ensure environment variables are correctly set both locally and in Vercel.
- Log in to your Gmail account.
- Click on the gear icon ⚙️ in the top-right corner.
- Select "See all settings" from the dropdown menu.
- Click on the "Accounts and Import" tab at the top.
- In the "Send mail as" section, click on "Add another email address".
- Name: Enter the name you want recipients to see.
- Email address: Enter your custom domain email (e.g.,
[email protected]
). - Treat as an alias: Keep this checked if you want to receive replies in Gmail.
- Click "Next Step".
- SMTP Server:
smtp.gmail.com
- Username: Your full Gmail address (e.g.,
[email protected]
). - Password: Your Gmail password or App Password if 2FA is enabled.
- Port:
587
(TLS) or465
(SSL) - Secure Connection: Select "Secured connection using TLS".
- Click "Add Account".
- Gmail will send a verification email to your custom email address.
- Access your custom email inbox (via webmail or email client).
- Open the email from Gmail and click the verification link or enter the provided code in Gmail's settings.
- In Gmail settings under "Send mail as", you can make your custom email the default address by clicking "make default" next to it.
This project is licensed under the MIT License.
Contributions are welcome! Please open an issue or submit a pull request.