symfony/notifier
Symfony Notifier lets your app send notifications through multiple channels like email, SMS, chat, and more. It provides a unified API, integrates with many third-party providers, and supports routing, transports, and message formatting for flexible delivery.
Install the Package
composer require symfony/notifier
For Laravel-specific integrations (e.g., Mailer transport), use:
composer require symfony/mailer
Configure Transports
Define transports in config/services.php (or a dedicated notifier.php config file):
'notifier' => [
'dsn' => [
'default' => 'smtp://user:pass@smtp.example.com:587',
'slack' => 'slack://token?channel=general',
'twilio' => 'sms://account_sid:auth_token@twilio.com?from=+1234567890',
],
],
Or use environment variables (recommended):
NOTIFIER_DSN_DEFAULT=smtp://user:pass@smtp.example.com:587
NOTIFIER_DSN_SLACK=slack://token?channel=general
First Notification Send an email via the CLI or a controller:
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Bridge\Mailer\MailerTransport;
$notifier = new Notifier([
new MailerTransport($mailer), // Laravel's built-in mailer
]);
$notifier->send(
new Email('user@example.com', 'Welcome!', 'Hello, this is your first notification.')
);
Key Starting Points
symfony/mailer for email integration.Use the Notifier class to send messages across multiple channels with a single call:
$notifier = new Notifier([
new MailerTransport($mailer),
new SlackTransport('token', 'channel'),
new TwilioTransport('account_sid', 'auth_token', 'from'),
]);
$notifier->send(
new Email('user@example.com', 'Alert', 'Your action is required.'),
new SlackMessage('team', '🚨 Action required: ' . $context),
new Sms('+1234567890', 'Your code: ' . $otp)
);
Fetch recipients from Laravel models (e.g., User) and attach context:
$user = User::find(1);
$notifier->send(
new Email($user->email, 'Your Order Update', view('emails.order_update', ['order' => $user->orders->latest()]))
->priority(Email::PRIORITY_HIGH)
->replyTo('support@example.com')
);
Trigger notifications from Laravel events (e.g., OrderShipped):
// In EventServiceProvider
protected $listen = [
OrderShipped::class => [NotifyOrderShipped::class],
];
// Notification handler
class NotifyOrderShipped implements ShouldQueue {
public function handle(OrderShipped $event) {
$notifier = app(Notifier::class);
$notifier->send(
new Email($event->order->user->email, 'Order Shipped', view('emails.shipped', ['order' => $event->order]))
);
}
}
Leverage Markdown, HTML, or interactive elements (e.g., Slack buttons):
// Slack message with buttons
$slackMessage = new SlackMessage('team', 'Approve this?')
->addButton('Approve', 'https://app.example.com/approve?id=123')
->addButton('Reject', 'https://app.example.com/reject?id=123');
$notifier->send($slackMessage);
// Email with inline HTML
$email = new Email('user@example.com', 'Invoice', '<h1>Your Invoice</h1><p>Total: $99.99</p>');
Use Laravel Queues to avoid blocking requests:
$notifier->send(
new Email('user@example.com', 'Welcome')
->toQueue() // Dispatches to queue
);
Or wrap the Notifier in a job:
class SendWelcomeEmail implements ShouldQueue {
public function handle() {
$notifier = new Notifier([new MailerTransport($mailer)]);
$notifier->send(new Email('user@example.com', 'Welcome'));
}
}
Use Laravel’s built-in mailer with Symfony Notifier:
$notifier = new Notifier([
new MailerTransport($mailer), // Inject Laravel's \Illuminate\Mail\Mailer
]);
Combine with Laravel Queues for async delivery:
$notifier = new Notifier([new MailerTransport($mailer)]);
$notifier->send(
new Email('user@example.com', 'Hello')
->toQueue('notifications') // Custom queue name
);
Bind the Notifier to Laravel’s container in AppServiceProvider:
public function register() {
$this->app->singleton(Notifier::class, function ($app) {
return new Notifier([
new MailerTransport($app->make(\Illuminate\Mail\Mailer::class)),
new SlackTransport(env('SLACK_TOKEN'), env('SLACK_CHANNEL')),
]);
});
}
Use Laravel views with Notifier:
$email = new Email('user@example.com', 'Welcome')
->html(view('emails.welcome', ['name' => 'John']))
->text(view('emails.welcome_text', ['name' => 'John']));
Dynamically create transports based on config:
$transport = TransportFactory::get($dsn);
$notifier = new Notifier([$transport]);
Send to multiple recipients with a single call:
$notifier->send(
new Email(['user1@example.com', 'user2@example.com'], 'Team Update', 'Hello team!')
);
Add files to emails/SMS:
$email = new Email('user@example.com', 'Your File')
->attachment(new \Symfony\Component\Mime\Part\FilePart('path/to/file.pdf'));
Handle incoming webhooks (e.g., Slack events):
use Symfony\Component\Notifier\Bridge\Slack\SlackWebhook;
$webhook = new SlackWebhook($request->getContent());
if ($webhook->isValid()) {
$event = $webhook->getEvent();
// Process event (e.g., store reaction, update DB)
}
Extend for proprietary APIs:
use Symfony\Component\Notifier\Transport\TransportInterface;
class CustomTransport implements TransportInterface {
public function __send(NotificationInterface $notification, array $failedRecipients = []): void {
// Implement custom logic (e.g., HTTP request to your API)
}
}
? for options).
# Wrong: Missing `?` before options
NOTIFIER_DSN_SLACK=slack://tokenchannel=general
# Correct:
NOTIFIER_DSN_SLACK=slack://token?channel=general
TransportFactory::get() to validate DSNs early:
$transport = TransportFactory::get(env('NOTIFIER_DSN_SLACK'));
$email = new Email('invalid-email', 'Test');
if (!$email->isValid()) {
throw new \InvalidArgumentException('Invalid recipient');
}
How can I help you explore Laravel packages today?