Last updated: August 29, 2026.
An HTTP email API can return structured errors, message IDs, templates, and delivery webhooks. The endpoint and payload vary, but the transport pattern is consistent.
Call an email API with cURL
<?php
$payload = ['to' => [['email' => '[email protected]']], 'subject' => 'Receipt', 'text' => 'Ready'];
$ch = curl_init('https://api.mail-provider.example/v1/messages');
curl_setopt_array($ch, [
CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('MAIL_API_TOKEN'), 'Content-Type: application/json', 'Idempotency-Key: order-1042-receipt'],
CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR),
]);
$response = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($response === false || $status < 200 || $status >= 300) throw new RuntimeException('Mail API failed.');
curl_close($ch);Track delivery
- Use the provider’s documented payload.
- Store the returned message ID.
- Retry only temporary failures.
- Validate webhook signatures before updating status.
Make the request safe to retry
A network timeout can occur after the provider accepted the message. Use an idempotency key or store a unique logical-message key so retrying does not send a duplicate receipt or notification.
Test a timeout, rate limit, validation error, and provider-side failure. Store the returned message ID and validate webhook signatures before changing delivery status.
- Set connect and total timeouts.
- Retry only temporary responses.
- Keep API tokens in a secret store.
Keep resource limits explicit and make failure cleanup part of the example. Log enough context to diagnose the operation without recording passwords, private message bodies, or uploaded file contents.
Continue with queued delivery, delivery troubleshooting, and SMTP delivery.
Practical implementation check
Put the operation in a small function or service with explicit inputs and a clear failure result. Test invalid input, missing extensions, permission failures, and concurrent requests. Production logs should identify the operation and record ID without storing secrets or private content. Clean up partially created files or queue records when any later step fails.
Reference: PHP cURL reference.