Email Proxy API

This Val Town project creates an HTTP API that receives JSON data and forwards it as an email to the val owner.

Setup

  1. HTTP Trigger:

    • The val is configured with an HTTP trigger
    • It accepts POST requests with JSON data
    • On Val Town's free tier, emails automatically go to the val owner
  2. Optional Environment Variable:

    • You can set FORWARD_TO_EMAIL for documentation purposes, but it's not required on the free tier
    • On paid tiers, this would specify where emails should be forwarded

API Usage

Endpoint

POST https://[your-val-url]/

Request Format

{ "subject": "Your email subject", "text": "Your email message\n\nSent at: 2024-01-01T12:00:00Z" }

Example Request

fetch('https://[your-val-url]/', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ subject: "Test Email", text: "This is a test message\n\nSent at: " + new Date().toISOString() }) });

Response Format

Success (200):

{ "success": true, "message": "Email forwarded successfully", "timestamp": "2024-01-01T12:00:00.000Z" }

Error (400/500):

{ "error": "Error description", "message": "Additional details" }

How It Works

  1. Receives POST Request: API accepts JSON data with subject and text fields
  2. Validates Input: Ensures required fields are present and JSON is valid
  3. Formats Email: Creates a nicely formatted email with:
    • Original subject (prefixed with "[API Forward]")
    • Client IP address for security logging
    • Timestamp of when the request was received
    • Original message content
  4. Sends Email: Forwards the formatted email to the val owner (you)
  5. Returns Response: Sends back JSON confirmation or error details

Features

  • ✅ RESTful HTTP API with POST endpoint
  • ✅ JSON request/response format
  • ✅ Input validation and error handling
  • ✅ CORS headers for browser compatibility
  • ✅ Client IP logging for security
  • ✅ Clean HTML formatting for better readability
  • ✅ Error notifications sent to your email
  • ✅ Detailed logging and timestamps

Security

  • Only accepts POST requests
  • Logs client IP addresses
  • Validates all input data
  • Sends error notifications for monitoring

Environment Variables

  • FORWARD_TO_EMAIL: Optional on free tier (emails go to val owner automatically). On paid tiers, this would specify the forwarding address.

File Structure

  • emailProxy.ts: Main email proxy API with HTTP trigger