Send email with PHP

Plain PHP with cURL, no framework and no dependency.

You need a verified sending domain and an API key from Settings › API Keys. If neither exists yet, the quickstart covers both in a couple of minutes.

Send

<?php
 
$payload = json_encode([
    'from' => 'Acme <[email protected]>',
    'to' => '[email protected]',
    'subject' => 'Welcome',
    'html' => '<p>You are in.</p>',
    'idempotencyKey' => 'welcome-sara-001',
]);
 
$ch = curl_init('https://sending.dev/api/v1/emails');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('SENDING_API_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => $payload,
]);
 
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
 
echo $status, ' ', $body, PHP_EOL;

A successful call answers 202 with the message id: accepted and queued, not yet delivered. Delivery, opens, bounces and complaints arrive later, on webhooks or in the dashboard.

What changes in PHP

202 means accepted and queued, not delivered. Delivery, bounces and complaints arrive later on your webhook.

The thing that catches people

Do not send from a page render on shared hosting: an HTTP call inside the request makes page time depend on our latency. Queue it, or at least send after the response has been flushed.

Next

  • All the fields: cc and bcc, reply-to, attachments, templates, scheduling.
  • Webhooks: delivery, bounce and complaint events, signed with HMAC.
  • Domains: SPF, DKIM and MAIL FROM, and why sending is refused until they are in place.
  • MCP: the same operations as tools, when the one writing the code is an agent.

Nearby: PHP and Ruby

All 30 stacks