Back to Blog
Integration Guides

How to Send SMS with Twilio and Laravel: Step-by-Step Tutorial

Add Twilio SMS to your Laravel application using the Twilio PHP SDK with environment variable credential management and queue support.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
7 min read
Twilio
Integration
SDK
Mobile
How to Send SMS with Twilio and Laravel: Step-by-Step Tutorial

Twilio SMS and Laravel integrate through the Twilio PHP SDK installed via Composer, with credentials stored in Laravel's .env file and accessed through the config() helper, allowing you to dispatch SMS from service classes, queued jobs, or Artisan commands throughout your Laravel application. This integration is used by Laravel development teams building SaaS applications, e-commerce platforms, or internal tools that need SMS for OTP verification, order notifications, appointment reminders, or alert broadcasting, and who want to leverage Laravel's service container, queue system, and configuration management to build a maintainable SMS dispatch layer. The Twilio PHP client is initialized in a Laravel service class injected via the container, and long-running or high-volume SMS sends are dispatched through Laravel queues using Redis or database queue backends to keep HTTP response times fast.

What You Need Before You Start

Install the Twilio PHP SDK by running composer require twilio/sdk in your Laravel project root. Add your Twilio credentials to the .env file with TWILIO_SID, TWILIO_TOKEN, and TWILIO_FROM variables. Create a config/twilio.php configuration file that returns an array with 'sid' => env('TWILIO_SID'), 'token' => env('TWILIO_TOKEN'), and 'from' => env('TWILIO_FROM') so credentials are accessed through Laravel's config() helper rather than env() directly in service classes, which respects the config caching behavior of php artisan config:cache. Run php artisan config:clear after adding the new config file to ensure the configuration cache includes the new values. Provision a Twilio SMS-capable phone number and collect your Account SID and Auth Token from the Twilio Console.

Step-by-Step Integration Guide

Create a TwilioService class at app/Services/TwilioService.php that imports Twilio\Rest\Client and Twilio\Exceptions\RestException. In the constructor, instantiate the client with $this->client = new Client(config('twilio.sid'), config('twilio.token')), and define a sendSms(string $to, string $body): string method that calls $message = $this->client->messages->create($to, ['from' => config('twilio.from'), 'body' => $body]) inside a try block, returns $message->sid on success, and in the catch block logs the error using Log::error() and rethrows or returns null. Register TwilioService as a singleton in a service provider by calling $this->app->singleton(TwilioService::class, fn() => new TwilioService()) in the register method, enabling injection throughout the application. For queued SMS, create a SendSmsJob with php artisan make:job SendSmsJob, inject TwilioService in the constructor, call $this->twilioService->sendSms($this->to, $this->body) in the handle method, and dispatch from controllers with SendSmsJob::dispatch($phone, $message)->onQueue('notifications').

Common Issues and How to Fix Them

The TwilioService constructor throws a Twilio\Exceptions\ConfigurationException when config('twilio.sid') returns null because php artisan config:cache was run before the twilio.php config file was added, or because the .env file is missing the TWILIO_SID variable. Run php artisan config:clear followed by php artisan config:cache after adding the config file and .env variables, and add a startup assertion in the TwilioService constructor that calls abort_if(empty(config('twilio.sid')), 500, 'Twilio SID is not configured') to surface misconfiguration immediately. Laravel queued jobs that call TwilioService fail silently when the queue worker process was started before the .env and config files were updated, because the worker caches the configuration at startup. Restart queue workers with php artisan queue:restart after changing configuration so workers reload fresh configuration on their next job attempt. The Twilio webhook POST to a Laravel route returns HTTP 419 with a CSRF token mismatch error because Twilio does not send a Laravel session cookie with XSRF-TOKEN. Add the Twilio webhook route to the $except array in app/Http/Middleware/VerifyCsrfToken.php and add Twilio signature validation using the Twilio SDK's RequestValidator in a custom middleware to replace CSRF protection.

How to Get More from This Integration

Build Laravel Notifications support for Twilio SMS by creating a TwilioChannel class that implements the handle method accepting a Notifiable and Notification, calls TwilioService::sendSms with the notifiable's routeNotificationFor('twilio') phone number, and returning the message SID, then defining a toTwilio method in your notification classes that returns a TwilioMessage value object with the body string, enabling $user->notify(new OrderConfirmed($order)) syntax throughout your application. Add per-tenant Twilio credentials in a multi-tenant SaaS application by loading the tenant's Account SID, Auth Token, and from number from the database in the TwilioService constructor based on the current tenant context set by a middleware, constructing a separate Client instance per tenant, and caching the client instance keyed by tenant ID to avoid re-instantiating for every message within a request. Implement SMS delivery tracking by adding a statusCallback URL to the messages->create options array pointing to a dedicated Laravel route, creating a database migration for an sms_messages table with sid, to, body, status, and updated_at columns, and updating the status column in the webhook controller action each time Twilio posts a status change.

Conclusion

Laravel and Twilio together provide a well-structured SMS integration that uses Laravel's service container, queue system, and configuration management to keep dispatch logic maintainable at scale. Contact Telphi Consulting to implement and optimize the Twilio SMS integration in your Laravel application.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.