Send email with Laravel
A server-side call to the SendSend API from your Laravel app.
- Create a test API key in the dashboard first: it never delivers real email. Switch to a live key once your domain is verified.
- Add SENDSEND_API_KEY to .env and expose it in config/services.php as sendsend.key.
- Add this to
app/Http/Controllers/MailController.php:
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class MailController extends Controller
{
public function send(Request $request)
{
$response = Http::withToken(config('services.sendsend.key'))
->withHeaders(['Idempotency-Key' => (string) Str::uuid()])
->post('https://api.sendsend.email/v1/emails', [
'from' => 'Acme <hello@mail.acme.com>',
'to' => [$request->input('to')],
'subject' => 'Hello from Laravel',
'html' => '<p>It works!</p>',
]);
return response()->json($response->json(), $response->status());
}
}Prefer sending from a queued job so a slow response never holds up a web request.
Good to know
- A
202response means the message was queued, not delivered. See message states and webhooks. - Always send an Idempotency-Key, so a retry cannot send twice.
- Check the response status. Errors have one shape: see Errors.
- More options: attachments, scheduling, headers, templates and React Email.