定制 mr-rijal/laravel-sms 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mr-rijal/laravel-sms

Composer 安装命令:

composer require mr-rijal/laravel-sms

包简介

Unified, extensible SMS gateway for Laravel

README 文档

README

Workflow Latest Version PHP Latest Stable Version License Downloads Star

A unified, extensible SMS gateway for Laravel. Send SMS via Twilio, Vonage, MSG91, Sparrow, AWS SNS, WhatsApp, or your own driver—with one simple API.

Features

  • Multiple drivers — Twilio, Vonage (Nexmo), MSG91, Sparrow SMS, AWS SNS, WhatsApp Business API
  • Laravel Notifications — Use the sms channel and SmsNotification out of the box
  • Queue & schedulesendLater() and sendLaterAt() for queued and scheduled messages
  • Templates — Template IDs and variables for providers that support them (e.g. MSG91, WhatsApp)
  • EventsSmsSending and SmsSent for logging, auditing, or blocking
  • Macros — Extend the facade with custom methods (e.g. sendOtp())
  • Random driver — Load balancing across multiple providers via random_drivers
  • Fake driver — Test without hitting real APIs
  • Custom drivers — Implement SmsProvider and register your own gateway

Requirements

  • PHP 8.2 or higher on Laravel 11–12; Laravel 13 requires PHP 8.3 or higher (upstream constraint)
  • Laravel 11.x and above
  • Guzzle HTTP (required by drivers that call APIs)

Installation

Install the package via Composer:

composer require mr-rijal/laravel-sms

Publish the config file:

php artisan vendor:publish --tag=sms-config

This creates config/sms.php. Set your default driver and provider credentials in .env:

SMS_PROVIDER=twilio

# Example: Twilio
TWILIO_SID=
TWILIO_TOKEN=
TWILIO_FROM=

Configuration

The published config defines the default driver, queue behaviour, and credentials per provider. Main options:

Key Description
default Default driver name (e.g. twilio, fake)
queue When true, send() queues instead of sending now
drivers Map of driver names to driver classes
providers Credentials per driver (keys, tokens, etc.)
random_drivers Drivers used when provider is random

Example config/sms.php layout:

return [
    'default' => env('SMS_PROVIDER', 'fake'),
    'queue'   => env('SMS_QUEUE', false),

    'drivers' => [
        'twilio'  => \MrRijal\LaravelSms\Drivers\TwilioDriver::class,
        'sparrow' => \MrRijal\LaravelSms\Drivers\SparrowDriver::class,
        'msg91'   => \MrRijal\LaravelSms\Drivers\Msg91Driver::class,
        'vonage'  => \MrRijal\LaravelSms\Drivers\VonageDriver::class,
        'aws_sns' => \MrRijal\LaravelSms\Drivers\AwsSnsDriver::class,
        'fake'    => \MrRijal\LaravelSms\Drivers\FakeDriver::class,
    ],

    'providers' => [
        'twilio'  => [
            'sid'   => env('TWILIO_SID'),
            'token' => env('TWILIO_TOKEN'),
            'from'  => env('TWILIO_FROM'),
        ],
        // … other providers
    ],

    'random_drivers' => ['twilio', 'msg91'],
];

Usage

Sending a message

use MrRijal\LaravelSms\Facades\Sms;

Sms::to('9812345678')
    ->message('Hello from Laravel SMS')
    ->send();

When sms.queue is true, send() queues the message. To send immediately:

Sms::to('9812345678')
    ->message('Urgent message')
    ->sendNow();

Using a specific provider

Sms::provider('twilio')
    ->to('9812345678')
    ->message('Hello from Twilio')
    ->send();

Multiple recipients

Sms::to(['9812345678', '9800000000'])
    ->message('System maintenance tonight')
    ->send();

Template messages

Sms::to('9812345678')
    ->template('1207161789456789012', ['otp' => 123456])
    ->send();

template() takes a template ID and an array of variables. Supported where the driver implements it (e.g. MSG91, WhatsApp).

Queued and scheduled sending

// Queue and send when the worker runs
Sms::to('9812345678')->message('Queued SMS')->sendLater();

// Schedule for a specific time
Sms::to('9812345678')
    ->message('Scheduled SMS')
    ->sendLaterAt(now()->addMinutes(10));

Random driver selection

Use the random provider to pick one of the configured drivers (e.g. for load balancing):

Sms::provider('random')
    ->to('9812345678')
    ->message('Sent via a random driver from config(random_drivers)')
    ->send();

Laravel Notifications

Use the built-in SMS channel and notification class.

Via SmsNotification

use MrRijal\LaravelSms\Notifications\SmsNotification;

$user->notify(new SmsNotification(message: 'Your order has been shipped'));

// With template
$user->notify(new SmsNotification(
    templateId: '1207161789456789012',
    variables: ['otp' => 456789]
));

User model setup

Add the HasSms trait and routeNotificationForSms():

use MrRijal\LaravelSms\Traits\HasSms;

class User extends Authenticatable
{
    use HasSms;

    public function routeNotificationForSms(): string
    {
        return $this->phone;
    }
}

Then you can do:

$user->sms('Welcome to the app');
$user->sendSms('Password changed');
$user->smsTemplate('1207161789456789012', ['otp' => 987654]);

Events

The package dispatches:

Event When
SmsSending Before the driver sends
SmsSent After send (success/failure)

Example listener:

use MrRijal\LaravelSms\Events\SmsSending;
use MrRijal\LaravelSms\Events\SmsSent;

Event::listen(SmsSending::class, function (SmsSending $event) {
    // Log, audit, or short-circuit sending
});

Event::listen(SmsSent::class, function (SmsSent $event) {
    if ($event->success) {
        // …
    } else {
        // $event->error …
    }
});

Macros

Extend the Sms facade without editing the package:

use MrRijal\LaravelSms\Facades\Sms;

Sms::macro('sendOtp', function (string $number, int $otp) {
    return $this->to($number)
        ->template('OTP_TEMPLATE', ['otp' => $otp])
        ->sendNow();
});

Sms::sendOtp('9812345678', 123456);

Custom drivers

  1. Implement MrRijal\LaravelSms\Contracts\SmsProvider:
use MrRijal\LaravelSms\Contracts\SmsProvider;
use MrRijal\LaravelSms\SmsMessage;

class MySmsDriver implements SmsProvider
{
    public function __construct(protected array $config) {}

    public function send(SmsMessage $message): bool
    {
        // Use $message->getTo(), getText(), getTemplateId(), getVariables()
        return true;
    }
}
  1. Register the driver and its config in config/sms.php:
'drivers' => [
    // …
    'mysms' => \App\Sms\MySmsDriver::class,
],
'providers' => [
    // …
    'mysms' => [
        'api_key' => env('MYSMS_API_KEY'),
    ],
],
  1. Use it:
Sms::provider('mysms')
    ->to('9812345678')
    ->message('Hello from MySMS')
    ->sendNow();

Testing

Use the fake driver so no real SMS are sent:

Sms::provider('fake')
    ->to('9800000000')
    ->message('Test')
    ->sendNow();

In tests you can assert on FakeDriver::$messages and call FakeDriver::reset() between tests.

Run the test suite:

composer test
# or
vendor/bin/phpunit

Continuous integration

Pull requests and pushes to main run GitHub Actions: Pint (code style), PHPStan (Larastan), PHPUnit across PHP 8.2–8.4 with Laravel 11–13 (Laravel 13 runs on PHP 8.3+ only, per Laravel’s requirement), plus a lowest-dependencies sanity job.

Run the same checks locally:

composer ci

Changelog

Please see Releases for a changelog.

Contributing

Contributions are welcome. Please see CONTRIBUTING.md for details.

Security

If you discover a security issue, please email prashantrijal.721@gmail.com instead of opening an issue. See SECURITY.md for the full policy.

Credits

Support

If this package is useful to you, you can support its maintenance via buymemomo.com/rijal.

License

The MIT License (MIT). Please see LICENSE for more information.

mr-rijal/laravel-sms 适用场景与选型建议

mr-rijal/laravel-sms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 280 次下载、GitHub Stars 达 45, 最近一次更新时间为 2026 年 01 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mr-rijal/laravel-sms 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mr-rijal/laravel-sms 我们能提供哪些服务?
定制开发 / 二次开发

基于 mr-rijal/laravel-sms 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 280
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 45
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 45
  • Watchers: 4
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-01