Email delivery via HTTP provider with context, timeout, and idempotency

8925
0

For transactional email, the reliability problem is usually latency and retries, not MIME formatting. I prefer an HTTP email provider because requests are easy to bound with context.WithTimeout and easier to observe than raw SMTP. The code below builds a JSON payload, sets Authorization and an Idempotency-Key, and sends it through a tuned http.Client. The idempotency key is important: if a job retries after a timeout, the provider can dedupe the send instead of delivering duplicates. I also treat non-2xx responses as errors and avoid logging the full email body to prevent leaking PII. In production, this call lives in a background worker and the API request returns quickly, but the same pattern applies: timeouts, explicit headers, and clear error handling keep email from turning into a tail-latency trap.