Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Email

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Send Text Message With PHP

A text message, as probably every reader of this article knows, is an SMS message to a mobile phone. "SMS" stands for "Short Message Service".

An SMS text message to a mobile phone can be sent with the standard PHP mail() function. (An SMS text message can also be sent with your regular email app, if you know the email address to send it to.)

It really is relatively easy to code SMS sending functionality with PHP.

Note that mobile phone carriers (companies that provide telephone service) have implemented effective spam prevention. If you send over a certain number of SMS messages within a certain period of time, or trigger other spam-prevention algorithms the carrier has, your IP address is likely to be banned from their service.

Consider it prudent to use the PHP mail() function for sending only occasional SMS text messages.

Any replies to SMS messages sent with the PHP mail() function, as this article shows you how to do, are sent to your email address as an email.

This article has a form you can use for sending SMS text messages — instead of requiring you to update the PHP source code with destination and message every time you want to send one.

Mobile phone carriers have an internet domain through which SMS is sent. The SMS domain is likely to be different than the company's business domain.

Using the method described in this article, SMS messages are sent to a special email address. The address is the person's telephone number in front of the "@" character and the carrier's SMS domain after the "@" character. Example:

1234567890@example.com

If you know the SMS email address, you can send a message with your email app. The subject line won't display in the text message. ("SMS" can be used as the subject should your app require a subject to be filled in.)

The software provided with this article can be used to determine the SMS email address. The software can, optionally, also send a custom SMS message for you using the PHP mail() function.

Two things are required to determine the SMS email address:

  1. The telephone number of the mobile phone where the SMS is to be received.

    Only the digits are used to construct the email address, no hyphens, dots, spaces, parenthesis, or any other characters that may be in place for making the telephone number more human readable.

  2. The SMS domain of the carrier for the mobile phone where the SMS is to be received.

    Lists of SMS domains are available at various places on the internet. I compiled a list of carriers operating in the USA and Canada for the dropdown of the SMS sending form (presented later in this article).

    As of writing of this article, this free carrier lookup site can provide the information you need to select the correct carrier or to update the dropdown list.

As you can see, when the telephone number and the carrier's SMS domain are both known, an SMS email address can be constructed.

Realize that Short Message Service really does mean short. The maximum message length can vary from carrier to carrier. If the message is longer than a carrier permits, it might truncate the message or it might drop the message without delivering it.

If your message stays under 160 characters, it should fit into all maximum length requirements — assuming all characters are plain regular-keyboard text. If the message has any extended characters (curly quotes, for example), the message length may need to be as low as 70 characters.

The SMS Sending Form for Your Website

The SMS sending part of this demonstration form has been disabled. But it can still be used to obtain an SMS email address for sending with your own email app — send it the way you normally send an email, but use the SMS email address for the "To" line.

SMS Sending Form

*Telephone number of destination mobile phone.

*Carrier for destination mobile phone.

The SMS email address (above fields are required): The SMS address may be copied for your email sending app.


If you wish to also send an SMS message via this SMS sending form, fill in the following fields.

Your "From:" email address for the SMS.

The message to be sent (remember to keep it short). (Characters: )

Here is the source code for the form. Notes follow.

<form method="post" action="https://example.com/secret/sms.php" style="margin:0;">
<h3 style="margin-top:0;">SMS Sending Form</h3>
<p>
*Telephone number of destination mobile phone. 
<input type="text" style="width:100%; font-size:1em;" id="mobilenumber" name="mobilenumber" onchange="SMS_UpdateEmailAddress()">
</p>
<p>
*Carrier for destination mobile phone. 
<select style="width:100%; font-size:1em;" id="mobilecarrier" name="mobilecarrier" onchange="SMS_UpdateEmailAddress()">
<option value="">- Select Carrier -</option>
<option value="sms.3rivers.net">3 River Wireless</option>
<option value="paging.acswireless.com">ACS Wireless</option>
<option value="message.alltel.com">Alltel</option>
<option value="txt.att.net">AT&T</option>
<option value="txt.bellmobility.ca">Bell Canada</option>
<option value="txt.bell.ca">Bell Mobility (Canada)</option>
<option value="txt.bellmobility.ca">Bell Mobility</option>
<option value="blueskyfrog.com">Blue Sky Frog</option>
<option value="sms.bluecell.com">Bluegrass Cellular</option>
<option value="myboostmobile.com">Boost Mobile</option>
<option value="bplmobile.com">BPL Mobile</option>
<option value="cwwsms.com">Carolina West Wireless</option>
<option value="mobile.celloneusa.com">Cellular One</option>
<option value="csouth1.com">Cellular South</option>
<option value="cwemail.com">Centennial Wireless</option>
<option value="messaging.centurytel.net">CenturyTel</option>
<option value="txt.att.net">Cingular (Now AT&T)</option>
<option value="msg.clearnet.com">Clearnet</option>
<option value="comcastpcs.textmsg.com">Comcast</option>
<option value="corrwireless.net">Corr Wireless Communications</option>
<option value="mobile.dobson.net">Dobson</option>
<option value="sms.edgewireless.com">Edge Wireless</option>
<option value="fido.ca">Fido</option>
<option value="sms.goldentele.com">Golden Telecom</option>
<option value="messaging.sprintpcs.com">Helio</option>
<option value="text.houstoncellular.net">Houston Cellular</option>
<option value="ideacellular.net">Idea Cellular</option>
<option value="ivctext.com">Illinois Valley Cellular</option>
<option value="inlandlink.com">Inland Cellular Telephone</option>
<option value="pagemci.com">MCI</option>
<option value="page.metrocall.com">Metrocall</option>
<option value="my2way.com">Metrocall 2-way</option>
<option value="mymetropcs.com">Metro PCS</option>
<option value="fido.ca">Microcell</option>
<option value="clearlydigital.com">Midwest Wireless</option>
<option value="mobilecomm.net">Mobilcomm</option>
<option value="text.mtsmobility.com">MTS</option>
<option value="messaging.nextel.com">Nextel</option>
<option value="onlinebeep.net">OnlineBeep</option>
<option value="pcsone.net">PCS One</option>
<option value="txt.bell.ca">President's Choice</option>
<option value="sms.pscel.com">Public Service Cellular</option>
<option value="qwestmp.com">Qwest</option>
<option value="pcs.rogers.com">Rogers AT&T Wireless</option>
<option value="pcs.rogers.com">Rogers Canada</option>
<option value="satellink.net">Satellink</option>
<option value="email.swbw.com">Southwestern Bell</option>
<option value="messaging.sprintpcs.com">Sprint</option>
<option value="tms.suncom.com">Sumcom</option>
<option value="mobile.surewest.com">Surewest Communicaitons</option>
<option value="tmomail.net">T-Mobile</option>
<option value="msg.telus.com">Telus</option>
<option value="txt.att.net">Tracfone</option>
<option value="tms.suncom.com">Triton</option>
<option value="utext.com">Unicel</option>
<option value="email.uscc.net">US Cellular</option>
<option value="txt.bell.ca">Solo Mobile</option>
<option value="uswestdatamail.com">US West</option>
<option value="vtext.com">Verizon</option>
<option value="vmobl.com">Virgin Mobile</option>
<option value="vmobile.ca">Virgin Mobile Canada</option>
<option value="voicestream.net">VoiceStream</option>
<option value="sms.wcc.net">West Central Wireless</option>
<option value="cellularonewest.com">Western Wireless</option>
</select>
</p>
<p>
The SMS email address (above fields are required): 
<input type="text" style="width:100%; font-size:1em;" id="mobileaddress" name="mobileaddress" onclick="SMS_UpdateEmailAddress()"> 
The SMS address may be copied for your email sending app.
</p>
<script type="text/javascript">
function SMS_UpdateEmailAddress()
{
   var d1 = document.getElementById("mobilenumber");
   var d2 = document.getElementById("mobilecarrier");
   var d2x = d2.selectedIndex;
   var d3 = document.getElementById("mobileaddress");
   d3.value = "";
   if(d1.value.match(/\d/) && d2x>0){d3.value=d1.value.replace(/\D/g,"")+"@"+d2.options[d2x].value;}
}
function SMS_CountMessageCharacters(d,id)
{
   document.getElementById(id).value = d.value.length;
}
</script>
<hr style="height:6px; border-radius:6px 6px; border:3px solid #ccc; background-color:#ccc;">
<p>
If you wish to also send an SMS message via this SMS sending form, fill in the following fields.
</p>
<p>
Your "From:" email address for the SMS. 
<input type="text" style="width:100%; font-size:1em;" id="mobilefrom" name="mobilefrom">
</p>
<p>
The message to be sent (remember to keep it short). 
<textarea style="width:100%; height:.75in; font-size:1em;" id="mobilemessage" name="mobilemessage" onkeyup="SMS_CountMessageCharacters(this,'chars')"></textarea> 
(Characters: <input type="text" style="width:3em; font-size:1em;" id="chars" name="chars">)
</p>
<p>
<input type="submit" value="Send SMS" style="width:100%;">
</p>
</form>

If you will only use the form to construct SMS sending email addresses (for use in your email sending app), then the above form is all you need. Paste it into a web page and load it into your browser.

But if you also want to use the form to send SMS messages, you'll need to upload the SMS-sending PHP script (below) on your server and then change the above form's action value https://example.com/secret/sms.php to the URL of your PHP script.

Here is the source code of the SMS-sending PHP script.

<?php
// SMS Sender
// July 3, 2021
// Will Bontrager Software LLC
// https://www.willmaster.com/
if( empty($_POST['mobilenumber']) or empty($_POST['mobilecarrier']) or empty($_POST['mobilefrom']) or empty($_POST['mobilemessage']) )
{
   echo 'The destination phone number, carrier, from email address, and message are all required.';
   exit;
}
$_POST['mobilefrom'] = trim($_POST['mobilefrom']);
$_POST['mobilemessage'] = trim($_POST['mobilemessage']);
$_POST['mobilenumber'] = preg_replace('/\D/','',$_POST['mobilenumber']);
echo <<<MESSAGE
<div style="border-left:9px solid #ccc; padding-left:1em;">
<b>Sending SMS message &hellip;</b>
<p>From:<br>{$_POST['mobilefrom']}</p>
<p>To:<br>{$_POST['mobilenumber']}@{$_POST['mobilecarrier']}</p>
<p>Message:<br>{$_POST['mobilemessage']}</p>
<b>
MESSAGE;
mail("{$_POST['mobilenumber']}@{$_POST['mobilecarrier']}",'SMS',$_POST['mobilemessage'],"From: {$_POST['mobilefrom']}");
echo '<b>&hellip; Message Sent.</b></div>';
?>

No customization is required. Simply save the above source code as sms.php (or other *.php file name) and upload it to the server.

It is prudent to put SMS sending software into a password-protected directory. A spammer could ruin your reputation, not only with your hosting company but also with mobile phone carriers.

The action value https://example.com/secret/sms.php of the form needs to be changed to the URL of your sms.php script. Then, the form can be used for sending SMS text messages.

The SMS Form can be used for either or both (a) obtaining an SMS email address for using with regular email apps and (b) sending an SMS text message.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC