Send text messages with PHP

TheVenerableZ 0 Tallied Votes 2K Views Share

I made a PHP class that dramatically simplifies sending text messages with PHP. I know this isn't really a "help me out!" type question per se, but I would like to share the code because I have found it to be tremendously useful. You're free to do whatever you'd like with the code . You can even go around telling people you made it. Just don't accuse me if anything goes wrong.

Example Usage:

Cell::send('5551234567', VERIZON, 'Hi man, this message will be automatically paginated to be sent via text messages. How cool is that? Plus, you'll get page numbers in case the messages are received out of order! Awesome!');
// Carrier email suffixes
define('ATT',                 'txt.att.net');
define('SPRINT',              'messaging.sprintpcs.com');
define('TMOBILE',             'tmomail.net');
define('US_CELLULAR',         'email.uscc.net');
define('VERIZON',             'vtext.com');
define('VIRGIN_MOBILE',       'vmobl.com');

// Message parameters
define('MAX_SMS_LENGTH',       140);
define('DEFAULT_CELL_SENDER',  'sender@example.com');

class Cell
{
	public static function send($pNumber, $pCarrier, $pMessage)
	{
		// Keep a notifier of whether the message was sent or not
		$Success = true;
		
		// Define the recipient address
		$Recipient = $pNumber . '@' . $pCarrier;
		
		// Find out how many message will have to be sent
		$MessageCount = ceil(strlen($pMessage) / MAX_SMS_LENGTH);
		
		for ($i = 0; $i < $MessageCount; $i++)
		{
			// Calculate the subset of the entire message that can be sent at once
			$StartIndex = $i * MAX_SMS_LENGTH;
			$Message = stripslashes(substr($pMessage, $StartIndex, MAX_SMS_LENGTH));
			
			// Display page numbers on messages that span multiple iterations
			if ($MessageCount > 1)
			{
				$Message .= ' (' . ($i + 1) . '/' . $MessageCount . ')';
			}
			
			// Send the message
			$Success &= mail($Recipient, null, $Message, 'From: ' . DEFAULT_CELL_SENDER);
		}
		
		return $Success;		
	}
}
shubhamjain1 7 Junior Poster in Training

Great if works! Can you give the a similar list of providers for Indian cell providers?

EvolutionFallen 107 Junior Poster

That's pretty awesome, thanks!

dottomm -2 Junior Poster in Training

Nice. Could someone post an example of how to implement this?

sam023 0 Junior Poster

i think to send sms.. u need sms gateway..

EvolutionFallen 107 Junior Poster

Nice. Could someone post an example of how to implement this?

This should do the trick:

$num = 1234567890;
$car = "ATT";
$mssg = "Hello World!";
$cellphone = new Cell();
$cellphone->send($num, $car, $mssg);
gaurang4 1 Newbie Poster

Do i need sms gatewy for this ? plz someone clear it.

jane.n@live.com 0 Newbie Poster

How would I code a history file to add information on who text who and what time

chrishea 182 Nearly a Posting Virtuoso

You don't need an SMS gateway. Messages are sent as email messages.

ﻼim 0 Junior Poster

not working. is this the code way should be?

<html>
<body>

Text Sample : PHP text message

<?php

// Carrier email suffixes
define("ATT",                 "txt.att.net");
define("SPRINT",              "messaging.sprintpcs.com");
define("TMOBILE",             "tmomail.net");
define("US_CELLULAR",         "email.uscc.net");
define("VERIZON",             "vtext.com");
define("VIRGIN_MOBILE",       "vmobl.com");


// Message parameters
define("MAX_SMS_LENGTH",       140);
define("DEFAULT_CELL_SENDER",  "my.email.@yahoo.com");

$num = 09123456789;
$car = "ATT";
$mssg = "Hello World!";
$cellphone = new Cell();
$cellphone = send($num, $car, $mssg);


class Cell
{
	public static function send($pNumber, $pCarrier, $pMessage)
	{
		// Keep a notifier of whether the message was sent or not
		$Success = true;
		
		// Define the recipient address
		$Recipient = $pNumber . "@" . $pCarrier;
		
		// Find out how many message will have to be sent
		$MessageCount = ceil(strlen($pMessage) / MAX_SMS_LENGTH);
		
		for ($i = 0; $i < $MessageCount; $i++)
		{
			// Calculate the subset of the entire message that can be sent at once
			$StartIndex = $i * MAX_SMS_LENGTH;
			$Message = stripslashes(substr($pMessage, $StartIndex, MAX_SMS_LENGTH));
			
			// Display page numbers on messages that span multiple iterations
			if ($MessageCount > 1)
			{
				$Message .= " (" . ($i + 1) . "/" . $MessageCount . ")";
			}
			
			// Send the message
			$Success &= mail($Recipient, null, $Message, "From: " . DEFAULT_CELL_SENDER);
		}
		
		return $Success;		
	}
}

Cell::send("09184395235", VERIZON, "Hi man, this message will be automatically paginated to be sent via text messages. 
How cool is that? Plus, you will get page numbers in case the messages are received out of order! Awesome!");

?>




</body>
</html>

got a display error of :

Text Sample : Hello World ! 1) { $Message .= " (" . ($i + 1) . "/" . $MessageCount . ")"; } // Send the message $Success &= mail($Recipient, null, $Message, "From: " . DEFAULT_CELL_SENDER); } return $Success; } } Cell::send("09184395235", VERIZON, "Hi man, this message will be automatically paginated to be sent via text messages. How cool is that? Plus, you will get page numbers in case the messages are received out of order! Awesome!"); ?>

regards everyone!
thanks :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.