0

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;		
	}
}
9
Contributors
9
Replies
16
Views
7 Years
Discussion Span
Last Post by ﻼim
0

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

Edited by sam023: n/a

0

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);
0

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

Edited by gaurang4: n/a

0

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:

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.