Back to Blog
Language How-Tos

How to Send Bulk SMS in Twilio Using PHP

Loop through a recipient array in PHP and send bulk Twilio SMS with error handling, rate limiting, and delivery tracking per message.

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

This tutorial shows how to send SMS to a list of recipients in PHP using a foreach loop with per-message error handling and a one-second delay between sends to respect Twilio's default rate limit. It targets PHP developers building notification, reminder, or alert systems that must reach multiple users at once from a script or a queued job. You will iterate a recipient array, track successes and failures, and log a summary at the end without one bad number stopping the entire batch.

Prerequisites and Setup

Install the Twilio SDK with composer require twilio/sdk vlucas/phpdotenv and load your credentials from a .env file as shown in the first SMS tutorial. Prepare your recipients as a PHP array of associative arrays with at minimum a phone key in E.164 format; you can populate this from a database query using PDO or from a CSV file read with fgetcsv(). Twilio's free tier allows approximately one message per second per account, so a sleep(1) call after each send keeps you safely within that limit. In production, move bulk sends to a background queue using Laravel Queues or a similar mechanism so HTTP requests are not blocked waiting for all sends to complete.

composer require twilio/sdk vlucas/phpdotenv

# recipients.php
return [
    ['phone' => '+15551234567', 'name' => 'Alice'],
    ['phone' => '+15559876543', 'name' => 'Bob'],
    ['phone' => '+15554443322', 'name' => 'Charlie'],
];

The Complete Code

The script wraps each individual messages->create() call in a try/catch block that catches RestException and appends the error to a failures array without halting the loop. The sleep(1) call after each send creates the one-second pause needed to stay within Twilio's rate limit without a separate sleep library. Collecting results in $sent and $failed arrays lets you print an accurate summary at the end and optionally persist the results to a database. Printing the SID for each successful send gives you a reference to look up any message individually in the Twilio 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']);
$from       = $_ENV['TWILIO_PHONE_NUMBER'];
$recipients = require 'recipients.php';

$sent   = [];
$failed = [];

foreach ($recipients as $recipient) {
    try {
        $message = $client->messages->create(
            $recipient['phone'],
            [
                'from' => $from,
                'body' => 'Hi ' . $recipient['name'] . ', this is a message from Telphi.',
            ]
        );
        $sent[] = ['phone' => $recipient['phone'], 'sid' => $message->sid];
        echo 'Sent to ' . $recipient['phone'] . ' SID: ' . $message->sid . PHP_EOL;
    } catch (RestException $e) {
        $failed[] = ['phone' => $recipient['phone'], 'error' => $e->getMessage()];
        echo 'Failed: ' . $recipient['phone'] . ' - ' . $e->getMessage() . PHP_EOL;
    }
    sleep(1);
}

echo PHP_EOL . 'Sent: ' . count($sent) . '  Failed: ' . count($failed) . PHP_EOL;

Testing and Debugging

Start with a recipients array containing two or three verified numbers to confirm the loop, error handling, and rate limiting work before scaling up. If several messages fail with status code 21614, those numbers are landlines that cannot receive SMS; add a Twilio Lookup check before the send loop to filter them out. Check the Twilio Console under Messaging Logs to compare the SIDs in your output against what Twilio received, which helps reconcile partial batch failures. For very large recipient lists, consider using a Messaging Service SID in place of the from number to distribute sends across a sender pool and increase throughput.

Taking It Further

Replace the hardcoded recipients array with a PDO database query so the list is always current at send time without manual file maintenance. Add a statusCallback URL to each create() options array so Twilio POSTs delivery receipts to your server for per-recipient tracking. Use Laravel's Bus::batch() feature to run bulk sends as a batchable job chain with built-in progress tracking and failure callbacks in the queue. Store batch run results to a database table with a batch ID so you can audit delivery rates and identify carrier rejection patterns over multiple campaigns.

Contact Telphi Consulting to build a high-volume PHP bulk SMS pipeline with carrier compliance and real-time delivery reporting.

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.