Back to Blog
Language How-Tos

How to Send Your First SMS in Twilio Using PHP

Install the Twilio PHP SDK via Composer, configure your credentials, and send your first SMS in under 10 lines of PHP code.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
PHP
How-To
Code
How to Send Your First SMS in Twilio Using PHP

This tutorial walks through sending your first SMS with the Twilio PHP SDK, from installing the library via Composer through authenticating with your credentials and dispatching a real message to a phone number. It targets PHP developers who have a Twilio account and want to add programmable SMS to a script or web application. By the end, your terminal will print a message SID confirming Twilio queued the message for delivery.

Prerequisites and Setup

You need PHP 7.4 or higher with Composer installed, a Twilio account with a purchased phone number, and your Account SID and Auth Token from the Twilio Console. Run composer require twilio/sdk vlucas/phpdotenv to install the Twilio SDK and a dotenv library that keeps credentials out of source files. Create a .env file at your project root with three variables: TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER. Add .env to your .gitignore before committing any code to prevent accidentally publishing credentials.

composer require twilio/sdk vlucas/phpdotenv

# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567

The Complete Code

Load environment variables with the Dotenv library, then instantiate the Twilio client by passing your Account SID and Auth Token to new Client(). Call $client->messages->create() with the recipient number as the first argument and an options array containing from and body as the second argument. The method returns a message object whose sid property uniquely identifies the message across Twilio's platform. Printing the SID and status lets you immediately confirm the message was accepted and queued without checking the Console.

<?php
require_once 'vendor/autoload.php';

use TwilioRestClient;
use TwilioExceptionsRestException;

$dotenv = DotenvDotenv::createImmutable(__DIR__);
$dotenv->load();

$client = new Client($_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN']);

try {
    $message = $client->messages->create(
        '+15551234567',
        [
            'from' => $_ENV['TWILIO_PHONE_NUMBER'],
            'body' => 'Hello from Twilio and PHP!',
        ]
    );
    echo 'SID: ' . $message->sid . PHP_EOL;
    echo 'Status: ' . $message->status . PHP_EOL;
} catch (RestException $e) {
    echo 'Error ' . $e->getStatusCode() . ': ' . $e->getMessage() . PHP_EOL;
}

Testing and Debugging

Run php send-sms.php and verify a SID starting with SM prints to the terminal alongside a status of queued. If you see a RestException with status code 401, your Account SID or Auth Token is incorrect; copy them fresh from the Twilio Console. Error code 21608 in the exception message means the destination number is not verified under Verified Caller IDs, which trial accounts require. Navigate to Monitor in the Twilio Console to see the full delivery chain if the script exits cleanly but no SMS arrives on the handset.

Taking It Further

Wrap the send call in a function that accepts $to, $body, and optional parameters so it can be reused across multiple scripts and controllers without duplicating the client setup. Add a statusCallback key to the options array pointing to a webhook URL so Twilio POSTs delivery receipts to your server without you needing to poll. Replace the from number with a messagingServiceSid key to route messages through a Messaging Service that handles sender pool selection automatically. For Laravel applications, inject the Twilio client as a singleton in the service container so it is shared across requests without re-instantiating the SDK per call.

Contact Telphi Consulting to discuss building production-grade SMS workflows tailored to your PHP application stack.

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.