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.
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.
- 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.
Follow these steps to get your email API live.
- An iCloud+ subscription with a custom domain configured.
- A Vercel account.
- Go installed on your machine.
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.
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,
a) change the name of the folder to whatever you want your endpoint to be located at.
e.g.
if folder is named email
--> https://<your-project-name>.vercel.app/api/email
b) Update CORS middleware (line 25
) settings to your domain(s). (IMPORTANT!)
c) Change email addresses at 79, 81 lines.
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., your-name@icloud.com ) |
The email used to log in to Apple's SMTP server. |
ICLOUD_APP_SPECIFIC_PASSWORD |
The password you generated in Step 2 (e.g., xxxx-xxxx-xxxx-xxxx ) |
The secure password for this application. |
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.
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": "john.doe@example.com", "content": "Hello, I would like to get in touch!" }
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
}
}
This project is licensed under the MIT License. See the LICENSE file for details.