dunp/smsend-laravel
Pacchetto Laravel per inviare SMS tramite il provider italiano smsend.it. Compatibile con Laravel 9 e 10, include configurazione pubblicabile, timeout, qualità e mittente di default. Integrazione rapida con poche righe di codice.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require dunp/smsend-laravel
Publish the config file:
php artisan vendor:publish --provider="Dunp\SmsendLaravel\SmsendServiceProvider"
Configuration
Edit .env with your Smsend API credentials:
SMSEND_API_KEY=your_api_key_here
SMSEND_API_SECRET=your_api_secret_here
First Use Case: Sending a Basic SMS
use Dunp\SmsendLaravel\Facades\Smsend;
Smsend::send([
'to' => '391234567890',
'text' => 'Hello from Laravel!'
]);
Verify Configuration
Check the published config at config/smsend.php for additional settings like:
Use the Smsend facade for simplicity in controllers/blades:
Smsend::send([
'to' => '391234567890',
'text' => 'Your OTP is 12345',
'sender' => 'MyApp' // Optional
]);
Wrap the send call in a job to avoid blocking the request:
use Dunp\SmsendLaravel\Jobs\SendSmsJob;
SendSmsJob::dispatch([
'to' => '391234567890',
'text' => 'Your order is confirmed!'
]);
Capture the API response for logging or retries:
$response = Smsend::send([...]);
if ($response->success()) {
// Handle success
} else {
logger()->error('SMS failed:', ['error' => $response->getError()]);
}
Send to multiple recipients efficiently:
$recipients = ['391234567890', '399876543210'];
foreach ($recipients as $number) {
Smsend::send(['to' => $number, 'text' => 'Batch message']);
}
Trigger SMS on events (e.g., user registration):
// In an event listener
event(new Registered($user));
// In the listener
Smsend::send([
'to' => $user->phone,
'text' => 'Welcome to our platform!'
]);
Override the default sender per message:
Smsend::send([
'to' => '391234567890',
'text' => 'Message',
'sender' => 'CustomSender' // Overrides config/smsend.php default
]);
Enable Unicode for non-Latin characters:
Smsend::send([
'to' => '391234567890',
'text' => 'Ciao! 你好!',
'unicode' => true
]);
Enable logging in config/smsend.php:
'log' => [
'enabled' => true,
'channel' => 'single', // or 'stack'
],
Use Laravel’s mocking to test SMS logic:
$this->mock(Smsend::class)->shouldReceive('send')->once();
Implement a fallback for failed sends (e.g., email):
try {
Smsend::send([...]);
} catch (\Exception $e) {
Mail::to($user->email)->send(new FallbackSmsEmail($message));
}
401 Unauthorized or empty responses..env and config/smsend.php for typos.php artisan config:clear after changes.39 for Italy) and no spaces/dashes.use Dunp\SmsendLaravel\Validators\PhoneValidator;
if (!PhoneValidator::validate('391234567890')) {
// Handle invalid format
}
429 Too Many Requests.'unicode' => true in the send array or globally in config.SendSmsJob jobs remain in the queue.HandleFailedJobs middleware or manually retry:
php artisan queue:retry all
Add to config/smsend.php:
'log' => [
'enabled' => true,
'level' => 'debug',
],
Check logs at storage/logs/laravel.log.
Temporarily modify the send method in Dunp\SmsendLaravel\SmsendManager to log raw responses:
$response = $this->client->send($data);
logger()->debug('Raw API Response', ['response' => $response->getBody()->getContents()]);
Use Smsend’s API docs to validate your credentials and payloads manually.
Bind a custom Guzzle client in AppServiceProvider:
$this->app->singleton(\GuzzleHttp\Client::class, function () {
return new \GuzzleHttp\Client([
'timeout' => 30,
'headers' => [
'User-Agent' => 'MyApp/Smsend',
],
]);
});
Extend the SmsendManager to add logic before/after sending:
// In a service provider
$this->app->extend('smsend', function ($manager) {
$manager->extend('preSend', function ($data) {
// Modify $data or validate
return $data;
});
return $manager;
});
Implement a provider interface and register it:
// Example: Add Twilio support
$this->app->bind(\Dunp\SmsendLaravel\Contracts\SmsProvider::class, function () {
return new \App\Providers\TwilioSmsProvider();
});
Use a mock provider for testing:
// In phpunit.php
$this->app->when(Smsend::class)
->needs(\Dunp\SmsendLaravel\Contracts\SmsProvider::class)
->give(new \App\Providers\MockSmsProvider());
config('smsend.sender').config/smsend.php:
'sender' => env('SMSEND_SENDER', 'DefaultSender'),
10 seconds. Adjust in config/smsend.php:
'timeout' => 30, // seconds
How can I help you explore Laravel packages today?