How to Send Email with Laravel

Integrate Mailsurge into a Laravel app using the HTTP client — from local config through queued production sends.

Before you start

You need a verified domain and API key in the Mailsurge dashboard. The from address must use that domain. See the API docs for field formats and response codes.

Add your API key to config

Store the key in .env and expose it through config/services.php so config caching works in production.


                                                1
                                // config/services.php
                                                2
                                'mailsurge' => [
                                                3
                                    'key' => env('MAILSURGE_API_KEY'),
                                                4
                                ],
                    

Send with the HTTP client

Use Laravel's built-in HTTP client with a Bearer token. A 201 response means the message was accepted and queued for delivery.


                                                1
                                use Illuminate\Support\Facades\Http;
                                                2
                                
                                                3
                                $response = Http::withToken(config('services.mailsurge.key'))
                                                4
                                    ->asJson()
                                                5
                                    ->post('https://mailsurge.dev/api/v1/message', [
                                                6
                                        'from' => 'Acme <no-reply@yourdomain.com>',
                                                7
                                        'to' => ['customer@example.com'],
                                                8
                                        'subject' => 'Welcome to Acme',
                                                9
                                        'html' => '<p>Your account is ready.</p>',
                                                10
                                        'text' => 'Your account is ready.',
                                                11
                                    ]);
                                                12
                                
                                                13
                                if (! $response->successful()) {
                                                14
                                    report(new \RuntimeException('Mailsurge send failed: '.$response->body()));
                                                15
                                }
                    

Production checklist

Move sends off the request cycle and plan for failures before you ship.

  • Dispatch sends from a queued job so HTTP timeouts never block users.
  • Use verified domains for every from address.
  • Log non-2xx responses and retry transient errors with backoff.
  • Consider the official mailsurge/laravel package for a typed client.

Continue reading

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

Sending Email with PHP

Framework-agnostic cURL example for any PHP app.

Read guide

Email Deliverability Checklist

Verify DNS and monitoring before going live.

Read guide
Need examples in other languages? See guides for Node.js, Python, PHP, and Ruby.

Ready to send your first email?

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