Symfony
Send your first email from a Symfony controller using the Symfony HttpClient against the MailBlastr API.
PHP has an official SDK — composer require mailblastr/mailblastr — and it works in any Symfony controller or service (Mailblastr::client($key), then $mailblastr->emails->send([ ... ])). The example below instead shows the dependency-free route, POSTing JSON to https://www.mailblastr.com/api/emails with Symfony's HttpClient component, which you can inject into any controller or service.
Prerequisites
- A MailBlastr API key.
- A verified domain to send from.
- A Symfony application.
1. Install the HTTP client
Install the Symfony HttpClient component with Composer.
composer require symfony/http-client2. Configure your API key
In your .env.local file, which you can create if needed, add your MailBlastr API key. Never commit this file.
MAILBLASTR_API_KEY=mb_xxxxxxxxx3. Send your first email
Inject the HttpClientInterface into a controller and POST the email body. The easiest way to send is with the html field.
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class EmailController
{
public function __construct(
private readonly HttpClientInterface $client,
) {
}
#[Route('/send', name: 'send_email')]
public function send(): JsonResponse
{
$response = $this->client->request('POST', 'https://www.mailblastr.com/api/emails', [
'auth_bearer' => $_ENV['MAILBLASTR_API_KEY'],
'json' => [
'from' => 'Acme <onboarding@yourdomain.com>',
'to' => ['delivered@mailblastr.dev'],
'subject' => 'Hello world',
'html' => '<strong>it works!</strong>',
],
]);
return new JsonResponse($response->toArray());
}
}id. See Send an email for the complete request body.