Send emails with PHP & Laravel
Call the MailBlastr REST API from Laravel with the Http facade, or from plain PHP with cURL.
MailBlastr ships an official PHP package — composer require mailblastr/mailblastr. If you want it, start at Send emails with PHP, which installs the package and sends a first email with $mailblastr->emails->send(...).
This guide shows the raw-HTTP alternative instead: Laravel ships a clean HTTP client (the Http facade, built on Guzzle) that makes calling the REST API a few lines, and plain PHP works with the built-in cURL extension. Both call POST /emails with your mb_ Bearer token. The official package works the same inside Laravel — resolve it from the container or construct it in a service class.
Prerequisites
- A verified domain for your
fromaddress. (guide) - An API key (
mb_...), stored in your.envasMAILBLASTR_API_KEY. (Authentication) - Laravel 9+ (for the
Httpfacade) — or any PHP 8.1+ with thecurlandjsonextensions enabled. PHP 8.1 is the floor declared by the officialmailblastr/mailblastrpackage; the raw-cURL path shown here also runs on PHP 8.0.
Laravel (Http facade)
Reference the key from config/env, not inline. The Http facade lets you inspect the status and pull JSON fields directly.
<?php
use Illuminate\Support\Facades\Http;
$response = Http::withToken(env('MAILBLASTR_API_KEY'))
->acceptJson()
->post('https://www.mailblastr.com/api/emails', [
'from' => 'Acme <hello@yourdomain.com>',
'to' => ['delivered@mailblastr.dev'],
'subject' => 'Hello from Laravel',
'html' => '<p>Sent with the Http facade.</p>',
]);
if ($response->failed()) {
// MailBlastr returns { statusCode, name, message }.
abort($response->status(), $response->json('message'));
}
$id = $response->json('id');Plain PHP (cURL)
Without a framework, the cURL extension sends the same request.
<?php
$apiKey = getenv('MAILBLASTR_API_KEY');
$ch = curl_init('https://www.mailblastr.com/api/emails');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
// cURL sends no User-Agent by default and the API rejects that with 403.
'User-Agent: my-app/1.0',
],
CURLOPT_POSTFIELDS => json_encode([
'from' => 'Acme <hello@yourdomain.com>',
'to' => ['delivered@mailblastr.dev'],
'subject' => 'Hello from PHP',
'html' => '<p>Sent with cURL.</p>',
]),
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($body, true);
if ($status >= 400) {
throw new RuntimeException("MailBlastr {$data['name']}: {$data['message']}");
}
echo "Sent email {$data['id']}\n";Handling the response
A successful send returns HTTP 200 with { "id": "..." }. Errors return a non-2xx status with a { statusCode, name, message } body — check the status ($response->failed() or the cURL HTTP_CODE) before reading the id. Keep the id to retrieve the email or correlate webhook events.
mb_ API key in your server's .env (or secrets manager) and read it via env()/getenv(). Never hard-code it, commit it, or output it to a Blade view or browser — the key sends mail as you.Next steps
- Prefer the official package? Send emails with PHP installs
mailblastr/mailblastrand does the same send in three lines. - See the full Send Email API reference.
- Read Authentication for key permissions.
- Start from the Quickstart.