Laravel Fast2SMS dispatches events at key points in the SMS and WhatsApp send lifecycle. You can listen to these events to add logging, alerting, analytics, or any custom behaviour.
| Event | Namespace | When |
|---|---|---|
SmsSent |
Shakil\Fast2sms\Events\SmsSent |
After a successful SMS send |
SmsFailed |
Shakil\Fast2sms\Events\SmsFailed |
When an SMS send throws an exception |
WhatsAppSent |
Shakil\Fast2sms\Events\WhatsAppSent |
After a successful WhatsApp send |
WhatsAppFailed |
Shakil\Fast2sms\Events\WhatsAppFailed |
When a WhatsApp send throws an exception |
LowBalanceDetected |
Shakil\Fast2sms\Events\LowBalanceDetected |
When wallet balance drops below threshold |
SmsSentpublic function __construct(
public array $payload, // The SMS payload sent to the API
public SmsResponse $response // The API response
) {}
SmsFailedpublic function __construct(
public array $payload, // The SMS payload attempted
public \Throwable $exception, // The exception thrown
public ?array $response = null // Response if available
) {}
WhatsAppSentpublic function __construct(
public array $payload, // The WhatsApp payload sent
public WhatsAppResponse $response // The API response
) {}
WhatsAppFailedpublic function __construct(
public array $payload, // The WhatsApp payload attempted
public \Throwable $exception, // The exception thrown
public ?array $response = null // Response if available
) {}
LowBalanceDetectedpublic function __construct(
public float $balance, // Current wallet balance
public float $threshold // Configured threshold
) {}
Add your listeners in App\Providers\EventServiceProvider:
use Shakil\Fast2sms\Events\SmsSent;
use Shakil\Fast2sms\Events\SmsFailed;
use Shakil\Fast2sms\Events\LowBalanceDetected;
use Shakil\Fast2sms\Events\WhatsAppSent;
use Shakil\Fast2sms\Events\WhatsAppFailed;
protected $listen = [
SmsSent::class => [
\App\Listeners\LogSmsSent::class,
\App\Listeners\TrackSmsAnalytics::class,
],
SmsFailed::class => [
\App\Listeners\AlertOnSmsFailed::class,
],
LowBalanceDetected::class => [
\App\Listeners\NotifyAdminOfLowBalance::class,
],
WhatsAppSent::class => [
\App\Listeners\LogWhatsAppSent::class,
],
WhatsAppFailed::class => [
\App\Listeners\AlertOnWhatsAppFailed::class,
],
];
namespace App\Listeners;
use Shakil\Fast2sms\Events\SmsSent;
class LogSmsSent
{
public function handle(SmsSent $event): void
{
\Log::info('SMS sent', [
'request_id' => $event->response->requestId,
'numbers' => $event->payload['numbers'] ?? null,
]);
}
}
To disable all event dispatching (e.g. in high-throughput scenarios):
FAST2SMS_EVENTS_ENABLED=false
php artisan fast2sms:events
This lists all package events with their descriptions in a table.
The package ships with built-in listeners that log to the database when database_logging is enabled:
| Listener | Event | Action |
|---|---|---|
LogSmsSent |
SmsSent |
Writes send record to DB |
LogSmsFailed |
SmsFailed |
Writes failure record to DB |
LogWhatsAppSent |
WhatsAppSent |
Writes send record to DB |
LogWhatsAppFailed |
WhatsAppFailed |
Writes failure record to DB |
How can I help you explore Laravel packages today?