Sending SMS with PHP and TextMagic: An A to Z guide

Over the years, Short message service (SMS) has become a very important way of communication, and many businesses are looking for easy ways to send automated text messages to their customers. In this tutorial, I'm going to show you how you can send SMS using PHP and a third party service called TextMagic. Its very easy to do!

Step 1: Creating a TextMagic account

In order to send SMS to mobile phones, you need to use a third-party service. In this tutorial, I’m using a service called TextMagic to send SMS. There are lots of other services to do so, but I’ve tested Textmagic and I’m very happy with it so far.

To use TextMagic, you have to create an account. Go here and register. Prices starts at $27 for 200 SMS (13.5 cents per sms)

Step 2: Configure your API account

Once you have created your TextMagic account, go to My services → API → Password. You need to generate an API password in order to be able to send SMS from your website. Just enter your account password and click on “Generate API password”.

Once done, you have to download the API files. Go to this page and click on the “Download” button to get the API files. Extract the file and upload all of its content to your server.

Step 3: Let’s code!

Now, we have to create a function that will send the SMS throught the TextMagic API. The following code, taken from TextMagic website is the easiest way to send a SMS using the TextMagic API.


$api = new TextMagicAPI(array(
    "username" => "yourTextMeUsername", 
    "password" => "yourTextMePassword"

$text = "Your message here";

// Use this number for testing purposes. This is absolutely free.
$phones = array(9991234567);

$results = $api->send($text, $phones, true);

So how does this code works? First, I’m including the TextMagic API. Then we have to instantiate a TextMagicAPI object. To do so, you need your TextMagic username as well as your API password. $phones is an array that can contains how many phone numbers as you want. Useful for bulk text messaging!

Finally, the SMS is sent using the sent method. For testing purposes, you can use the 9991234567 number provided by TextMagic. It is totally free!

Now, how to make sure your SMS has been sent? Easy, just see if $results is true:

if ($results = $api->send($text, $phones, true)){
    echo "SMS sent successfully!";

You’re done. This is basically all you need to send text messages with TextMagic.
By the way, if anyone ever heard of a free and reliable service to send SMS, please let me know in a comment!