symfony/sms-biuras-notifier
Symfony Notifier bridge for SmsBiuras (smsbiuras.lt). Configure via DSN with UID and API key, set sender (“from”), and optionally enable test_mode (0 real SMS, 1 test). Lets your Symfony app send SMS through SmsBiuras.
Installation
composer require symfony/sms-biuras-notifier
Ensure your project uses Symfony 6.4+ or Laravel 10+ (via Symfony Bridge).
Configuration
Add Biuras SMS credentials to your .env:
SMS_BIURAS_API_KEY=your_api_key_here
SMS_BIURAS_SENDER_ID=YourSender
First Use Case Send a basic SMS via a Laravel service:
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransport;
$notifier = new Notifier([
new SmsBiurasTransport('SMS_BIURAS_API_KEY', 'SMS_BIURAS_SENDER_ID'),
]);
$notifier->send(
new Notification('Welcome!', 'Your verification code: 12345')
);
config/services.php (Add Biuras SMS config if using Laravel’s service container).app/Providers/AppServiceProvider.php (Bind the transport to Laravel’s container).Laravel Integration
Bind the transport to Laravel’s container in AppServiceProvider:
public function register()
{
$this->app->bind(\Symfony\Component\Notifier\Transport\Dsn::class, function ($app) {
return new SmsBiurasTransport(
config('services.sms_biuras.api_key'),
config('services.sms_biuras.sender_id')
);
});
}
Sending Notifications
Use Laravel’s Notification facade or inject the Notifier:
use App\Notifications\SmsVerification;
// Option 1: Via Notification class
$user->notify(new SmsVerification());
// Option 2: Directly via Notifier
$notifier->send(
new Notification('Subject', 'Message'),
new Recipient('+37061234567', 'user@example.com')
);
Batch Sending Loop through users and send SMS in bulk (with rate-limiting):
foreach ($users as $user) {
$notifier->send(
new Notification('Hello', "Hi {$user->name}!"),
new Recipient($user->phone, $user->email)
);
sleep(1); // Avoid rate limits
}
Event-Driven Notifications
Trigger SMS on model events (e.g., created):
class User extends Model
{
protected static function booted()
{
static::created(function ($user) {
$user->notify(new SmsVerification());
});
}
}
$user->notify(new SmsVerification())->onQueue('sms');
$notifier = new Notifier([$transport], ['logger' => new SymfonyLogger()]);
Rate Limits
sleep() or queue retries with jitter.Phone Number Formatting
+37061234567). Validate input:use Libphonenumber\PhoneNumberUtil;
$phone = PhoneNumberUtil::getInstance()->parse($rawPhone, 'LT');
$recipient = new Recipient($phone->getNumber(), $email);
API Key Security
.env and validate in config:if (empty(config('services.sms_biuras.api_key'))) {
throw new \RuntimeException('SMS_BIURAS_API_KEY not configured.');
}
Character Limits
$chunks = array_chunk(str_split($longMessage, 150), 150);
foreach ($chunks as $chunk) {
$notifier->send(new Notification('Part', $chunk));
}
Recipient Validation
if (!preg_match('/^\+370\d{8}$/', $phone)) {
throw new \InvalidArgumentException('Invalid Lithuanian phone number.');
}
NOTIFIER_DEBUG=true in .env to log all requests/responses.429 for rate limits and 400 for invalid inputs. Handle in a custom transport wrapper:
class BiurasTransport extends SmsBiurasTransport
{
public function __construct(string $apiKey, string $senderId)
{
parent::__construct($apiKey, $senderId);
$this->setHttpClient(new \Symfony\Contracts\HttpClient\HttpClient([
'timeout' => 10,
'headers' => ['Accept' => 'application/json'],
]));
}
}
Custom Notification Classes
Extend Symfony’s Notification to add metadata:
class SmsVerification extends Notification
{
public function __construct(private string $code) {}
public function asSms(SmsMessage $message): void
{
$message->subject('Verification Code');
$message->text("Your code: {$this->code}");
}
}
Webhook Handling
Use Laravel’s HandleIncomingWebhook to process Biuras delivery reports:
Route::post('/sms-webhook', [SmsWebhookHandler::class, 'handle']);
Testing Mock the transport in PHPUnit:
$mockTransport = $this->createMock(SmsBiurasTransport::class);
$mockTransport->method('send')->willReturn(true);
$notifier = new Notifier([$mockTransport]);
Local Development
Use a local SMS gateway (e.g., symfony/notify-bundle’s null transport) to avoid hitting rate limits:
$notifier = new Notifier([
new NullTransport(), // For local testing
new SmsBiurasTransport($apiKey, $senderId),
]);
How can I help you explore Laravel packages today?