Professional Documents
Culture Documents
How To Send Text Messages With PHP - Envato Tuts+
How To Send Text Messages With PHP - Envato Tuts+
How To Send Text Messages With PHP - Envato Tuts+
Skip
to content
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. If you're looking to grow
your business, a PHP text message script can be crucial.
In this tutorial, you'll learn how to send a message to mobile using PHP code.
Premium Option
Before we get into the step-by-step process, you might want to look at a ready-made
solution: Smart SMS & Email Manager (SSEM), available on CodeCanyon. This script to
send SMS through PHP lets you:
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 1/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
If you want to learn how to send SMS through PHP code, here's how to do it.
Overview
Sending a text message (SMS) is actually pretty easy. Below is a simplified diagram of
how a message can be sent from a web application to a wireless device.
The message is composed using a web application that's stored and executed on
an HTTP server and then sent through the internet ("the cloud") as an email
message.
The email is received by a Short Message Service Gateway (SMS Gateway), which
converts the message from an email message to an SMS message.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 2/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
The SMS message is then handed to a Short Message Service Center (SMSC),
which is a server that routes data to specific mobile devices.
The message is finally transmitted over the wireless network to the recipient.
Most wireless networks have an 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's generally free—however, it's of course not a free service for the end
user. Fees still apply to the recipient, and messages sent via email will be billed as a
non-network text message.
Advertisement
Email to SMS
To learn how to send a message to mobile via email using PHP code, you'll generally
require only two things:
1. the phone number or unique identifier of the mobile device you want to reach
2. the wireless network's domain name (many can be found in this list of email to
SMS addresses)
1 phoneNumber@domainName.com
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 3/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
phoneNumber is the phone number of the mobile device to send the message to, and
domainName.com is the address for the network's SMS Gateway.
To send an SMS through PHP 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.
Pretty awesome! It's fun to think of the possibilities available when sending SMS from a
PHP website to mobile.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 4/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
1 bool mail ( string $to , string $subject , string $message [, string $additional_head
$to defines the receiver or receivers of the message. Valid examples include:
user@example.com
user1@example.com, user2@example.com
User <user@example.com>
User1 <user1@example.com>, User2 <user2@example.com>
$subject is rather self-explanatory; it should be a string containing the desired
subject. However, SMS messages do not require a subject.
$message is the message to be delivered. As mentioned in the PHP manual, "each
line should be separated with a LF (\n). Lines should not be larger than 70
characters."
To replicate the earlier functionality, we could write the following PHP code:
A Test Drive
Let's run a test with PHP to make sure that everything is set up correctly and that the
mail function will, in fact, send a text message. Using the following code, we can run:
1 <?php
2
3 var_dump( mail( '##########@vtext.com', '', 'This was sent with PHP.' ) ); // bool(tr
4
5 ?>
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 5/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
As you can see in the PHP send SMS example above, the message shows that it's 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 Usability
The Markup
With the basics out of the way, let's take this PHP text message script and wrap a user
interface around it. First, we'll set up a simple form to our send free SMS HTML code:
1 <!DOCTYPE html>
2 <head>
3 <meta charset="utf-8" />
4 </head>
5 <body>
6 <div id="container">
7 <h1>Sending SMS with PHP</h1>
8 <form action="" method="post">
9 <ul>
10 <li>
11 <label for="phoneNumber">Phone Number</label>
12 <input type="text" name="phoneNumber" id="phoneNumber" placeholder="385555016
13 <li>
14 <label for="carrier">Carrier</label>
15 <input type="text" name="carrier" id="carrier" />
16 </li>
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 6/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
17 <li>
18 <label for="smsMessage">Message</label>
19 <textarea name="smsMessage" id="smsMessage" cols="45" rows="15"></textarea>
20 </li>
21 <li><input type="submit" name="sendMessage" id="sendMessage" value="Send Messag
22 </ul>
23 </form>
24 </div>
25 </body>
26 </html>
Advertisement
The Style
Next, we'll sprinkle in some CSS:
1 body {
2 margin: 0;
3 padding: 3em 0;
4 color: #fff;
5 background: #0080d2;
6 font-family: Georgia, Times New Roman, serif;
7 }
8
9 #container {
10 width: 600px;
11 background: #fff;
12 color: #555;
13 border: 3px solid #ccc;
14 -webkit-border-radius: 10px;
15 -moz-border-radius: 10px;
16 -ms-border-radius: 10px;
17 border-radius: 10px;
18 border-top: 3px solid #ddd;
19 padding: 1em 2em;
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 7/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
20 margin: 0 auto;
21 -webkit-box-shadow: 3px 7px 5px #000;
22 -moz-box-shadow: 3px 7px 5px #000;
23 -ms-box-shadow: 3px 7px 5px #000;
24 box-shadow: 3px 7px 5px #000;
25 }
26
27 ul {
28 list-style: none;
29 padding: 0;
30 }
31
32 ul > li {
33 padding: 0.12em 1em
34 }
35
36 label {
37 display: block;
38 float: left;
39 width: 130px;
40 }
41
42 input, textarea {
43 font-family: Georgia, Serif;
44 }
Our "send free SMS" HTML code and CSS give us the following simple form:
The Script
The most important part of this is the PHP text message script. We'll write that bit of
code now:
1 <?php
2
3 if ( isset( $_REQUEST ) && !empty( $_REQUEST ) ) {
4 if (
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 8/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
The script first checks to see if the form has been submitted.
If yes, it checks to see if the phoneNumber , carrier , and smsMessage variables
were sent. This is useful in the case where there may be more than one form on
the page.
If phoneNumber , carrier , and smsMessage are available and phoneNumber and
carrier are not empty, it's okay to attempt to send the message.
The message argument in the mail function should be 70 characters in length per
line. We can chop the message into 70-character chunks using the wordwrap
function.
phoneNumber and carrier are concatenated, and then the message is sent using
the mail function.
If data is missing or it cannot be validated, the script simply returns Not all
information was submitted.
Finally, mail returns a boolean indicating whether it was successful or not. The
value is stored in $result in case I need to verify that the message was in fact
sent.
Note: The mail method only notifies whether the message was sent or not. It does not
provide a way to check to see if the message was successfully received by the recipient
server or mailbox.
1 <?php
2
3 if ( isset( $_REQUEST ) && !empty( $_REQUEST ) ) {
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 9/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
4 if (
5 isset( $_REQUEST['phoneNumber'], $_REQUEST['carrier'], $_REQUEST['smsMessage'] ) &&
6 !empty( $_REQUEST['phoneNumber'] ) &&
7 !empty( $_REQUEST['carrier'] )
8 ) {
9 $message = wordwrap( $_REQUEST['smsMessage'], 70 );
10 $to = $_REQUEST['phoneNumber'] . '@' . $_REQUEST['carrier'];
11 $result = @mail( $to, '', $message );
12 print 'Message was sent to ' . $to;
13 } else {
14 print 'Not all information was submitted.';
15 }
16 }
1 ?>
2 <!DOCTYPE html>
3 <head>
4 <meta charset="utf-8" />
5 <style>
6 body {
7 margin: 0;
8 padding: 3em 0;
9 color: #fff;
10 background: #0080d2;
11 font-family: Georgia, Times New Roman, serif;
12 }
13
14 #container {
15 width: 600px;
16 background: #fff;
17 color: #555;
18 border: 3px solid #ccc;
19 -webkit-border-radius: 10px;
20 -moz-border-radius: 10px;
21 -ms-border-radius: 10px;
22 border-radius: 10px;
23 border-top: 3px solid #ddd;
24 padding: 1em 2em;
25 margin: 0 auto;
26 -webkit-box-shadow: 3px 7px 5px #000;
27 -moz-box-shadow: 3px 7px 5px #000;
28 -ms-box-shadow: 3px 7px 5px #000;
29 box-shadow: 3px 7px 5px #000;
30 }
31
32 ul {
33 list-style: none;
34 padding: 0;
35 }
36
37 ul > li {
38 padding: 0.12em 1em
39 }
40
41 label {
42 display: block;
43 float: left;
44 width: 130px;
45 }
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 10/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
46
47 input, textarea {
48 font-family: Georgia, Serif;
49 }
50 </style>
51 </head>
52 <body>
53 <div id="container">
54 <h1>Sending SMS with PHP</h1>
55 <form action="" method="post">
56 <ul>
57 <li>
58 <label for="phoneNumber">Phone Number</label>
59 <input type="text" name="phoneNumber" id="phoneNumber" placeholder="385555016
60 <li>
61 <label for="carrier">Carrier</label>
62 <input type="text" name="carrier" id="carrier" />
63 </li>
64 <li>
65 <label for="smsMessage">Message</label>
66 <textarea name="smsMessage" id="smsMessage" cols="45" rows="15"></textarea>
67 </li>
68 <li><input type="submit" name="sendMessage" id="sendMessage" value="Send Messag
69 </ul>
70 </form>
71 </div>
72 </body>
73 </html>
Advertisement
Troubleshooting
Localhost Error
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 11/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
In order to use the mail function, you must have a mail server running. If you're
running this SMS PHP free script on a web host, you're probably okay. But if you're
unsure, I recommend talking to an administrator. This also holds true for personal
machines. So if you get errors like...
... you'll have to install and configure a mail server. This is out of the scope of this
tutorial. However, if you're 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').
PHPMailer
Another 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.
1 require 'class.phpmailer.php';
2
3 // Instantiate Class
4 $mail = new PHPMailer();
5
6 // Set up SMTP
7 $mail->IsSMTP(); // Sets up a SMTP connection
8 $mail->SMTPDebug = 2; // This will print debugging info
9 $mail->SMTPAuth = true; // Connection with the SMTP does require authorizati
10 $mail->SMTPSecure = "tls"; // Connect using a TLS connection
11 $mail->Host = "smtp.gmail.com";
12 $mail->Port = 587;
13 $mail->Encoding = '7bit'; // SMS uses 7-bit encoding
14
15 // Authentication
16 $mail->Username = "email.address@gmail.com"; // Login
17 $mail->Password = "password"; // Password
18
19 // Compose
20 $mail->Subject = "Testing"; // Subject (which isn't required)
21 $mail->Body = "Testing"; // Body of our message
22
23 // Send To
24 $mail->AddAddress( "##########@vtext.com" ); // Where to send it
25 var_dump( $mail->send() ); // Send!
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 12/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
It may take a little more to set up the connection, depending on your situation. If you're
planning on using PHP to send a text message free via Gmail, Google has provided
information on connecting.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 13/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 14/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
auto-responder
contact list creation
multilanguage support
delivery reports
messages in the admin panel
Not a bad batch of features to have for a PHP send text message free script.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 15/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
this PHP code to send SMS to mobile from website free script if you want to send deals
or product updates to customers.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 16/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
Conclusion
Learning how to send and receive a message in PHP can be simple. It's nice that there
are a myriad of methods to accomplish the task of sending an 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 how to send SMS through PHP
code, but it should get you started! I hope this PHP text message script tutorial has
been of interest to you. Thank you so much for reading!
If you still need help with this or any other PHP issue, try our free PHP tutorial, which
takes you through the fundamentals of PHP in a methodical, comprehensive set of
video classes. You can also learn more about PHP from the articles below.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 17/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
PHP Fundamentals
Jeremy McPeak
29 Oct 2021
This post has been updated with contributions from Nathan Umoh. Nathan is a staff
writer for Envato Tuts+.
Advertisement
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 18/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
Yes No
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss
out on learning about the next big thing.
Sign up
Kevin Jensen
My name is Kevin Jensen. I love video games and love making them. I want to
share that love. This site/blog is devoted to sharing my thoughts about video
games and also information about making video games. I do this in hopes of
helping others to achieve their dreams of also making games.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 19/20
9/7/23, 1:35 AM How to Send Text Messages With PHP | Envato Tuts+
Advertisement
ENVATO TUTS+
HELP
Envato Envato Elements Envato Market Placeit by Envato All products Careers Sitemap
© 2023 Envato Pty Ltd. Trademarks and brands are the property of their respective owners.
https://code.tutsplus.com/how-to-send-text-messages-with-php--net-17693t 20/20