Go SMTP Serverless Function for Vercel & iCloud+
A simple, secure, and serverless Go function designed for deployment on Vercel. It acts as an API backend for a static website's contact form, sending emails through an iCloud+ custom domain email address.
Purpose
This project provides a lightweight backend solution for static sites (like those built with Svelte, React, or plain HTML/JS) that need a "Get In Touch" or contact form. It avoids the need for a dedicated server or third-party email services by leveraging the SMTP server provided with an iCloud+ subscription.
Features
- Serverless: Deploys as a single function on Vercel's free tier.
- Secure: Uses environment variables for credentials and an app-specific password, keeping secrets out of the code.
- CORS Protected: Includes middleware to only allow requests from your specified domains.
- Lightweight: Written in Go for fast cold starts and minimal resource usage.
- Direct Reply: Sets the
Reply-To
header, so you can reply directly to the person who contacted you.
Setup and Deployment
Follow these steps to get your email API live.
1. Prerequisites
- An iCloud+ subscription with a custom domain configured.
- A Vercel account.
- Go installed on your machine.
2. Generate an iCloud App-Specific Password
You cannot use your regular Apple ID password. You must generate one for this application.
- Sign in to appleid.apple.com.
- Go to Sign-In and Security > App-Specific Passwords.
- Click "Generate an app-specific password".
- Give it a label (e.g.,
Vercel Contact Form
) and copy the generated password (xxxx-xxxx-xxxx-xxxx
). Save it somewhere safe.
3. Clone and Configure the Project
Clone this repository to your local machine.
git clone https://github.com/andrinoff/emails.git
cd emails
The project is structured for Vercel. The function code is located at /api/andrinoff/index.go
.
To use, you must configure the following:
- Change the name of the folder to whatever you want your endpoint to be located at. For example, if the folder is named
email
, the endpoint will behttps://<your-project-name>.vercel.app/api/email
. - Update the CORS middleware settings (
line 25
in the Go file) to your domain(s). (This is very important!) - Change the `from` and `to` email addresses at lines 79 and 81 in the Go file.
4. Set Environment Variables on Vercel
This is the most critical step. In your Vercel project dashboard, go to Settings > Environment Variables and add the following:
Key | Value | Description |
---|---|---|
ICLOUD_AUTH_USER |
Your primary Apple ID (e.g., [email protected] ) |
The email used to log in to Apple's SMTP server. |
ICLOUD_APP_SPECIFIC_PASSWORD |
The password you generated in Step 2 | The secure password for this application. |
5. Deploy
Deploy the project to Vercel. If you have the Vercel CLI, you can run:
vercel --prod
Alternatively, push the repository to GitHub and link it in the Vercel dashboard. After setting the environment variables, Vercel will automatically build and deploy the function.
API Usage
Once deployed, your function will be available at a URL like https://<your-project-name>.vercel.app/api/<folder-name>
.
- Method:
POST
- Headers:
Content-Type: application/json
- Request Body (JSON):
{
"name": "John Doe",
"email": "[email protected]",
"content": "Hello, I would like to get in touch!"
}
Example Frontend Fetch Request
Here is how you can call the API from your website's JavaScript:
async function submitContactForm(name, email, content) {
const endpoint = 'https://<your-project-name>.vercel.app/api/sendmail';
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, email, content }),
});
const result = await response.json();
if (!response.ok) {
throw new Error(result.message || 'An error occurred.');
}
console.log('Success:', result.message);
// Display success message to the user
} catch (error) {
console.error('Error:', error.message);
// Display error message to the user
}
}