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

by Jean. 10 Comments -

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 textmagic-sms-api-php.zip 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.

require('textmagic-sms-api-php/TextMagicAPI.php');

$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!

  • http://www.bit.ly/PersonalChefToGo Chris

    Try www.Twilio.com for $0.01 per text option and even click-to-call tools.

  • http://www.100webhosting.com Jasmine

    TextMagic looks really good. I will give it a try… sending some test SMS using the free 10 SMS credits. Thanks for sharing this great service!

  • https://twitter.com/#!/Valwriting_ valwriting.net

    Very nice!

  • http://www.telecomblog.info/ Nitin

    Thanks a lot, I was looking for something like this and I am going to do this for my wordpress blog now.

  • http://filmyboss.com Mohit Bumb

    I’m sms addict lol :p

  • http://www.sutanaryan.com/ Ryan S

    is this TextMagic work in other country? like Philippines or it only works in US based country?

  • http://www.csshtml.co Gautam Lakum

    I was looking for something like this. Thank you.
    You guys really rocks for us. :)

  • Mike T

    bulletin.net have global connectivity and provide free credits also. I used their messenger product a while ago for a project and it was like threaded webmail but with texting…. pretty cool!

  • amit

    is there any free api available for sms sending..

    • Mittul Chauhan

      no amit .. i do not think so ..