ersalak/ersalak-laravel-sms
Laravel package for sending SMS via Ersalak API. Provides facade-based methods for simple SMS, P2P messages, OTP template sends, and message status reports. Easy install via Composer, publish config, and set ERSALAK env credentials.
Installation:
composer require ersalak/ersalak-laravel-sms
php artisan vendor:publish --tag=ersalak-config
Update .env with your Ersalak credentials:
ERSALAK_SMS_USERNAME=your_username
ERSALAK_SMS_PASSWORD=your_password
ERSALAK_SMS_BASE_URL=https://sms.ersalak.ir
ERSALAK_SMS_LOG=true
First Use Case: Send a basic SMS in a controller:
use Ersalak\Sms\Facade\ErsalakSmsFacade;
public function sendOtp()
{
$response = ErsalakSmsFacade::sendSms(
source: '1000', // Your sender ID
destination: '09121234567',
message: 'Your OTP is 123456',
send_to_black_list: 0
);
return $response;
}
Key Files to Review:
config/ersalak-sms.php (published config)vendor/ersalak/ersalak-laravel-sms/src/ErsalakSmsServiceProvider.php (service provider)vendor/ersalak/ersalak-laravel-sms/src/Facade/ErsalakSmsFacade.php (facade methods)$response = ErsalakSmsFacade::sendSms([
'source' => '1000',
'destination' => '09121234567',
'message' => 'Hello from Laravel!',
'send_to_black_list' => 0,
]);
$response = ErsalakSmsFacade::sendBulkSms([
'source' => '1000',
'destinations' => ['09121234567', '09357654321'],
'message' => 'Bulk message',
]);
$status = ErsalakSmsFacade::getMessageStatus('message_id_from_response');
$report = ErsalakSmsFacade::getReport(['message_id_1', 'message_id_2']);
use Illuminate\Support\Facades\Event;
Event::listen('registered', function ($user) {
ErsalakSmsFacade::sendSms([
'source' => '1000',
'destination' => $user->phone,
'message' => "Welcome, {$user->name}! Your account is active.",
]);
});
php artisan make:job SendSmsJob
public function handle()
{
ErsalakSmsFacade::sendSms([
'source' => '1000',
'destination' => $this->phone,
'message' => $this->message,
]);
}
SendSmsJob::dispatch('09121234567', 'Hello from queue!');
.env:
ERSALAK_SMS_LOG=true
storage/logs/laravel.log for SMS responses and errors.Validate phone numbers before sending:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make(['phone' => $request->phone], [
'phone' => 'required|string|regex:/^09[0-9]{9}$/',
]);
Use Laravel's rate limiting middleware:
Route::middleware(['throttle:10,1'])->group(function () {
Route::post('/send-sms', [SmsController::class, 'send']);
});
Implement a retry logic for failed SMS:
use Illuminate\Support\Facades\Bus;
Bus::dispatch(new SendSmsJob($phone, $message))->onQueue('sms');
Mock the facade in tests:
$this->mock(ErsalakSmsFacade::class)->shouldReceive('sendSms')->once()->andReturn(['success' => true]);
API Rate Limits:
.env for rate limit errors and implement exponential backoff in your retry logic.Phone Number Formatting:
09121234567 without + or spaces). Invalid formats will fail silently or return errors.Blacklist Flag:
send_to_black_list defaults to 0 (do not send to blacklisted numbers). Set to 1 if you want to override this.Message Length:
Logging Overhead:
ERSALAK_SMS_LOG=true can bloat logs quickly. Disable in production if not needed for debugging.Check Raw Responses:
try {
$response = ErsalakSmsFacade::sendSms([...]);
} catch (\Throwable $e) {
\Log::error('Ersalak SMS Error: ' . $e->getMessage(), ['response' => $e->getTraceAsString()]);
}
Validate Credentials:
ERSALAK_SMS_USERNAME and ERSALAK_SMS_PASSWORD are correct. Test with a simple API call using Postman or cURL to verify credentials.Network Issues:
max_execution_time or implement retry logic.Facade vs. Service Container:
Ersalak\Sms\ErsalakSms) in services/repositories for better testability.Store Message IDs:
message_ids in your database to track status later:
$message = new Message();
$message->phone = $request->phone;
$message->message_id = $response['message_id'];
$message->save();
Customize Config:
config/ersalak-sms.php:
'timeout' => 30, // Increase timeout for slow connections
'retry_attempts' => 3, // Retry failed requests
Extend Functionality:
trait SmsNotifier
{
public function notifyViaSms($phone, $message)
{
return ErsalakSmsFacade::sendSms([
'source' => config('ersalak-sms.source'),
'destination' => $phone,
'message' => $message,
]);
}
}
Webhook for Status Updates:
Route::post('/ersalak-webhook', [ErsalakWebhookController::class, 'handle']);
Environment-Specific Config:
'base_url' => env('ERSALAK_SMS_BASE_URL', 'https://sms.ersalak.ir'),
Local Testing:
Error Handling:
try {
$response = ErsalakSmsFacade::sendSms([...]);
} catch (\Ersalak\Sms\Exceptions\InvalidCredentialsException $e) {
// Handle credential errors
} catch (\Ersalak\Sms\Exceptions\RateLimit
How can I help you explore Laravel packages today?