How to schedule an SMS Marketing message with PHP and the Twilio Messaging API

Table of Contents

As the world becomes increasingly digital, smartphones begin to play more of a vital role in our daily lives. These pocket-size devices help us to work more efficiently, socialise with anyone from any part of the world and grant us access to infinite information.

And should all of this activity become rather intense, we can simply unwind with some retail therapy, thanks again to our smartphones. It’s no wonder we’re addicted.

An average person checks their smartphone 160 times a day, 34% of people read their text messages within 5 minutes of receiving them and with an average response rate of 45%, it’s clear that smartphones are valuable real estate for any determined digital marketer.

Sure, social media and email marketing might generate all the buzz, but for any brand serious about leaving their competition in the dust, SMS is a channel that cannot be ignored.

SMS marketing is very effective because it provides direct access to your customers or leads, in a way that is more personal. For this reason, it is perfect for those scenarios where you want to drive up holiday promotions, send secret sales alerts or even just the occasional birthday greeting.

By including your product or business link in your text message, you can massively scale your engagement rate because open rates for text messages outperform email.

Currently, at 98% versus just 20% for email, mobile text messaging may be the closest thing in the information-overloaded digital marketing world to a guaranteed read.

However, it appears that many companies would rather run an ad campaign using morse code than go near SMS marketing. If you’re amongst the SMS-shy marketers, don’t worry because there’s hope.

On January 12 2022, Twilio released a Message Scheduling feature and we’re going to use this to execute an SMS marketing campaign.

In this tutorial, you will learn how to send scheduled SMS marketing messages using PHP and Twilio Messaging API.

Tutorial Requirements

Open a Twilio account and get a phone number

If you’re new to Twilio, then the first thing to do is to sign up for free and purchase a phone number. Twilio offers $10 credit to all new users, which you can use for this project.

Once you’ve signed up, you need to choose your Twilio phone number and to do this, log into your console and click on “Explore Products”.

Image of Twilio’s console dashboard.

Then scroll to the bottom of the page and under “Super Network”, click on “Phone numbers”. Then click the “Buy a number” option and on this page, you may proceed to select your number. Although you can choose a number that is specific to your country be aware that not all countries have the full capabilities of Voice, SMS, MMS and Fax.

I’m in London and Twilio’s UK numbers don’t have the capabilities of MMS, so I’m going to choose a US number. This is because I want a number that gives me the flexibility to create whatever I want.

Now click the search button to see a list of available numbers. If you don’t like the first list presented to you, then click the “refresh results” button at the bottom of the page to get a whole new list. Click the “buy” button next to the number that you want to purchase.

Messaging Service Setup

Next, you need to set up the messaging service. Click the “Explore Products” once more and this time choose the “Messaging” option. Click on services and then click the “Create Messaging Service” button.

For this tutorial, I’m notifying my users of a Valentines Day Promo, so I’ll type “Valentines Day Promo” in the “Messaging Service friendly name” text field. You can choose whatever name you want. Then on the drop-down list for “select what you want to use Messaging for”, click on the “notify my users” option.

When you’re done click on the “Create Messaging Service” button so that you can go to step two of this setup. Next, you need to add the sender. A sender represents a channel, number type or messaging type that can send and receive messages using the Twilio Messaging API.

Here you’ll be adding the Twilio number you just purchased, so click the “Add Senders” button to begin. Choose the “Phone Number” option and click on continue.

Click on the checkbox next to the number you want to add and then click the “Add Phone Numbers” button.

Now click the “Step 3: Set up integration” button. You don’t need to update any information on this page, so go ahead and click the “Step 4: Add compliance info” button. Again, there are no changes to be made for this page, so click the “Complete Messaging Service Setup” button.

Now let’s test that it works by clicking on the “Try sending a message” button.

In the “To phone number” text field, add your verified personal phone number. In the “From Messaging Service SID” choose your newly created messaging service. For the “Body Text” field, I have added a Valentines Day call to action message, but you can write whatever you want. Click the “Send test SMS” button and then confirm that your phone has received the message.

Congratulations on successfully setting up your new messaging service. It’s now time to write some PHP code so that we can schedule our SMS marketing messages.

Let’s create the PHP project

First, you need to create a project directory, then switch to it and install the Twilio PHP Helper Library. To do all of this, type out the following in a terminal.

To be able to send an SMS message with Twilio, your PHP application requires access to your account credentials and to do this, you need to copy and paste the following lines of code to the terminal.

Next, we’ll create a file named “sms-scheduler.php” to house all of the code for scheduling the SMS marketing message that we’ll send to our users. To do this, run the following code in the project’s root directory.

We now need to write the code to schedule the SMS marketing message.

Let’s write some code

Open the sms-scheduler.php file, then copy and paste the following code inside it.

The code starts by requiring all the dependencies that you need. Then a new Twilio object is initialised with your Twilio Account SID and Token values. You then create an SMS to send to your user by firstly adding your Twilio verified personal number.

Next, you need to include the “Messaging Service ID” that you created earlier. You can locate this by going into your console and clicking on “services” under “Messaging.”

The “body” field, contains the actual message that you want to send. The “send_at,” sets up the exact date and time for the text message to be sent. At the moment, this feature only allows a message to be scheduled an hour to 7 days in advance.

The “scheduleType” describes the type of scheduling to set up. Again, this feature only currently allows the option for “fixed.” Finally, the code ends with a command to print out the “$sid” number you used to initialize the Twilio object in the beginning.

Let’s go for a test drive

To test that the code works, start the application using PHP’s built-in server by running the command below in the root directory of your project.

Next, open your browser to and assuming all is successful, you should see your account “$id” printed on the screen. You’ll need to wait for an hour to receive your text message.

Congratulations on completing this tutorial!

You now have all the tools and knowledge you need to programmatically schedule an SMS marketing campaign.

get the best real-world tutorial, right to your inbox!

    let's get social!

    recent posts

    Get the best real-world projects, right to your inbox!