This tutorial builds a complete voicemail system in PHP using Twilio TwiML that plays a professional greeting to callers, records their message, and sends an SMS notification to your team with the recording details. It targets PHP developers who need programmable voicemail with transcription and notification support without dedicated hardware. You will build two PHP scripts: one for the greeting and recording flow, and one that receives the recording details as a callback webhook after the call ends.
Prerequisites and Setup
Install the packages with composer require twilio/sdk vlucas/phpdotenv and configure your Twilio number's voice webhook to POST to your greeting.php script so every inbound call (or no-answer transfer) triggers the voicemail flow. Store the notification recipient's phone number as an environment variable so it can be changed without modifying PHP code. The system uses two PHP files: greeting.php returns the greeting and Record verb TwiML, and recorded.php receives the recording details, sends an SMS notification, and speaks a thank-you message. Expose your local server with ngrok during development since all callback URLs must be publicly accessible.
composer require twilio/sdk vlucas/phpdotenv
# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
NOTIFICATION_NUMBER=+15559998888
The Complete Code
The greeting script uses $response->say() with a professional message and then $response->record() with a 120-second maximum duration, a 5-second silence timeout, transcription enabled, and an action URL pointing to recorded.php. Setting 'playBeep' => true on the Record verb plays a tone so callers know when to start speaking. The recorded.php script reads the caller's number, recording SID, and duration from POST, logs them, and sends an SMS notification to the team using the Twilio client. A thank-you message built with VoiceResponse and returned from recorded.php confirms to the caller their message was saved before the call ends.
<?php
// greeting.php
require_once 'vendor/autoload.php';
use TwilioTwiMLVoiceResponse;
$response = new VoiceResponse();
$response->say(
'You have reached Telphi Consulting. We are unable to take your call right now. ' .
'Please leave your name, number, and a brief message after the tone, ' .
'and we will return your call within one business day.',
['voice' => 'alice']
);
$response->record([
'timeout' => 5,
'maxLength' => 120,
'transcribe' => true,
'action' => 'https://xxxx.ngrok.io/recorded.php',
'finishOnKey' => '#',
'playBeep' => true,
'transcribeCallback' => 'https://xxxx.ngrok.io/transcription.php',
]);
$response->say('We did not receive a message. Goodbye.', ['voice' => 'alice']);
header('Content-Type: text/xml');
echo $response;
<?php
// recorded.php
require_once 'vendor/autoload.php';
use TwilioRestClient;
use TwilioTwiMLVoiceResponse;
use TwilioExceptionsRestException;
$dotenv = DotenvDotenv::createImmutable(__DIR__);
$dotenv->load();
$caller = $_POST['Caller'] ?? 'Unknown';
$duration = $_POST['RecordingDuration'] ?? '0';
$recSid = $_POST['RecordingSid'] ?? '';
error_log('New voicemail from ' . $caller . ' (' . $duration . 's) SID: ' . $recSid);
$client = new Client($_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN']);
try {
$client->messages->create($_ENV['NOTIFICATION_NUMBER'], [
'from' => $_ENV['TWILIO_PHONE_NUMBER'],
'body' => 'New voicemail from ' . $caller . ' (' . $duration . 's). ' .
'SID: ' . $recSid . '. Check the Twilio Console to listen.',
]);
} catch (RestException $e) {
error_log('Notification failed: ' . $e->getMessage());
}
$response = new VoiceResponse();
$response->say('Your message has been recorded. Thank you and goodbye.', ['voice' => 'alice']);
header('Content-Type: text/xml');
echo $response;
Testing and Debugging
Call your Twilio number, listen to the greeting, leave a 10-second test message, and press # or hang up. Within 30 to 60 seconds you should receive the SMS notification with the recording SID. Check the PHP error log for the recorded.php log entry to confirm Twilio posted the recording details to the action URL. If the SMS notification fails, check the error log for a caught RestException and verify your notification number is verified under Verified Caller IDs if you are on a trial account.
Taking It Further
Store each voicemail's recording SID, caller, duration, and timestamp in a database and build a simple Laravel or plain PHP admin page that lists new voicemails sorted by date with play links. Add an audio playback controller that streams the recording MP3 from Twilio's API through your PHP server so the admin UI does not expose your Twilio credentials to the browser. Set up a cron job using a system crontab or Laravel's scheduler to download recordings to S3 using the AWS PHP SDK and delete them from Twilio with $client->recordings($sid)->delete() after a configurable retention period. Send email notifications using PHP's mail() function or a library like SwiftMailer with the recording details and transcript in the body for team members who prefer email alerts over SMS.
Contact Telphi Consulting to build a production PHP voicemail management system with transcription, notifications, and a web playback interface.
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)
Be the first to comment
No comments yet. Share your thoughts below.