Back to Blog
Language How-Tos

How to Send MMS with Media in Twilio Using PHP

Send MMS messages with image attachments from PHP using the Twilio SDK with proper media URL formatting and size limit handling.

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

This tutorial shows how to send MMS messages with image and media attachments using the Twilio PHP SDK's mediaUrl option in the messages create call. It targets PHP developers who need to include visual content in outbound messages such as product images, receipts, or QR codes sent directly to customers' phones. You will send an MMS with a publicly accessible media URL and verify the attachment arrives on the recipient's device alongside the message text.

Prerequisites and Setup

Install the Twilio SDK with composer require twilio/sdk vlucas/phpdotenv and configure your credentials in a .env file. Your Twilio number must support MMS, which requires a US or Canadian long code or short code; numbers in other countries or voice-only numbers cannot send MMS. The media file must be hosted at a publicly reachable HTTPS URL that returns the correct MIME type in its response headers, because Twilio fetches the file server-side before attaching it to the message. Supported formats include JPEG, PNG, GIF, and WebP for images up to 5 MB per file, with a maximum of ten attachments per MMS message.

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

The mediaUrl key in the options array accepts an array of HTTPS URLs, each pointing to a media file Twilio will attach to the MMS. You can include up to ten URLs in the array to send multiple images in a single message. Twilio transcodes the media to match destination carrier capabilities, so the format that arrives on the handset may differ slightly from what you uploaded. The returned message object has the same sid and status properties as a standard SMS message, giving you a consistent way to track MMS delivery.

<?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'     => 'Here is your confirmation image.',
            'mediaUrl' => ['https://example.com/confirmation.png'],
        ]
    );
    echo 'MMS 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

If Twilio throws a RestException with status code 21617, the media URL is not publicly reachable or is returning a non-200 HTTP response; use PHP's get_headers() to verify the URL responds correctly before passing it to the API. Error code 21620 means the media type is not supported; convert the file to JPEG, PNG, or MP4 before re-uploading to your host. On trial accounts MMS is still limited to verified destination numbers just like SMS, so test with a number you have verified in the Console. Inspect the Twilio Console under Messaging Logs and click the message SID to see the media fetch status and whether Twilio successfully retrieved your file.

Taking It Further

Generate images dynamically using the GD library or Imagick extension in PHP, write the output to a temporary file or stream, upload it to S3 with a public ACL using the AWS SDK for PHP, and pass the S3 URL to mediaUrl. Combine MMS with the StatusCallback feature to track when media messages are delivered separately from standard SMS delivery tracking. For marketing campaigns, A/B test different product images by randomly selecting one from an array and recording which version produced higher response rates. Delete S3 objects after the message SID confirms delivery to avoid accumulating unused media files and incurring unnecessary storage costs.

Contact Telphi Consulting to build a rich media messaging workflow integrated into your PHP or 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.