daika7ana/laravel-web2sms
Laravel 5.4+ package to send SMS via Web2SMS.ro. Provides a service provider, config publishing, and a Web2sms facade/alias to set recipient and message, then send. Configure credentials and defaults in config/web2sms.php.
Installation
composer require daika7ana/laravel-web2sms
Publish the config file:
php artisan vendor:publish --provider="Daika7ana\Web2Sms\Web2SmsServiceProvider"
Configuration
Edit .env with your Web2SMS credentials:
WEB2SMS_USERNAME=your_username
WEB2SMS_PASSWORD=your_password
WEB2SMS_API_URL=https://api.web2sms.com
First Use Case Send a test SMS in a controller:
use Daika7ana\Web2Sms\Facades\Web2Sms;
public function sendTestSms()
{
$response = Web2Sms::send([
'to' => '254712345678', // Format: CountryCode + PhoneNumber
'message' => 'Hello from Laravel!',
]);
return $response->success() ? 'SMS sent!' : 'Failed: ' . $response->message();
}
Basic SMS Sending
Web2Sms::send([
'to' => '254712345678',
'message' => 'Your OTP is 12345',
]);
Bulk SMS with Recipients
$recipients = ['254712345678', '254798765432'];
foreach ($recipients as $phone) {
Web2Sms::send(['to' => $phone, 'message' => 'Bulk message']);
}
Sending with Custom Headers
Web2Sms::send([
'to' => '254712345678',
'message' => 'Custom header test',
'headers' => ['X-Custom-Header' => 'value'],
]);
Handling Responses
$response = Web2Sms::send([...]);
if ($response->success()) {
$messageId = $response->messageId(); // Unique ID for tracking
} else {
Log::error('SMS failed: ' . $response->message());
}
Queue SMS Jobs Use Laravel queues to avoid timeouts for bulk SMS:
dispatch(new SendSmsJob($phone, $message));
Job class:
class SendSmsJob implements ShouldQueue
{
public function handle()
{
Web2Sms::send(['to' => $this->phone, 'message' => $this->message]);
}
}
Logging Failed SMS Extend the package to log failures:
Web2Sms::extend(function ($builder) {
$builder->afterSend(function ($response) {
if (!$response->success()) {
Log::error('Web2SMS Error', [
'phone' => $response->to(),
'message' => $response->message(),
'code' => $response->code(),
]);
}
});
});
Rate Limiting
Use Laravel’s throttle middleware to limit SMS bursts:
Route::middleware(['throttle:10,1'])->group(function () {
Route::post('/send-sms', [SmsController::class, 'send']);
});
Phone Number Format
CountryCodePhoneNumber format (e.g., 254712345678 for Kenya).$phone = preg_replace('/[^0-9]/', '', $phone); // Remove non-digits
if (strlen($phone) < 10) {
throw new \InvalidArgumentException('Invalid phone number');
}
API URL Hardcoding
https://api.web2sms.com, but some providers use custom URLs (e.g., https://yourprovider.com/api)..env or config:
WEB2SMS_API_URL=https://yourprovider.com/api
No Retry Mechanism
Web2Sms::extend(function ($builder) {
$builder->setRetryPolicy(new RetryPolicy(3)); // Retry 3 times
});
No Transactional Support
$sms = Sms::create(['phone' => $phone, 'message' => $message]);
$response = Web2Sms::send(['to' => $phone, 'message' => $message]);
if ($response->success()) {
$sms->update(['status' => 'sent', 'message_id' => $response->messageId()]);
}
No Webhook Support
$status = Web2Sms::checkStatus($messageId);
Enable Debug Mode
Add to .env:
WEB2SMS_DEBUG=true
This logs the raw API response for troubleshooting.
Check HTTP Status Codes
The response object includes the HTTP status code:
if ($response->code() === 401) {
// Unauthorized (check credentials)
}
Test with a Sandbox Use a Web2SMS sandbox environment (if available) to avoid hitting rate limits during development.
Custom Response Handling
Extend the Daika7ana\Web2Sms\Response class to add custom methods:
Web2Sms::extend(function ($builder) {
$builder->addResponseMethod('isDelivered', function () {
return $this->status() === 'delivered';
});
});
Add Templates Support SMS templates for consistency:
Web2Sms::sendTemplate('otp', ['code' => '12345'], '254712345678');
(Requires modifying the package or creating a wrapper.)
Support for Unicode If sending non-ASCII messages (e.g., emojis), ensure the API supports UTF-8:
Web2Sms::send([
'to' => '254712345678',
'message' => 'Hello 🌍!', // Unicode character
'encoding' => 'UTF-8',
]);
How can I help you explore Laravel packages today?