hermaily / hermaily-mailer
Hermaily transport for Symfony Mailer and Laravel: send email through the Hermaily API or SMTP relay, and verify Hermaily webhooks.
Requires
- php: >=8.2
- symfony/http-client: ^6.4 || ^7.0 || ^8.0
- symfony/http-client-contracts: ^3.0
- symfony/mailer: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- orchestra/testbench: ^9.0 || ^10.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^10.5 || ^11.0
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
Suggests
- illuminate/mail: Laravel 11 or 12: the service provider is auto-discovered
- symfony/http-kernel: To register HermailyMailerBundle in a Symfony application
Provides
None
Conflicts
None
Replaces
None
README
Hermaily transport for Symfony Mailer and Laravel, plus a verifier and parser for Hermaily webhooks.
hermaily+api://sends through Hermaily's native API (POST https://api.hermaily.com/v1/send) over HTTPS.hermaily+smtp://sends through the SMTP relay (smtp.hermaily.com, port 587 with STARTTLS, or 465).- Laravel gets a
hermailymail transport through package auto-discovery.
Requirements
- PHP 8.2 or later
- Symfony Mailer 6.4, 7.x or 8.x (Symfony 6.4+ apps, or Laravel 11 and 12, which use Symfony Mailer)
- A Hermaily API key (
hm_...) for a verified domain: https://app.hermaily.com/api-keys
Install
composer require hermaily/hermaily-mailer
Symfony
Register the bundle in config/bundles.php (there is no Flex recipe yet):
return [ // ... Hermaily\Mailer\Symfony\HermailyMailerBundle::class => ['all' => true], ];
Then set the DSN in .env:
MAILER_DSN=hermaily+api://YOUR_API_KEY@default
| DSN | Transport |
|---|---|
hermaily+api://KEY@default or hermaily://KEY@default |
API, https://api.hermaily.com/v1/send |
hermaily+smtp://KEY@default |
SMTP, smtp.hermaily.com:587 (STARTTLS), user apikey |
hermaily+smtps://KEY@default |
SMTP, smtp.hermaily.com:465 (implicit TLS) |
Replace default with a host (and optionally :port) to point elsewhere. With the API transport, ?lang=es returns
error messages in Spanish (default en). URL-encode the key if it contains reserved characters.
Tags and metadata use Symfony's standard headers:
use Symfony\Component\Mailer\Header\MetadataHeader; use Symfony\Component\Mailer\Header\TagHeader; $email = (new Email()) ->from('Acme <hello@acme.example>') ->to('lucia@example.com') ->subject('Your order is on its way') ->html('<p>Hi Lucía, your order 48213 has shipped.</p>'); $email->getHeaders()->add(new TagHeader('shipping')); $email->getHeaders()->add(new MetadataHeader('order_id', '48213')); $sent = $mailer->send($email); // with the transport directly, $sent->getMessageId() is Hermaily's message id
Laravel
The service provider is auto-discovered. Add a mailer to config/mail.php:
'mailers' => [ // ... 'hermaily' => [ 'transport' => 'hermaily', 'key' => env('HERMAILY_API_KEY'), // 'endpoint' => 'api.hermaily.com', // optional host[:port]; always HTTPS // 'lang' => 'en', // language of API error messages: en or es ], ],
and in .env:
MAIL_MAILER=hermaily HERMAILY_API_KEY=hm_...
Mailables work as usual; ->tag('welcome') and ->metadata('user_id', '7') become Hermaily's tags and metadata (call ->tag() several times for several tags).
For SMTP, use Laravel's built-in smtp mailer with host smtp.hermaily.com, port 587, username apikey and your
API key as password.
What is sent
The API transport builds the native request (see https://hermaily.com/openapi.yaml, SendRequest):
Symfony Email |
Hermaily field |
|---|---|
| From (first address) | from |
| envelope recipients without Cc/Bcc, Cc, Bcc | to, cc, bcc |
| Reply-To | reply_to (Hermaily uses the first address) |
| Subject, text body, HTML body | subject, text, html |
| attachments | attachments[] with filename, base64 content and content_type |
inline parts (embed(), asInline()) |
the same, plus content_id (reference it as cid:...) |
TagHeader (one or more) |
tags (up to 10 per message, in order, duplicates removed) |
MetadataHeader |
metadata |
X-Hermaily-Stream: broadcast |
stream |
X-Hermaily-Track-Opens / X-Hermaily-Track-Clicks: true|false |
track_opens / track_clicks |
| any other header | headers |
The key goes in Authorization: Bearer. A 202 answer sets the message id; any other answer throws
HttpTransportException with Hermaily's message, error code and HTTP status, for example
Unable to send an email: Invalid API key (code unauthorized, HTTP 401). Codes: unauthorized, forbidden,
validation, quota_exceeded, rate_limited, internal.
A 202 means accepted, not delivered. If every recipient is on the suppression list, Hermaily still answers 202
(status suppressed) and nothing is sent; follow messages with webhooks.
Over SMTP, each TagHeader becomes an X-Hermaily-Tag header, and X-Hermaily-Stream: broadcast selects the broadcast stream.
The SMTP relay has no metadata field and does not rewrite links (no click tracking); use the API for those.
Webhooks
Hermaily signs every webhook with X-Hermaily-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256(secret, "<t>." + body)>.
WebhookSignatureVerifier checks it over the raw body in constant time and rejects signatures older than 5 minutes
(configurable; 0 disables the check). Every webhook format carries this header, so the verifier works whichever
format you chose in the dashboard. WebhookParser reads the native format into a WebhookEvent.
use Hermaily\Mailer\Webhook\EventType; use Hermaily\Mailer\Webhook\Exception\InvalidSignatureException; use Hermaily\Mailer\Webhook\WebhookParser; use Hermaily\Mailer\Webhook\WebhookSignatureVerifier; $parser = new WebhookParser(new WebhookSignatureVerifier($_ENV['HERMAILY_WEBHOOK_SECRET'])); try { $event = $parser->verifyAndParse(file_get_contents('php://input'), $_SERVER['HTTP_X_HERMAILY_SIGNATURE'] ?? ''); } catch (InvalidSignatureException) { http_response_code(401); exit; } if (EventType::Bounce === $event->eventType()) { // $event->recipient, $event->data['diagnostic'], $event->message->metadata['order_id'] ... } http_response_code(204);
Event types: send, delivery, delivery_delay, bounce, complaint, reject, open, click,
rendering_failure, unsubscribe. Delivery is at least once: use $event->id to discard duplicates.
Symfony. Configure the secret and inject WebhookParser (or WebhookSignatureVerifier) into your controller:
# config/packages/hermaily_mailer.yaml hermaily_mailer: webhook: secret: '%env(HERMAILY_WEBHOOK_SECRET)%' # tolerance: 300
#[Route('/webhooks/hermaily', methods: ['POST'])] public function __invoke(Request $request, WebhookParser $parser): Response { try { $event = $parser->verifyAndParse($request->getContent(), (string) $request->headers->get('X-Hermaily-Signature')); } catch (InvalidSignatureException) { return new Response('', 401); } // ... return new Response('', 204); }
Laravel. Add the secret to config/services.php and exclude the route from CSRF protection (or put it in
routes/api.php):
'hermaily' => [ 'webhook_secret' => env('HERMAILY_WEBHOOK_SECRET'), // 'webhook_tolerance' => 300, ],
Route::post('/webhooks/hermaily', function (Request $request, WebhookParser $parser) { try { $event = $parser->verifyAndParse($request->getContent(), (string) $request->header('X-Hermaily-Signature')); } catch (InvalidSignatureException) { abort(401); } // ... return response()->noContent(); });
To test your endpoint, $verifier->sign($body) returns the header Hermaily would send.
Plain PHP
use Hermaily\Mailer\Transport\HermailyApiTransport; use Symfony\Component\Mailer\Mailer; use Symfony\Component\Mime\Email; $transport = new HermailyApiTransport($_ENV['HERMAILY_API_KEY']); $sent = $transport->send( (new Email())->from('hello@acme.example')->to('lucia@example.com')->subject('Hello')->text('Hi Lucía') ); echo $sent?->getMessageId(); // or from a DSN, through Symfony's Mailer $transport = (new Symfony\Component\Mailer\Transport([new Hermaily\Mailer\Transport\HermailyTransportFactory()])) ->fromString('hermaily+api://'.$_ENV['HERMAILY_API_KEY'].'@default'); $mailer = new Mailer($transport);
Development
composer install composer test # PHPUnit composer phpstan # PHPStan, level max composer cs # PHP CS Fixer (dry run)
License
MIT. See LICENSE.