954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Send text messages with PHP

0
By TheVenerableZ on Jun 20th, 2010 12:54 pm

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;		
	}
}

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

shubhamjain1
Junior Poster in Training
56 posts since Oct 2009
Reputation Points: 17
Solved Threads: 14
 

That's pretty awesome, thanks!

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

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

dottomm
Junior Poster in Training
89 posts since Nov 2007
Reputation Points: 9
Solved Threads: 5
 

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

sam023
Junior Poster
164 posts since Jun 2009
Reputation Points: 11
Solved Threads: 6
 
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);
EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

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

gaurang4
Newbie Poster
21 posts since Jul 2010
Reputation Points: 11
Solved Threads: 1
 

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

jane.n@live.com
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

chrishea
Nearly a Posting Virtuoso
1,428 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
 

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:

ﻼim
Junior Poster
171 posts since Aug 2010
Reputation Points: 10
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: