How to Send Email with PHP

Use this cURL example to send a transactional email from any PHP app.

PHP cURL example


$apiKey = getenv('SENDWICH_API_KEY');

$payload = [
    'from' => 'Acme ',
    'to' => ['customer@example.com'],
    'subject' => 'Your receipt',
    'html' => '

Thanks for your order.

', 'text' => 'Thanks for your order.', ]; $ch = curl_init('https://sendwich.dev/api/v1/message'); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer '.$apiKey, 'Content-Type: application/json', ], CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR), ]); $responseBody = curl_exec($ch); $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($statusCode !== 201) { throw new RuntimeException('Sendwich request failed: '.$responseBody); }

Recommended next step

Add this send logic behind your own service class so your controllers and jobs stay clean.