|
SMS from your website
Text messaging has become extremely widespread throughout the world -- to the point where an increasing number of web applications have integrated SMS to notify users of events, sales or coupons directly through their mobile devices.
In this tutorial, we will cover the fundamentals of sending text messages with PHP.
OverviewSending a text message (SMS) message is actually pretty easy. Below is a simplified diagram of how a message can be sent from a web application to a wireless device. We'll break this down -- one piece at a time:
Most wireless networks have a SMS gateway through which email messages can be sent as text messages to a mobile device. This is nice, because, from a developer's standpoint, it is generally free—however, it is of course not a free service for the end user. Fees still apply to the recipient of the message and messages sent via email will be billed as a non-network text message. Email to SMSTo send a SMS via email, you'll generally require only two things:
The following convention can be followed for most carriers: phoneNumber@domainName.com
To send a text to Mr. Example, you could simply add 3855550168@vtext.com to any email client, type a message and hit send. This will send a text message to phone number +1 (385) 555-0168 on the Verizon Wireless Network. For example, I'll send a text message to myself using Gmail. When my phone receives the message, it should look like so:Function mail()Let's take things a step further. Using the SMS Gateway, we can send a text message via email using PHP's mail function. The mail function has the following signature: bool mail ( string $to, string $subject, string $message [, string $additional_headers [, string $additional_parameters ]])You can read more about it here.
To replicate the earlier functionality, we could write the following PHP code: mail( '3855550168@vtext.com', '', 'Testing'); A Test Drive Let's run a test with PHP to make that sure everything is setup correctly and that the <?php var_dump( mail( '##########@vtext.com', '', 'This was sent with PHP.'));//bool(true) ?> When my phone receives the message, it looks like so: As you can see in the image above, the message shows that it is from Gmail. This is because I route all my outgoing messages from my local server through that service. Unfortunately, as of this writing, I have been unsuccessful at altering the From header to reflect an alternate address. It seems that the email headers are stripped and replaced with headers prepared by the SMS gateway. If anyone knows of a workaround, please leave a comment and let the rest of us know!Adding UsabilityThe Markup With the basics out of the way, let's take this idea and wrap a user interface around it. First we'll set up a simple form:<!DOCTYPE html> <head> <meta charset="utf-8"/> </head> <body> <div id="container"> <h1>Sending SMS with PHP</h1> <form action="" method="post"> <ul> <li> <label for="phoneNumber">Phone Number</label> <input type="text" name="phoneNumber" id="phoneNumber" placeholder="3855550168"/></li> <li> <label for="carrier">Carrier</label> <input type="text" name="carrier" id="carrier"/> </li> <li> <label for="smsMessage">Message</label> <textarea name="smsMessage" id="smsMessage" cols="45" rows="15"></textarea> </li> <li><input type="submit" name="sendMessage" id="sendMessage" value="Send Message"/></li> </ul> </form> </div> </body> </html> The StyleNext we'll sprinkle in some CSS:body { margin: 0; padding: 3em 0; color: #fff; background: #0080d2; font-family: Georgia, Times New Roman, serif; } #container { width: 600px; background: #fff; color: #555; border: 3px solid #ccc; -webkit-border-radius: 10px; -moz-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px; border-top: 3px solid #ddd; padding: 1em 2em; margin: 0 auto; -webkit-box-shadow: 3px 7px 5px #000; -moz-box-shadow: 3px 7px 5px #000; -ms-box-shadow: 3px 7px 5px #000; box-shadow: 3px 7px 5px #000; } ul { list-style: none; padding: 0; } ul > li { padding: 0.12em 1em } label { display: block; float: left; width: 130px; } input, textarea { font-family: Georgia, Serif; } The ScriptThe most important part to this is the PHP script. We'll write that bit of code now:<?php if ( isset( $_REQUEST) && !empty( $_REQUEST)) { if ( isset( $_REQUEST['phoneNumber'], $_REQUEST['carrier'], $_REQUEST['smsMessage']) && !empty( $_REQUEST['phoneNumber']) && !empty( $_REQUEST['carrier']) ) { $message=wordwrap( $_REQUEST['smsMessage'], 70); $to=$_REQUEST['phoneNumber'] . '@' . $_REQUEST['carrier']; $resalt=@mail( $to, '', $message); print 'Message was sent to ' . $to; } else { print 'Not all information was submitted.'; } } ?>
Note: The The Final Code<?php if ( isset( $_REQUEST) && !empty( $_REQUEST)) { if ( isset( $_REQUEST['phoneNumber'], $_REQUEST['carrier'], $_REQUEST['smsMessage']) && !empty( $_REQUEST['phoneNumber']) && !empty( $_REQUEST['carrier']) ) { $message=wordwrap( $_REQUEST['smsMessage'], 70); $to=$_REQUEST['phoneNumber'] . '@' . $_REQUEST['carrier']; $resalt=@mail( $to, '', $message); print 'essage was sent to ' . $to; } else { print 'Not all information was submitted.'; } } ?> <!DOCTYPE html> <head> <meta charset="utf-8"/> <style> body { margin: 0; padding: 3em 0; color: #fff; background: #0080d2; font-family: Georgia, Times New Roman, serif; } #container { width: 600px; background: #fff; color: #555; border: 3px solid #ccc; -webkit-border-radius: 10px; -moz-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px; border-top: 3px solid #ddd; padding: 1em 2em; margin: 0 auto; -webkit-box-shadow: 3px 7px 5px #000; -moz-box-shadow: 3px 7px 5px #000; -ms-box-shadow: 3px 7px 5px #000; box-shadow: 3px 7px 5px #000; } ul { list-style: none; padding: 0; } ul > li { padding: 0.12em 1em } label { display: block; float: left; width: 130px; } input, textarea { font-family: Georgia, Serif; } </style> </head> <body> <div id="container"> <h1>Sending SMS with PHP</h1> <form action="" method="post"> <ul> <li> <label for="phoneNumber">Phone Number</label> <input type="text" name="phoneNumber" id="phoneNumber" placeholder="3855550168"/></li> <li> <label for="carrier">Carrier</label> <input type="text" name="carrier" id="carrier"/> </li> <li> <label for="smsMessage">Message</label> <textarea name="smsMessage" id="smsMessage" cols="45" rows="15"></textarea> </li> <li><input type="submit" name="sendMessage" id="sendMessage" value="Send Message"/></li> </ul> </form> </div> </body> </html> TroubleshootingLocalhost ErrorIn order to use the mail function, you must have a mail server running. If you're running this on a web host, you're probably okay. But if you are unsure, I recommend talking to an administrator. This also holds true for personal machines. So if you get errors like.. Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\sms\mail-test.php...you will have to install and configure a mail server. This is out of the scope of this tutorial. However, if you are working on your local machine, switching to something like XAMPP might solve this problem. Alternatively, try installing Mercury Mail alongside WAMP, MAMP or on a LAMP (or SAMP or OAMP, etc.) system (that's a lot of 'AMPs'). PHPMailerAnother option (which is the method I prefer) is to use PHPMailer. Below is an example of how to use PHPMailer to connect to Gmail's SMTP server and send the message. Using it is as simple as including a class in your script. require 'class.phpmailer.php'; //Instantiate Class $mail=new PHPMailer(); //Set up SMTP $mail->IsSMTP(); //Sets up a SMTP connection $mail->SMTPDebug =2; //This will print debugging info $mail->SMTPAuth=true; //Connection with the SMTP does require authorization $mail->SMTPSecure="tls"; //Connect using a TLS connection $mail->Host="smtp.gmail.com"; $mail->Port=587; $mail->Encoding='7bit'; //SMS uses 7-bit encoding //Authentication $mail->Username ="email.address@gmail.com";//Login $mail->Password ="password";//Password //Compose $mail->Subject="Testing"; //Subject (which isn't required) $mail->Body="Testing"; //Body of our message //Send To $mail->AddAddress( "##########@vtext.com");//Where to send it var_dump( $mail->send()); //Send! It may take a little more to set up the connection depending on your situation. If you're planning on using Gmail, Google has provided information on connecting. ConclusionThere are a myriad of methods to accomplish the task of sending a SMS through a web application. This method is really meant for low volume messaging (most likely less than 1,000 text messages per month) and developers looking to get their feet wet without forking out cash. Other options include:
This tutorial is in no way a comprehensive review of sending tex messages with PHP; but it should get you started! I hope this tutorial has been of interest to you. Thank you so much for reading! Sourse tutsplus | SMS for free Phone nuber for one time use SMS to the computer SMS to Internet-pagers How to delete the SMS banner Forget my number HOW close the site page SMS registration Unsubscribe from the SMS |