Bird (formerly MessageBird) PHP SDK that lets you send email from your PHP applications is now available for installation.
The messagebird/sdk package is available through Composer and requires PHP 8.2 or later. If you have used the Amazon SES PHP SDK, the approach will feel familiar.
The SDK is synchronous, which means your PHP script waits for the API request to complete before continuing. It works with PSR-18 HTTP clients, including Guzzle and Symfony HttpClient, which Bird says it can discover automatically.
Once the SDK is installed, you can send an email with a few lines of PHP. The basic email call looks like this:
$message = $bird->email->send(
from: 'Bird onboarding@messagebird.dev',
to: ['delivered@messagebird.dev'],
subject: 'Hello from Bird',
html: '
My first Bird email.',
);
The SDK supports batch sending, message retrieval and listing, cancellation, statistics, mailboxes and threads. Beyond email, it provides access to Bird’s SMS, WhatsApp, Verify, contacts, contact properties, audiences, etc.
Another important feature of the SDK is automatic email delivery failure retry. It retries certain temporary failures, including HTTP 429 responses, most 5xx server errors and PSR-18 transport errors. It also honours the server’s Retry-After response when one is provided. The default maximum is two retries.
The SDK also includes webhook verification that allows Bird to send information back to your application when something happens, such as a change in an email’s status.
You can verify an incoming webhook with:
$bird->webhooks->unwrap()
Using Bird PHP SDK to send email
To get started, you need a Bird API key. You can create one under Developers → API keys.
- Set the key as an environment variable: export BIRD_API_KEY=”your-api-key”
- Then install the SDK with Composer:
- composer require messagebird/sdk
- Create a PHP file such as send.php:
<?php
declare(strict_types=1);
require DIR . '/vendor/autoload.php';
use MessageBird\Bird;
$bird = new Bird(getenv('BIRD_API_KEY') ?: '');
$message = $bird->email->send(
from: 'Bird onboarding@messagebird.dev',
to: ['delivered@messagebird.dev'],
subject: 'Hello from Bird',
html: '
My first Bird email.',
);
echo $message->getId(), ' ', $message->getStatus(), "\n";
You can then run the script with:
php send.php
The SDK can be used in a plain PHP script or in Laravel and Symfony projects, but Bird has not released dedicated packages for the two frameworks. You will need to use the standard PHP SDK.
