teneo-laravel maintained by teneo
Teneo for Laravel
Send mail from Laravel through the Teneo relay, and receive signed delivery events back in near real-time.
- A
teneomail driver, over SMTP (the default) or over the Teneo submission API. - Tags and metadata on every message, using the normal
Mailablemethods. - A webhook endpoint that verifies the signature and fires one Laravel event per delivery event.
php artisan teneo:checkto test your relay login without sending any mail.
Teneo has no API key. Your relay login and its password are your credentials, for SMTP and for the API.
Requirements
- PHP 8.2 or higher
- Laravel 11, 12 or 13 (Laravel 13 needs PHP 8.3 or higher)
- A relay login from Teneo, which looks like
mailclass-<name>@tlsrelay.teneo.be
Installation
composer require teneo/teneo-laravel
The service provider is discovered automatically. Add your credentials to .env:
MAIL_MAILER=teneo
TENEO_RELAY_LOGIN=mailclass-example@tlsrelay.teneo.be
TENEO_RELAY_PASSWORD=your-relay-password
TENEO_WEBHOOK_SECRET=whsec_...
TENEO_WEBHOOK_SECRET is only needed when you want to receive webhook events (see below).
Then add the mailer to config/mail.php:
'mailers' => [
'teneo' => [
'transport' => 'teneo',
],
],
That is all. Any Mail::to(...)->send(...) or notification that uses the default mailer now goes through Teneo. To
use it next to another mailer, leave MAIL_MAILER alone and call Mail::mailer('teneo').
To change the config file, publish it:
php artisan vendor:publish --tag=teneo-config
SMTP or API
By default the driver talks SMTP to tlsrelay.teneo.be on port 587 with STARTTLS, and logs in with your relay login
and password. Your server must be allowed to make outgoing connections on port 587.
To send over the submission API instead (HTTPS on port 443, useful when port 587 is blocked), set via:
'teneo' => [
'transport' => 'teneo',
'via' => 'api',
],
The API uses the same login and password. The package posts the complete message to the API, and a message is only
counted as sent when the API says it accepted it. If it does not, you get a TransportException with the error and the
reason the API gave. Both ways support Cc and Bcc, and a Bcc recipient never appears in the message headers.
A mailer entry may also carry its own login and password keys, which then win over the values in
config/teneo.php. Use this when one application sends through two relay logins.
Tags and metadata
Tags and metadata are your own labels on a message. Teneo gives them back in every webhook event, so you can match an event to an order, a user or a campaign.
use Illuminate\Mail\Mailables\Envelope;
public function envelope(): Envelope
{
return new Envelope(
subject: 'Your order',
tags: ['order', 'shipping'],
metadata: ['customer' => 'c-1042', 'order' => '4821'],
);
}
The fluent form works as well: $mailable->tag('order')->metadata('customer', 'c-1042').
The package sends them as one X-Tag header (tags joined by commas) and one X-Metadata header (one JSON object).
Two limits apply, and the package throws a TransportException before sending when they are broken:
- A tag cannot contain a comma.
- The metadata, encoded as JSON, can be at most 986 bytes.
If you add an X-Tag or X-Metadata header yourself and use neither tag() nor metadata(), your header is sent as it is.
Webhooks
Teneo posts the delivery events of your messages to an HTTPS address of your choice, in near real-time, as signed batches. The package registers the endpoint for you:
POST https://your-app.example/teneo/webhook
- In the Teneo console, add a webhook endpoint with that address.
- Copy the secret that the console shows once (it starts with
whsec_) intoTENEO_WEBHOOK_SECRET. - Press "Send test event" in the console. Your application receives a
WebhookTestReceivedevent.
The route has no session and no CSRF check. The signature is the authentication: a request without a valid signature
gets a 4xx answer and nothing is dispatched. If TENEO_WEBHOOK_SECRET is not set, the endpoint answers 500 and logs
the problem, it never accepts unsigned requests. A request whose timestamp is more than 5 minutes off is refused.
While you replace a secret, Teneo signs with the old and the new secret for 24 hours, and the package accepts both.
A correctly signed batch always gets a 204 answer, whatever is inside. A malformed event in a batch is logged and skipped. This matters: Teneo disables an endpoint that keeps answering with errors for 5 days. Your listeners run after the response is sent, and an exception in one listener does not stop the others.
Settings in .env:
TENEO_WEBHOOK_ENABLED=true # false leaves the route out
TENEO_WEBHOOK_PREFIX=teneo # the address becomes /<prefix>/webhook
Events
| Laravel event | When it fires |
|---|---|
Teneo\Laravel\Events\MessageDelivered |
The receiving server accepted the message. |
Teneo\Laravel\Events\MessageBounced |
The message bounced. |
Teneo\Laravel\Events\MessageExpired |
Teneo stopped trying after the retry period. |
Teneo\Laravel\Events\MessageDropped |
Teneo did not send the message, for example because the recipient is blocked. |
Teneo\Laravel\Events\MessageComplained |
The recipient reported the message as spam. |
Teneo\Laravel\Events\WebhookTestReceived |
The test event of the console. |
Teneo\Laravel\Events\UnknownEventReceived |
An event type this version does not know yet. Nothing breaks when Teneo adds one. |
Every event has one public property, $event, a read-only Teneo\Laravel\Webhook\DeliveryEvent:
| Property | Content |
|---|---|
id |
The event id. |
type |
The event type, for example message.delivered. |
time |
When it happened, a DateTimeImmutable. |
recipient |
The recipient address, or null. A complaint has no recipient address. |
messageId |
The Message-ID of the message without angle brackets, or null. |
relayAccount |
The name of your relay account, or null. |
tags |
The tags you set, a list of strings. |
metadata |
The metadata you set, an array. |
headers |
The other message headers Teneo returned. |
category, permanent, suppress |
For bounces and expired messages: the bounce category, whether it is permanent, whether Teneo suppresses the address. |
smtpResponse |
The response of the receiving server, when there is one. |
reason |
For drops: why the message was dropped. |
payload |
The event exactly as Teneo sent it. |
The message id is the same value that SentMessage::getMessageId() gives you when you send, so you can store it at send time
and find the message again when an event arrives. A complaint carries no recipient, so use the message id or your metadata.
A listener
use Illuminate\Support\Facades\Event;
use Teneo\Laravel\Events\MessageBounced;
// in a service provider, or as a listener class that Laravel discovers
Event::listen(MessageBounced::class, function (MessageBounced $e) {
if ($e->event->permanent) {
User::where('email', $e->event->recipient)->update(['email_valid' => false]);
}
logger()->info('Bounce', [
'order' => $e->event->metadata['order'] ?? null,
'category' => $e->event->category,
'response' => $e->event->smtpResponse,
]);
});
A listener class can implement ShouldQueue as usual to do its work on a queue.
Check your setup
php artisan teneo:check
The command sends no mail. It checks that the relay login has the right shape, that port 587 of tlsrelay.teneo.be
answers, that the connection upgrades to TLS and that the login and password are accepted. If something is wrong it says
in plain words what, for example a wrong password or a firewall that blocks port 587. It also notes whether a webhook
secret is set. The exit code is 0 when everything is fine and 1 otherwise.
When the package is installed but not configured
Installing the package never breaks your application. If the login or password is missing, the error appears when the
teneo mailer is first used, and it names the two .env keys to set.
Testing
composer install
vendor/bin/phpunit
The tests never open a connection.
License
MIT. See LICENSE.