The "Sending free SMS online" cookbook - Part 3
Background
This article is the 3rd post in a 5 part series cookbook on build a "send free sms" website. In the first post1, we explained why there is a huge interest in having a website that allows people to send free SMS and explained the basics behind some of these free sites. In the second post2, we explained how you can use the phone companies email to SMS gateways for sending SMS. We have give the code examples in Php because thats our favorite language but you could adapt the principles to the code of your choice.
In this article, we will explain some techniques that you can use to improve the success of sending SMS using email to SMS gateways.
Formatting Issues
So we have seen how easy it is to send an email to one of these phone company's email to SMS gateways. But most people dont know that each phone company can be slightly different formatting requirements. And if they dont receive the incoming email in the correct format then they will reject the email and your SMS wont get sent.
What type of formatting?
Some phone companies enforce some limitations and formating such as
- Maxmium length of the from address
- Maximum length of the Text Message
- Include (or dont include) the country code in the destination mobile number
- In some cases message subject needs to have the from address
Heres some additional information on the additional formatting requirements for the phone company email to SMS gateways:
http://www.livejournal.com/tools/textmessage.bml?mode=details
Get around these formatting rules
In your script, you should do some post processing to fix up the format of the email that you send. You will need to know for each phone company what limitations they impose
Writing a Script to handle formatting
Heres a simple way to get around these formatting rules. We have written some simple Php code that sets out the formatting requirements for each phone company.
First-off, we make an array where all the providers are listed along with some of the formatting limitations such as from_limit,msg_limit, etc.
$providers = array(
'United States' => array(
'airtouch' => array(
'name' => 'airtouch',
'notes' => 'Enter your phone number. Messages are sent to number@airtouchpaging.com.
This is ONLY for former AirTouch customers. Verizon Wireless customers should use Verizon Wireless instead.',
'fromlimit' => 20,
'msglimit' => 120,
'totlimit' => 120,
'cc_length' => 1,
'add_prefix' => "",
),
'centennial' => array(
'name' => 'centennial',
'notes' => 'Enter your phone number. Sent via http://www.centennialwireless.com',
'fromlimit' => 10,
'msglimit' => 110,
'totlimit' => 110,
'cc_length' => 1,
'add_prefix' => "",
),
),
'Italy' => array(
'timnet' => array(
'name' => 'timnet',
'notes' => '10-digit phone number. Goes to number@timnet.com.',
'fromlimit' => 30,
'msglimit' => 350,
'totlimit' => 350,
'cc_length' => 2,
'add_prefix' => "0",
),
'vodafoneit' => array(
'name' => 'vodafoneit',
'notes' => 'Enter your phone number. Messages are sent to number@sms.vodafone.it',
'fromlimit' => 20,
'msglimit' => 132,
'totlimit' => 132,
'cc_length' => 2,
'add_prefix' => "0",
)
)
);
Then just before we send an email message we do some pre-processing based on the limitations that we have specified. We have written some Php code to do this for us. In this example we pass in the providers array, the country and the selected carrier.
function send_in_provider_format($providers, $country, $provider, $msg, $from, $to) {
$prov = $providers[$country][$provider];
//
// truncate 'from' if it's too long for the given provider
//
if (strlen($from) > $prov['fromlimit']) {
$form = substr($msg->{'from'}, 0, $prov['fromlimit']);
}
//
// truncate 'to' to remove the country code and add any leading digits
//
$to = $prov['add_prefix'] . substr($to, $prov['cc_length']);
//
// now send the message, based on the provider
//
if ($provider == "airtouch")
{
send_mail(array (
'to' => "$to@sender.airtouchpaging.com",
'from' => "$from",
'body' => "$msg",
));
}
elseif ($provider == "centennial")
{
post_webform("http://www.centennialwireless.com/home/sms.php", array(
'deviceid' => $to,
'mess' => $msg,
'yournumber' => $from,
));
}
}
Sending the email
In the example above, you will see that we call a php function to actually send the email. You can write a simple script to send this email and you need to take into account the from address formating
function send_mail($args) {
$to = $args['to'];
$subject = $args['to']. $args['subject'];
$message = $args['body'];
$headers = "'From: ".$args['from']."'\r\n" .
"'Reply-To: ".$args['from']. "'\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, NULL, "-f".$args['from']."@noreply.com");
}
Posting to a webform
In the example above, you will see that we post to a webform instead of sending an email. This is because some phone companies want you to submit the SMS to be sent via a webform on their website rather than using an email to sms gateway.
Again we have written some simple php code (using Curl) to post to these companies web forms:function post_webform($url, $args)
{
$ch = curl_init( $url);
curl_setopt( $ch, CURLOPT_VERBOSE, 1);
curl_setopt( $ch, CURLOPT_HEADER, 1);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt( $ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt( $ch, CURLOPT_POST, 1);
//check if there are already http headers in url
if (strpos ($url, '?')) {
$linkstr = '&';
}
else {
$linkstr = '?';
}
$postfields = '';
foreach($args as $key => $val)
{
$postfields .= "{$linkstr}{$key}={$val}";
$linkstr = '&';
}
$postfields = str_replace(" ","+",$postfields);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postfields); // add POST fields
// perform post
$rr = curl_exec( $ch);
curl_close( $ch);
}
Whats next in the cookbook
As mentioned this is a cookbook on how to create your own free sms sending website. The next post of the 5 part cookbook will show how we can pull all of this together to create a website based around these simple concepts.
blog
Me2mobile welcomes new owner
Feb-20-2010
comments (0)
Me2mobile founders moving on!
Feb-08-2010
comments (0)
Happy New Year to all me2mobilers
Jan-06-2010
comments (0)
Your Christmas Gift - Happy Holidays
Dec-23-2009
comments (0)
New webtext service on me2mobile
Dec-16-2009
comments (0)
Texting fish in New York
Sep-29-2009
comments (0)
Twittering passengers alert to ferry delays
Sep-17-2009
comments (0)
The Joy of Text for Irish businesses
Jul-15-2009
comments (0)
Like seeing in the dark
Jul-03-2009
comments (0)
Hello World
Jun-15-2009
comments (1)
