symfony/allmysms-notifier
Symfony Notifier bridge for AllMySms. Configure via ALLMYSMS_DSN with login, API key, and optional sender. Send SmsMessage through AllMySms and customize delivery using AllMySmsOptions (campaign, scheduling, simulation, identifiers, verbosity).
Install the package via Composer:
composer require symfony/allmysms-notifier
Configure the DSN in your .env:
ALLMYSMS_DSN=allmysms://LOGIN:APIKEY@default?from=SENDER_NUMBER
Replace LOGIN, APIKEY, and SENDER_NUMBER with your AllMySms credentials.
Register the transport in config/services.php (Laravel):
'notifier' => [
'transports' => [
'allmysms' => [
'dsn' => env('ALLMYSMS_DSN'),
],
],
],
Send your first SMS in a Laravel job or controller:
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Message\SmsMessage;
$notifier = new Notifier([new AllMySmsTransport(env('ALLMYSMS_DSN'))]);
$notifier->send(new SmsMessage('+1234567890', 'Hello from Laravel!'));
Use the package to send OTPs or alerts with minimal boilerplate:
$sms = new SmsMessage('+1234567890', 'Your OTP is: ' . $otp);
$notifier->send($sms);
Leverage Laravel’s queues to handle SMS asynchronously:
SendSmsJob::dispatch('+1234567890', 'Your order is confirmed!');
public function handle()
{
$notifier = app(Notifier::class);
$notifier->send(new SmsMessage($this->phone, $this->message));
}
Attach AllMySmsOptions for advanced features like scheduling or campaigns:
$sms = new SmsMessage('+1234567890', 'Scheduled alert');
$sms->options((new AllMySmsOptions())
->date('2023-12-31 12:00:00') // Schedule for later
->campaignName('HolidayPromo') // Track in AllMySms dashboard
);
$notifier->send($sms);
Extend Laravel’s Notification class to use AllMySms:
use Illuminate\Notifications\Notification;
use Symfony\Component\Notifier\Message\SmsMessage;
class SmsNotification extends Notification
{
public function via($notifiable)
{
return ['sms'];
}
public function toSms($notifiable)
{
return (new SmsMessage($notifiable->phone, 'Hello, ' . $notifiable->name))
->options((new AllMySmsOptions())->campaignName('UserAlerts'));
}
}
Mock the transport for unit tests:
$transport = $this->createMock(TransportInterface::class);
$transport->expects($this->once())
->method('send')
->with($this->isInstanceOf(SmsMessage::class));
$notifier = new Notifier([$transport]);
$notifier->send(new SmsMessage('+1234567890', 'Test'));
DSN Format Sensitivity:
allmysms://LOGIN:APIKEY@default?from=SENDER exactly.default or from may cause silent failures.Phone Number Validation:
+123 instead of +1234567890).Str::startsWith($phone, '+') before sending.Rate Limits:
$this->dispatch(new SendSmsJob($phone, $message))->onQueue('sms');
Options Overrides:
AllMySmsOptions (e.g., simulate) may conflict with production use.Enable Verbose Logging:
$options = (new AllMySmsOptions())->verbose(1);
$sms->options($options);
Check Laravel logs for detailed API responses.
Check AllMySms Dashboard:
Custom Transport:
Extend AllMySmsTransport to add retries or fallback logic:
class CustomAllMySmsTransport extends AllMySmsTransport
{
public function send(SmsMessage $message): SentMessage
{
try {
return parent::send($message);
} catch (TransportException $e) {
// Fallback to email or retry
}
}
}
Dynamic DSN: Load the DSN from a config file or environment variable dynamically:
$dsn = config('services.allmysms.dsn');
$transport = new AllMySmsTransport($dsn);
Event Listeners:
Listen for sent/failed events to trigger analytics or retries:
public function handle(SentMessage $message)
{
if ($message->failed()) {
// Log or retry
}
}
Use Laravel’s env() Helper:
Avoid hardcoding credentials; use .env:
$transport = new AllMySmsTransport(env('ALLMYSMS_DSN'));
Batch Processing: For bulk SMS, use Laravel’s chunking:
User::chunk(100, function ($users) {
foreach ($users as $user) {
$this->dispatch(new SendSmsJob($user->phone, 'Batch message'));
}
});
Fallback Mechanism: Combine with another transport (e.g., Twilio) for redundancy:
$notifier = new Notifier([
new AllMySmsTransport(env('ALLMYSMS_DSN')),
new TwilioTransport(env('TWILIO_DSN')),
]);
How can I help you explore Laravel packages today?