PHP
Send your first email from PHP using cURL against the MailBlastr API.
MailBlastr ships an official PHP SDK — see Send emails with PHP for the composer require mailblastr/mailblastr path — but you do not need it: this guide sends email by POSTing JSON to https://www.mailblastr.com/api/emails. The examples below use PHP's built-in cURL extension, which ships with most PHP installs.
Prerequisites
- A MailBlastr API key.
- A verified domain to send from.
- PHP with the
curlextension enabled — any version can run the raw cURL example below. - PHP 8.1 or newer if you install the official
mailblastr/mailblastrpackage, which also needs thejsonextension.
1. Set your API key
Read your API key from the environment rather than hardcoding it. Set MAILBLASTR_API_KEY in your environment (or your framework's .env).
MAILBLASTR_API_KEY=mb_xxxxxxxxx2. Send email using HTML
The easiest way to send an email is with the html field.
<?php
$apiKey = getenv('MAILBLASTR_API_KEY');
$payload = json_encode([
'from' => 'Acme <onboarding@yourdomain.com>',
'to' => ['delivered@mailblastr.dev'],
'subject' => 'hello world',
'html' => '<strong>it works!</strong>',
]);
$ch = curl_init('https://www.mailblastr.com/api/emails');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
// REQUIRED: PHP cURL sends no User-Agent of its own, and the API answers a
// request without one with 403 validation_error before it authenticates.
'User-Agent: my-app/1.0',
],
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($status >= 200 && $status < 300) {
$data = json_decode($response, true);
echo $data['id'];
} else {
echo 'Error: ' . $response;
}id. See the full set of body fields in the Send an email reference.Laravel / Symfony
In a framework, you can use the HTTP client you already have — Laravel's Http facade or Symfony's HttpClient — pointing at https://www.mailblastr.com/api/emails with the same Authorization: Bearer header. The request body shape is identical to the cURL example above. See Send emails with PHP & Laravel for the framework walkthrough, or Send emails with PHP to use the official package instead.