This Val Town project creates an HTTP API that receives JSON data and forwards it as an email to the val owner.
-
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
-
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
POST https://[your-val-url]/
{
"subject": "Your email subject",
"text": "Your email message\n\nSent at: 2024-01-01T12:00:00Z"
}
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()
})
});
Success (200):
{
"success": true,
"message": "Email forwarded successfully",
"timestamp": "2024-01-01T12:00:00.000Z"
}
Error (400/500):
{
"error": "Error description",
"message": "Additional details"
}
- Receives POST Request: API accepts JSON data with subject and text fields
- Validates Input: Ensures required fields are present and JSON is valid
- 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
- Sends Email: Forwards the formatted email to the val owner (you)
- Returns Response: Sends back JSON confirmation or error details
- ✅ 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
- Only accepts POST requests
- Logs client IP addresses
- Validates all input data
- Sends error notifications for monitoring
FORWARD_TO_EMAIL: Optional on free tier (emails go to val owner automatically). On paid tiers, this would specify the forwarding address.
emailProxy.ts: Main email proxy API with HTTP trigger