symfony/mobyt-notifier
Symfony Notifier bridge for Mobyt SMS. Configure via MOBYT_DSN with user key, access token, sender, and message quality. Supports MobytOptions to customize message type and other delivery parameters when sending SmsMessage.
Install the package alongside Symfony Notifier (Laravel’s Notifications already includes it):
composer require symfony/notifier mobyt/mobyt-notifier
Add DSN to .env:
MOBYT_DSN=mobyt://USER_KEY:ACCESS_TOKEN@default?from=+391234567890&type_quality=L
USER_KEY, ACCESS_TOKEN, and from with Mobyt credentials.type_quality defaults to L (medium); use N for high priority or LL for low cost.Create a Mobyt SMS Notification:
use Illuminate\Notifications\Notification;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytOptions;
class MobytSmsNotification extends Notification
{
public function via($notifiable)
{
return ['mobyt']; // Requires custom channel registration
}
public function toMobyt($notifiable)
{
$message = new SmsMessage(
recipient: $notifiable->phone,
text: 'Hello from Mobyt!'
);
// Optional: Set Mobyt-specific options
$message->options(
(new MobytOptions())
->messageType(MobytOptions::MESSAGE_TYPE_QUALITY_HIGH)
);
return $message;
}
}
Register the Mobyt Channel (if not auto-detected):
// config/services.php
'notifications' => [
'channels' => [
'mobyt' => [
'dsn' => env('MOBYT_DSN'),
],
],
];
use App\Notifications\MobytSmsNotification;
use App\Models\User;
$user = User::find(1);
$user->notify(new MobytSmsNotification());
Leverage Laravel’s custom notification channels to integrate Mobyt:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Notification;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Transport\Dsn;
public function boot()
{
Notification::extend('mobyt', function ($app) {
$dsn = new Dsn(env('MOBYT_DSN'));
$notifier = new Notifier([$dsn]);
return new class($notifier) implements Illuminate\Notifications\Channel {
public function __construct(private Notifier $notifier) {}
public function send($notifiable, $notification)
{
$message = $notification->toMobyt($notifiable);
$this->notifier->send($message);
}
};
});
}
Use MobytOptions for Mobyt-specific features:
$options = (new MobytOptions())
->messageType(MobytOptions::MESSAGE_TYPE_QUALITY_HIGH)
->reference('ORDER_12345') // Mobyt’s reference ID
->validity(24); // Message validity in hours
$sms->options($options);
For bulk SMS (e.g., marketing campaigns), use Laravel’s queued notifications:
$user->notify(new MobytSmsNotification())->onQueue('mobyt');
mobyt queue in config/queue.php:
'connections' => [
'mobyt' => [
'driver' => 'sync', // Or 'database', 'redis', etc.
'notifier' => true, // Custom logic to batch Mobyt requests
],
],
Handle Mobyt API failures gracefully:
public function send($notifiable, $notification)
{
try {
$message = $notification->toMobyt($notifiable);
$this->notifier->send($message);
} catch (\Exception $e) {
// Fallback to another channel (e.g., email)
$notifiable->notify(new FallbackNotification());
}
}
Mock Mobyt responses in tests:
use Symfony\Component\Notifier\Test\TransportTestCase;
public function testMobytNotification()
{
$transport = new MobytTransport(new Dsn(env('MOBYT_DSN')));
$this->assertInstanceOf(SmsMessage::class, $transport->send(new SmsMessage('+123', 'Test')));
// Assert Mobyt-specific options were applied
$this->assertEquals(
MobytOptions::MESSAGE_TYPE_QUALITY_HIGH,
$transport->getLastMessage()->options()->messageType()
);
}
DSN Configuration Errors:
MOBYT_DSN must include from (sender phone) and type_quality..env:
MOBYT_DSN=mobyt://USER_KEY:ACCESS_TOKEN@default?from=+391234567890&type_quality=L
PHP Version Mismatch:
symfony/notifier to ^6.4 in composer.json:
"require": {
"symfony/notifier": "^6.4",
"mobyt/mobyt-notifier": "^7.4"
}
Recipient Format:
+393451234567). Local formats (e.g., 3451234567) may fail.libphonenumber:
use libphonenumber\PhoneNumberUtil;
use libphonenumber\PhoneNumberFormat;
$phone = PhoneNumberUtil::getInstance()->parse($user->phone, 'IT');
$recipient = $phone->format(PhoneNumberFormat::E164);
Rate Limits and Costs:
TYPE_QUALITY affects delivery speed and cost. N (high) is faster but pricier.L (medium) for bulk messages and N for urgent alerts (e.g., password resets).No Async Support by Default:
// config/queue.php
'connections' => [
'mobyt' => [
'driver' => 'database',
'table' => 'mobyt_jobs',
'notifier' => true,
],
];
Enable Symfony Notifier Debugging:
// config/services.php
'notifier' => [
'debug' => env('APP_DEBUG', false),
];
Check Mobyt API Status:
Validate Phone Numbers:
Custom Transport:
Extend MobytTransport to add features like:
class CustomMobytTransport extends MobytTransport
{
public function __construct(Dsn $dsn, private array $customHeaders = [])
{
parent::__construct($dsn);
}
protected function getHeaders(): array
{
return array_merge(parent::getHeaders(), $this->customHeaders);
}
}
Webhook Integration: Listen to Mobyt’s delivery reports via webhooks:
Route::post('/mobyt/webhook', function (Request $request) {
$event = $request->json()->all();
// Log or process delivery status (e.g., 'delivered', 'failed')
});
Message Templates: Use Mobyt’s template system for consistent formatting:
$options = (new MobytOptions())
->templateId('TEMPLATE_123')
->templateParams(['name'
How can I help you explore Laravel packages today?