Installation
composer require arcturial/clickatell
Add the service provider to config/app.php (if not using Laravel's auto-discovery):
'providers' => [
// ...
Arcturial\Clickatell\ClickatellServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Arcturial\Clickatell\ClickatellServiceProvider"
Update .env with your Clickatell API credentials:
CLICKATELL_API_KEY=your_api_key_here
CLICKATELL_ACCOUNT_SID=your_account_sid
First Use Case: Sending a Basic SMS
Inject the Clickatell facade or service into a controller/service:
use Arcturial\Clickatell\Facades\Clickatell;
public function sendSms()
{
$response = Clickatell::send([
'to' => '27821234567',
'text' => 'Hello from Laravel!',
]);
return $response->success() ? 'SMS sent!' : 'Failed: ' . $response->error();
}
Sending SMS with Recipients
// Single recipient
Clickatell::send(['to' => '27821234567', 'text' => 'Hi there']);
// Multiple recipients (bulk)
Clickatell::send([
'to' => ['27821234567', '27831234567'],
'text' => 'Group message',
]);
Sending Unicode/Emoji Messages
Clickatell::send([
'to' => '27821234567',
'text' => 'Hello! 😊 How are you?',
'unicode' => true, // Enable Unicode support
]);
Scheduling SMS (via Laravel Queues)
use Arcturial\Clickatell\Jobs\SendSmsJob;
SendSmsJob::dispatch([
'to' => '27821234567',
'text' => 'Scheduled message',
]);
Register the job in AppServiceProvider:
public function boot()
{
$this->app->make('Arcturial\Clickatell\Jobs\SendSmsJob')->handle();
}
Handling Responses
$response = Clickatell::send(['to' => '27821234567', 'text' => 'Test']);
if ($response->success()) {
$messageId = $response->messageId(); // Unique ID for tracking
} else {
$error = $response->error(); // Error message
}
Using Templates (Clickatell Smart Messages)
Clickatell::send([
'to' => '27821234567',
'templateId' => 'YOUR_TEMPLATE_ID',
'variables' => ['name' => 'John'],
]);
Logging Responses
Wrap API calls in a try-catch and log responses for debugging:
try {
$response = Clickatell::send([...]);
\Log::info('SMS sent', ['response' => $response->toArray()]);
} catch (\Exception $e) {
\Log::error('SMS failed', ['error' => $e->getMessage()]);
}
Rate Limiting
Implement Laravel's throttle middleware for API calls to avoid hitting Clickatell's rate limits:
Route::middleware(['throttle:10,1'])->group(function () {
Route::post('/sms', 'SmsController@send');
});
Retry Failed Jobs
Use Laravel's retryAfter for failed SMS jobs:
SendSmsJob::dispatch([...])
->retryAfter(5); // Retry after 5 minutes
Testing
Mock the Clickatell facade in tests:
$this->mock(Clickatell::class)->shouldReceive('send')
->once()
->andReturn(new \Arcturial\Clickatell\Response(true, 'OK', '123'));
Deprecated API
Clickatell class or use a wrapper to adapt to newer APIs:
class ExtendedClickatell extends \Arcturial\Clickatell\Clickatell {
public function sendV3(array $data) {
// Custom logic for v3 API
}
}
No Async Support by Default
Unicode Handling
unicode => true and may incur additional costs. Test with a small batch first.Error Handling
if (!$response->success()) {
$errorCode = $response->errorCode(); // Check for specific codes (e.g., 100 = invalid number)
}
Number Formatting
+27821234567). The package doesn’t auto-format.Enable Debug Mode
Set debug to true in config/clickatell.php to log raw API responses:
'debug' => env('CLICKATELL_DEBUG', false),
Check HTTP Status Codes
Inspect the raw response for HTTP status codes (e.g., 401 Unauthorized):
$response->raw(); // Returns the full HTTP response
Validate API Credentials Test credentials manually via Clickatell’s API docs or Postman before integrating.
Common Errors
Custom Response Handling
Extend the Response class to add domain-specific logic:
namespace App\Services;
use Arcturial\Clickatell\Response as BaseResponse;
class ClickatellResponse extends BaseResponse {
public function isDelivered() {
return $this->errorCode() === 0;
}
}
Add New Endpoints
Extend the Clickatell class to support unsupported features (e.g., MMS):
class ExtendedClickatell extends \Arcturial\Clickatell\Clickatell {
public function sendMms(array $data) {
$data['type'] = 'mms';
return $this->post('mms/send', $data);
}
}
Webhook Integration
Use Laravel’s route:list to verify webhook routes and handle Clickatell’s delivery reports:
Route::post('clickatell/webhook', function (Request $request) {
$payload = $request->json()->all();
// Process delivery status (e.g., update DB)
});
Fallback Mechanisms Implement a fallback to another SMS provider if Clickatell fails:
try {
$response = Clickatell::send([...]);
} catch (\Exception $e) {
$response = FallbackSms::send([...]);
}
How can I help you explore Laravel packages today?