定制 razikallayi/laravel-sms 二次开发

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

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

razikallayi/laravel-sms

Composer 安装命令:

composer require razikallayi/laravel-sms

包简介

A flexible SMS service for Laravel supporting multiple providers like Mithra, Twilio, Nexmo, and more

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

A flexible SMS service for Laravel supporting multiple providers like Mithra, Twilio, Nexmo, and more. Send SMS messages easily with a unified API similar to Laravel's Mail system.

Features

  • Multiple SMS Drivers: Support for Mithra, Twilio, Nexmo, Log, and Array drivers
  • Easy Provider Switching: Change SMS providers by updating configuration
  • Notification Channel: Send SMS through Laravel's notification system
  • Queue Support: SMS messages can be queued for better performance
  • Facade Support: Easy-to-use SMS facade for sending messages
  • Model Traits: Add SMS functionality to your models easily

Installation

Install the package via Composer:

composer require razikallayi/laravel-sms

Publish the configuration file:

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

Configuration

Add the following environment variables to your .env file:

# SMS Configuration
SMS_DRIVER=mithra

# Mithra SMS Configuration
MITHRA_SMS_TOKEN=your_mithra_token_here
MITHRA_SMS_SENDER=TXTLCL
MITHRA_SMS_BASE_URL=http://sms.mithraitsolutions.com/httpapi

# Twilio SMS Configuration  
TWILIO_ACCOUNT_SID=your_twilio_sid
TWILIO_AUTH_TOKEN=your_twilio_token
TWILIO_FROM_NUMBER=+1234567890

# Global SMS Settings
SMS_FROM_NAME="Your App Name"
SMS_FROM_NUMBER=+1234567890

Usage

Basic SMS Sending

use RaziKallayi\LaravelSms\Facades\SMS;
use RaziKallayi\LaravelSms\Services\SMS\Messages\SmsMessage;

// Simple message
$message = new SmsMessage('Hello, this is a test message!');
$message->to(['1234567890']);
$result = SMS::send($message);

// Advanced message with options
$message = (new SmsMessage('Your OTP is: 123456'))
    ->to(['1234567890'])
    ->route(config('sms.routes.trans_otp'))
    ->type(config('sms.message_types.text'))
    ->templateId('your_template_id');

$result = SMS::send($message);

Using Model Trait

Add the HasSmsNotifications trait to your models:

use RaziKallayi\LaravelSms\Traits\HasSmsNotifications;

class Customer extends Model
{
    use HasSmsNotifications;
    
    // Your model code...
}

// Send SMS to customer
$customer = Customer::find(1);
$customer->sendSms('Welcome to our service!');

Using Notifications

Create a notification that uses SMS:

use RaziKallayi\LaravelSms\Notifications\SmsChannel;
use RaziKallayi\LaravelSms\Services\SMS\Messages\SmsMessage;

class OrderConfirmation extends Notification
{
    public function via($notifiable)
    {
        return [SmsChannel::class];
    }

    public function toSms($notifiable)
    {
        return new SmsMessage("Your order has been confirmed!");
    }
}

// Send notification
$customer->notify(new OrderConfirmation());

Switching Providers

To switch SMS providers, simply update your environment variable:

# Use Mithra (default)
SMS_DRIVER=mithra

# Use Twilio
SMS_DRIVER=twilio

# Use Log for testing
SMS_DRIVER=log

Available Drivers

  • mithra - Mithra IT Solutions SMS service
  • twilio - Twilio SMS service
  • nexmo - Nexmo/Vonage SMS service
  • log - Log messages to Laravel logs
  • array - Store messages in memory (for testing)

SMS Routes

  • Promotional (1): For promotional messages
  • Transactional (2): For transactional messages
  • Sender ID (3): For custom sender ID messages
  • Trans OTP (4): For OTP messages
  • International (9): For international messages
  • Trans2 (10): Alternative transactional route

Message Types

  • Text (1): Regular text messages
  • Flash (2): Flash SMS messages
  • Unicode (3): Unicode messages for special characters

Error Handling

All SMS drivers return standardized response arrays:

// Success response
[
    'success' => true,
    'message_id' => '123456789',
    'recipients' => ['1234567890'],
    'content' => 'Your message content'
]

// Error response  
[
    'success' => false,
    'error_code' => '103',
    'error_message' => 'Invalid contact(s)',
    'recipients' => ['invalid_number']
]

Testing

For testing purposes, use the log or array drivers:

SMS_DRIVER=log  # Messages will be written to logs
SMS_DRIVER=array  # Messages will be stored in memory

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This package is open-sourced software licensed under the MIT license.

Support

If you discover any security related issues, please email razikallayi@gmail.com instead of using the issue tracker.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-28