Skip to content

fatonyahmadfauzi/Gmail-Services

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Email API Service for Contact Forms (Resend + Vercel)

🌐 Available in other languages: Bahasa Indonesia | Français | Deutsch | 日本語 | 中文 | Español | Polski | Русский | Português | 한국어

This is a simple backend project that serves as an email sending service for contact forms. It is built using Node.js and the Resend email service, designed to be deployed as a Serverless Function on Vercel.

This solution is a simpler and more modern alternative to Gmail with OAuth2, requiring only an API key for authentication.


✨ Features

  • Single API Endpoint: Accepts POST requests containing form data (name, email, message).
  • Reliable Email Delivery: Uses Resend to send transactional emails.
  • Ready for Vercel Deployment: Configured as a serverless function for scalability and ease of deployment.
  • CORS Handling: Includes built-in CORS configuration to allow requests from your frontend domain.
  • JSON Response: Provides clear status responses (success or error) for seamless frontend integration.

📦 Technologies Used

  • Node.js (ES Modules)
  • Resend
  • Vercel Serverless Function

🚀 Setup and Deployment Guide

Follow these steps to get your email service up and running.

1. Get Your Resend Credentials

Before deploying, you’ll need an API Key and a Verified Domain from Resend.

  1. Create an Account: Sign up for a free account at resend.com.
  2. Verify Your Domain:
    • In the Resend dashboard, go to "Domains".
    • Add the domain you’ll use to send emails (e.g., your portfolio domain).
    • Follow the instructions to add DNS records (typically MX and TXT) in your domain provider or DNS manager (like Netlify or Cloudflare).
  3. Create an API Key:
    • Go to "API Keys".
    • Click "Create API Key", give it a name, and set Permission to "Full Access".
    • Copy and save the API Key — it will only be shown once.

2. Deploy the Project to Vercel

  1. Clone or Fork This Repository: Get a copy of this project in your GitHub account.
  2. Import into Vercel:
    • Log in to Vercel and click "Add New... > Project".
    • Select your GitHub repository. Vercel will automatically detect it as a Node.js project.
  3. Set Environment Variables:
    • In your Vercel project settings, go to "Environment Variables".
    • Add the following variable:
      • Name: RESEND_API_KEY
      • Value: (Paste your Resend API key, starting with re_...)
  4. Deploy: Click "Deploy" to launch your project.

Once deployment completes, Vercel will provide a project URL (e.g., https://your-project-name.vercel.app).


📨 API Documentation

Your API endpoint will be available at your Vercel deployment URL followed by the API path.

  • URL: https://your-project-name.vercel.app/api/handle-form
  • Method: POST
  • Headers: Content-Type: application/json

Example Request Body (JSON)

{
  "name": "John Doe",
  "email": "john@example.com",
  "message": "Hello! I’m interested in collaborating with you."
}

Example Response

✅ Success (Status 200)

{
  "status": "success",
  "message": "Message sent successfully!"
}

❌ Error (Status 400/500)

{
  "status": "error",
  "message": "All fields are required."
}

🔌 Frontend Integration Example

Use fetch in your frontend JavaScript code to send form data to your deployed API endpoint.

// Example frontend implementation
const form = document.getElementById("contactForm");

form.addEventListener("submit", async (event) => {
  event.preventDefault();

  const apiUrl = "https://your-project-name.vercel.app/api/handle-form";
  const formData = {
    name: form.name.value,
    email: form.email.value,
    message: form.message.value,
  };

  try {
    const response = await fetch(apiUrl, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(formData),
    });

    const data = await response.json();

    if (data.status === "success") {
      alert("Message sent successfully!");
      form.reset();
    } else {
      alert("Error: " + data.message);
    }
  } catch (error) {
    console.error("Error:", error);
    alert("Failed to send message. Please try again later.");
  }
});

📄 License

This project is licensed under the MIT License.

About

Node.js contact form API powered by Resend for simple and reliable email delivery on Vercel.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors