mesalution / laravel-mesms
A simple laravel package that helps you send SMS though various service providers
v1.0.3
2025-07-25 14:22 UTC
Requires
- php: ^8.2
- illuminate/support: ^11.0
- spatie/laravel-data: ^4.0
Requires (Dev)
- orchestra/testbench: ^9.14
README
แพ็กเกจ Laravel สำหรับส่งและจัดการ OTP ผ่านผู้ให้บริการ SMS ได้หลายเจ้า รองรับการเปลี่ยน driver ได้ง่าย และรองรับการเขียน fake สำหรับการทดสอบ
Feature
- รองรับการขอ OTP / ยืนยัน OTP / ขอ OTP ซ้ำ
- สลับ driver ได้ตาม environment (เช่น Production ใช้ gateway จริง, Local ใช้ Fake)
- ออกแบบตามหลัก SOLID ใช้งานร่วมกับ Laravel ได้เต็มรูปแบบ
- รองรับการจัดการ error ผ่าน Exception เฉพาะทาง
Spec Requirement
- PHP >= 8.2
- Laravel >= 10
- Support
Illuminate\Support\Facades\App - Recommend use with Laravel Service Container
Installation
composer require mesalution/laravel-mesms
Configuration
Publish the config:
php artisan vendor:publish --provider="Mesalution\LaravelMesms\SmsServiceProvider"
Edit config/sms.php:
return [ 'driver' => env('SMS_DRIVER', 'promotech'), 'providers' => [ 'fake' => [ 'class' => \Mesalution\LaravelMesms\Providers\FakeSms::class, 'options' => [] ], 'promotech' => [ 'class' => \Mesalution\LaravelMesms\Providers\Promotech::class, 'options' => [ 'url' => env('PROMOTECH_URL', 'http://apisms.promotech.co.th'), 'username' => env('PROMOTECH_USERNAME'), 'password' => env('PROMOTECH_PASSWORD'), 'otcId' => env('PROMOTECH_OTC_ID'), 'senderName' => env('PROMOTECH_SENDER_NAME'), ], ] ] ];
Or config in .env
SMS_DRIVER=promotech
PROMOTECH_USERNAME={{username}}
PROMOTECH_PASSWORD={{password}}
PROMOTECH_OTC_ID={{otcId}}
PROMOTECH_SENDER_NAME={{senderName}}
Usage
Create instance from app() helper
use Mesalution\LaravelMesms\Sms; $sms = app(Sms::class); $otp = $sms->requestOTP('0812345678'); $sms->verifyOTP('otp-id-xxx', '123456'); $sms->resendOTP('otp-id-xxx');
Manual create instance
use Mesalution\LaravelMesms\Sms; $options = [ 'username'=>'{{username}}', 'password'=>'{{password}}', 'otcId'=>'{{otcId}}', 'senderName'=>'{{senderName}}', ]; $sms = new Sms('promotech',$options); $otp = $sms->requestOTP('0812345678'); $sms->verifyOTP('otp-id-xxx', '123456'); $sms->resendOTP('otp-id-xxx');
Inject in controller
use Mesalution\LaravelMesms\Sms; class ExampleController extends Controller { public function __constructor(protected Sms $sms){} public function index() { $otp = $this->sms->requestOTP('0812345678'); $this->sms->verifyOTP('otp-id-xxx', '123456'); $this->sms->resendOTP('otp-id-xxx'); } }
How to implement new driver
Driver must be implement interface Mesalution\LaravelMesms\Contract\Sms
use Mesalution\LaravelMesms\Contracts\Sms; use Mesalution\LaravelMesms\Data\Otp; class MySmsDriver implements Sms { public function requestOTP(string $mobile): Otp { // your logic } public function verifyOTP(string $otpId, string $otpCode): bool { // your logic } public function resendOTP(string $otpId): bool { // your logic } }
Error Handler
this package will throw exception :
- RequestOtpException
- VerifyOtpException
- ResendOtpException
- AuthException
- BadResponseException
- ClientException
- ConfirmedOtpException
- ConnectionException
- ExpiredOtpException
- ExternalException
- InternalException
- InvalidOtpException
- OtpException
- SmsException
You can use try-catch for handle:
try {
$sms->verifyOTP('otp-id', '000000');
} catch (\Mesalution\LaravelMesms\Exceptions\VerifyOtpException $e) {
// จัดการกรณี otp ผิด
}
Testing
./vendor/bin/phpunit