How to Send Email with PHP

Send transactional email from vanilla PHP with cURL — no framework required.

Prerequisites

Create an API key in the dashboard and verify the domain used in your from address. Set MAILSURGE_API_KEY in your environment before running the script.

PHP cURL example

The example below posts JSON to POST /api/v1/message and throws if the status is not 201.


                                                1
                                $apiKey = getenv('MAILSURGE_API_KEY');
                                                2
                                
                                                3
                                $payload = [
                                                4
                                    'from' => 'Acme <no-reply@yourdomain.com>',
                                                5
                                    'to' => ['customer@example.com'],
                                                6
                                    'subject' => 'Your receipt',
                                                7
                                    'html' => '<p>Thanks for your order.</p>',
                                                8
                                    'text' => 'Thanks for your order.',
                                                9
                                ];
                                                10
                                
                                                11
                                $ch = curl_init('https://mailsurge.dev/api/v1/message');
                                                12
                                curl_setopt_array($ch, [
                                                13
                                    CURLOPT_POST => true,
                                                14
                                    CURLOPT_RETURNTRANSFER => true,
                                                15
                                    CURLOPT_HTTPHEADER => [
                                                16
                                        'Authorization: Bearer '.$apiKey,
                                                17
                                        'Content-Type: application/json',
                                                18
                                    ],
                                                19
                                    CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR),
                                                20
                                ]);
                                                21
                                
                                                22
                                $responseBody = curl_exec($ch);
                                                23
                                $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                                                24
                                curl_close($ch);
                                                25
                                
                                                26
                                if ($statusCode !== 201) {
                                                27
                                    throw new RuntimeException('Mailsurge request failed: '.$responseBody);
                                                28
                                }
                    

Recommended next steps

Wrap this logic in a small service class so controllers stay thin. Add retries for 429 and 5xx responses, and never log full API keys.

For Laravel apps, prefer the dedicated Laravel quickstart and the HTTP client facade.

Continue reading

Related guides to help you ship production-ready transactional email.

Sending Email with Laravel

Config, HTTP client, and queued job patterns.

Read guide

Ready to send your first email?

Verify a domain, create an API key, and call POST /api/v1/message from your stack.