These days text messaging is everywhere and not just for personal conversations. Companies are using text messages to send you coupons, let you know when you bills and statements are ready or that your dry cleaning can be picked up. Text messaging seems more convenient than making phone calls and creating your own PHP script to send these messages is a very easy thing to set up. Get on board with these other companies that have found how convenient text messages are for their customer and start integrating text messaging into your PHP applications. In this article, I will show you two ways to accomplish this and you will be surprised at how simple this is to do.
Using Carrier Email
If you have ever sent email in PHP using sendmail, this is almost as easy. Mobile phone carriers have an email address that you can send messages to, along with a phone number, and your message will be delivered as a text. You must know two pieces of information: the carrier and the phone number. Once you have this information you can look up the carrier’s email text address from this list: http://www.venture-ware.com/kevin/web-development/email-to-sms/
To send an email that will be delivered as a text message, you compose an email and the To address needs to be in the format of: phonenumber@carrierdomainaddress. You can test this in your email client on yourself if you want to see it in action. However, we are going to setup a simple PHP script to handle this for us.
Using PHP Mail()
The PHP mail function is small and easy to use for our purpose. Here is the method:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Here is an example. We want to send the message “Your car is ready!” to someone on AT&T, with the phone number 555-123-1234. Here is what our method would look like:
mail("firstname.lastname@example.org", "", "Your car is ready!", "From: Your Name<email@example.com>\r\n");
Done! All you would need to do is fill in your information as the sender and this is ready to go. There is no other coding or magic involved. Just call this bit of code when you have your information ready and away it goes.
Sending Text Messages with TextMagic
The downside to the above method is that you need to know the mobile carrier for the person you want to text. Since this probably isn’t a question you want to ask all of your customers, there is an alternative. A Web service called TextMagic provides an API for you to send text messages and is available in several popular languages such as PHP, Perl, Python, Java, etc…To send text messages with TextMagic you do not need to know the carrier and it very easy to configure.
The first step would be signing up for a TextMagic account: http://www.textmagic.com/. The pricing is pretty reasonable considering how impressed your customers will be or if you are using it for advertising purposes. Once you have an account, you can download the API and set it up to send your text message. Here is an example:
// Include the TextMagic PHP lib require('textmagic-sms-api-php/TextMagicAPI.php'); // Set the username and password information $username = 'myusername'; $password = 'mypassword'; // Create a new instance of TM $router = new TextMagicAPI(array( 'username' => $username, 'password' => $password )); // Send a text message to '555-123-1234' $result = $router->send('Your car is ready!', array(5551231234), true); // result: Result is: Array ( [messages] => Array (  =>5551231234 ) [sent_text] =>Your car is ready! [parts_count] =>1 )
This example is using the same phone number and message from above, but is calling the TextMagic API to do the heavy lifting as far as knowing the carrier and sending the message. The result from the send call is an array of information you can use to get the status of your message. You will need to know the ID number of your message (11111111 in our example) and use the
messageStatus() method to pull up the information about it. You can check the status like this:
// Get the message send status by received ID $result = $router->messageStatus(array(11111111)); // result: Array (  => Array ( [text] =>Your car is ready! [status] => d [created_time] => 1325261093 [reply_number] => 447624800500 [completed_time] => 1325261120 [credits_cost] =>1 ) )
As you can see, the TextMagic service is really easy to use but if you need a free solution, the first example is still an excellent way to implement text messaging into your applications or for your business. This is a great way to make you stand out from the crowd or to impress your friends. If anything, you have learned a fun new way to use PHP!
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|Jun 23-24||Web Development with PHP/MySQL|
|- Classroom - Online|