prgayman/laravel-sms
Composer 安装命令:
composer require prgayman/laravel-sms
包简介
Laravel package for sending SMS
README 文档
README
Laravel SMS allows you to send SMS from your Laravel application using multiple sms providers, allow to add custom sms provider
Requirements
- php
^7.3|^8.0 - guzzlehttp/guzzle
^7.0.1
Installation
To get the latest version of laravel-sms on your project, require it from "composer":
$ composer require prgayman/laravel-sms
Or you can add it directly in your composer.json file:
{
"require": {
"prgayman/laravel-sms": "1.5.0"
}
}
Laravel
Register the provider directly in your app configuration file config/app.php config/app.php:
Laravel >= 5.5 provides package auto-discovery, thanks to rasmuscnielsen and luiztessadri who help to implement this feature in Zatca, the registration of the provider and the facades should not be necessary anymore.
'providers' => [ Prgayman\Sms\SmsServiceProvider::class, ]
Add the facade aliases in the same file:
'aliases' => [ 'Sms' => Prgayman\Sms\Facades\Sms::class, 'SmsHistory' => Prgayman\Sms\Facades\SmsHistory::class, ]
Lumen
Register the provider in your bootstrap app file boostrap/app.php
Add the following line in the "Register Service Providers" section at the bottom of the file.
$app->register(Prgayman\Sms\SmsServiceProvider::class);
For facades, add the following lines in the section "Create The Application" .
class_alias(\Prgayman\Sms\Facades\Sms::class, 'Sms'); class_alias(\Prgayman\Sms\Facades\SmsHistory::class, 'SmsHistory');
Run Migrations
Publish the migrations with this artisan command:
$ php artisan vendor:publish --tag=laravel-sms-migrations
Configuration
You can publish the config file with this artisan command:
$ php artisan vendor:publish --tag=laravel-sms-config
Available SMS Providers
| Provider | URL | Tested | Multiple contacts | Config |
|---|---|---|---|---|
| JawalSms | https://www.jawalsms.net/ | Yes | Yes | Click |
| Taqnyat | https://www.taqnyat.sa/ | Yes | Yes | Click |
| Nexmo | https://www.nexmo.com/ | Yes | No | Click |
| Twilio | https://www.twilio.com/ | Yes | No | Click |
| MoraSa | https://www.mora-sa.com/ | Yes | Yes | Click |
| Msegat | https://www.msegat.com/ | Yes | Yes | Click |
| Kobikom | https://kobikom.com.tr/ | Yes | Yes | Click |
| Unifonic | https://unifonic.com/ | No | Yes | Click |
| Jor Mall | https://www.josmsservice.com | Yes | No | Click |
Available SMS Drivers local development
| Provider | Multiple contacts | Config |
|---|---|---|
| array | Yes | - |
| log | Yes | Click |
Events
\Prgayman\Sms\Events\MessageSending::class\Prgayman\Sms\Events\MessageSent::class\Prgayman\Sms\Events\MessageFailed::class
Types
\Prgayman\Sms\SmsTypes::GENERAL\Prgayman\Sms\SmsTypes::OTP\Prgayman\Sms\SmsTypes::WELCOME\Prgayman\Sms\SmsTypes::AD
Usage
Set default driver
Using .env
SMS_DRIVER=log
Using facades
/** * Set the default sms driver name. * * @param string $driver */ Prgayman\Sms\Facades\Sms::setDefaultDriver("array");
Enable sms history using database (send multiple contacts is not support store history)
-
Enable the key
SMS_HISTORY_ENABLEDin.envfileSMS_HISTORY_ENABLED=true
-
Make sure publish the migrations with this artisan command:
$ php artisan vendor:publish --tag=laravel-sms-migrations -
Run migrate with this artisan command:
$ php artisan migrate
Send Message
You can simply send a message like this:
# Send message using facade use Prgayman\Sms\Facades\Sms; $to = "+962790000000"; $from = "SenderName"; $message = "Test Send Message"; /** * Send using default driver sms * * @return \Prgayman\Sms\SmsDriverResponse */ $response = Sms::to($to)->from($from)->message($message)->send(); # Get Message $response->getMessage(); # Get Request $response->getRequest(); # Get driver response $response->getResponse(); # Check is successfuly send sms message $response->successful(); # Check is failed send sms message $response->failed();
Send using select driver sms
Sms::driver("array") ->to($to) ->from($from) ->message($message) ->send();
Send multiple contacts
// please sure driver is support send multiple contacts Sms::to([ "+962792994123", "+962792994124", "+962792994125", ]) ->from($from) ->message($message) ->send();
Send using custom type
Sms::driver("array") ->type(\Prgayman\Sms\SmsTypes::OTP) ->to($to) ->from($from) ->message($message) ->send();
Send multiple messages (run events and store history per message)
$items = [ [ "to" => "+962792994123", "from" => "SenderName", "message" => "New message" ], [ "to" => "+962792994124", "from" => "SenderName", "message" => "Send Message" ] ]; /** * @param $items must contain message, to, and from keys per item * @return \Prgayman\sms\SmsDriverResponse[] */ $response = Sms::sendArray($items); // Or send using helper function $response = sms()->sendArray($items);
Send using helper function with default driver
sms() ->to($to) ->from($from) ->message($message) ->send();
Send using helper function and select driver
sms("array") ->to($to) ->from($from) ->message($message) ->send();
Send using helper function and custom type
sms("array") ->type(\Prgayman\Sms\SmsTypes::OTP) ->to($to) ->from($from) ->message($message) ->send();
Create custom driver
-
Create class extends from
\Prgayman\Sms\Drivers\Driverand handler send function -
if driver support send multiple contacts please implements from
Prgayman\Sms\Contracts\DriverMultipleContactsInterfaceuse Prgayman\Sms\Drivers\Driver; use Prgayman\Sms\SmsDriverResponse; use Prgayman\Sms\Contracts\DriverMultipleContactsInterface; class CustomDriver extends Driver implements DriverMultipleContactsInterface { # You not need to run events or store history # package automatically run all events and store history public function send() : SmsDriverResponse { $request = [ "to" => $this->getTo(), 'from' => $this->getFrom(), 'body' => $this->getMessage(), ]; try { # Handler send message $response = null; return new SmsDriverResponse($request, $response, true); } catch (\Exception $e) { return new SmsDriverResponse($request, null, false, $e->getMessage()); } } }
-
Add driver confg in
config/sms.php"drivers"=>[ ....... # Use custom driver 'your-driver-name'=>[ 'handler'=> \App\SmsDrivers\CustomDriver::class ], # Use supported drivers but different name # Copy driver object and change name "new-log-driver" => [ "driver" => "log", 'channel' => env('SMS_LOG_CHANNEL'), ], ]
-
Send message with custom driver
# Use driver Sms::driver("your-driver-name") ->to($to) ->from($from) ->message($message) ->send(); # Or set custom driver in default driver or set # SMS_DRIVER=your-driver-name in dotenv file Sms::setDefaultDriver("your-driver-name"); Sms::to($to) ->from($from) ->message($message) ->send();
Channel Usage
First you have to create your notification using php artisan make:notification command.
then Prgayman\Sms\Channels\SmsChannel::class can be used as channel like the below:
use Illuminate\Notifications\Notification; use Prgayman\Sms\SmsNotification; class SendSmsNotification extends Notification { /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['sms']; # add this channel } /** * @param mixed $notifiable * @return \Prgayman\Sms\SmsNotification */ public function toSms($notifiable) { # Send message with default driver return (new SmsNotification) ->to("+962790000000") ->from("SenderName") ->message("Test New Message"); # Send message with select driver return (new SmsNotification) ->driver('array') ->to("+962790000000") ->from("SenderName") ->message("Test New Message"); } }
SMS History
use Prgayman\Sms\Facades\SmsHistory; # Get all $histories = SmsHistory::get(); # Use Filters all filter is optional $histories = SmsHistory::recipients("+962790000000") ->senders(["SendName"]) ->statuses([ Prgayman\Sms\Models\SmsHistory::SUCCESSED, Prgayman\Sms\Models\SmsHistory::FAILED, ]) ->drivers(["log","array"]) ->driverNames(["custom_name"]) ->get(); # Or can use helper function $histories = smsHistory() ->recipients("+962790000000") ->senders(["SendName"]) ->statuses([ Prgayman\Sms\Models\SmsHistory::SUCCESSED, Prgayman\Sms\Models\SmsHistory::FAILED, ]) ->drivers(["log","array"]) ->driverNames(["custom_name"]) ->get();
Testing
composer test
Licence
This library is open-sourced software licensed under the MIT license.
prgayman/laravel-sms 适用场景与选型建议
prgayman/laravel-sms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.03k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2022 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「sms」 「twilio」 「laravel」 「nexmo」 「prgayman」 「taqnyat」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 prgayman/laravel-sms 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 prgayman/laravel-sms 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 prgayman/laravel-sms 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
SDK for payment gateway PlatbaMobilom.sk for PHP7.0
Componente para integração com serviços de mensageria com APIs externas como Twilio, SendGrid, AWS SES, etc.
Extensible library for building notifications and sending them via different delivery channels.
yii2-sms expand
Alfabank REST API integration
统计信息
- 总下载量: 5.03k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 35
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-17