TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   SMS Texting class (http://www.talkphp.com/script-giveaway/5573-sms-texting-class.html)

Arictos 09-15-2010 03:10 PM

SMS Texting class
 
Hey there. I haven't found a lot of resources on the web regarding PHP->SMS stuff, most of it is send text to ##########@txt.att.net (or other specified provider), in some cases you can specify a return address, other times it's completely fail and specifies your nameservers instead of a domain, and other random interesting quirks.

So anyway, here is what I ended up putting together.

Things it supports:
  • You only need to specify the 10 digit number, not the carrier
  • Verizon headers to ensure a proper return address
  • Other carriers support the reply "mask" that you can specify rather than an email

Note: I've only tested this on Verizon and AT&T, so if you have another carrier, please test it and let me know if it works the same on there, but I believe it will preform well.

It's really not that great of work, but hopefully it'll save someone the hassle of having to do it again. If you have anything that would improve it, feel free to add on :)
PHP Code:

<?php
        
    
/**
     * Short Message Service (SMS) class
     * 
     * This class is meant to provide communication to a mobile
     * phone, regardless of the carrier provided.
     * 
     * @author        Chase Hutchins    <arictos@gmail.com>
     * @version        0.1
     */
    
class SMS
    
{
        
/**
         * Extensions of all supported carriers
         * 
         * @var        array
         */
        
protected $carriers                = Array(
            
'Airtel (18 circles)'        => 'aircel.co.in',
            
'Airtel (Andhra Pradesh)'    => 'airtelap.com',
            
'Airtel (Karnataka)'        => 'airtelkk.com',
            
'Alaska Communication'        => 'msg.acsalaska.com',
            
'Alltell Wireless'            => 'message.alltel.com',
            
'AQL'                        => 'text.aql.com',
            
// AT&T is used a the "default" recieving party, so we do not need to resend to them
            //'AT&T'                        => 'txt.att.net',
            
'AT&T Godfathered'            => 'mmode.com',
            
'AT&T Paging'                => 'page.att.net',
            
'Bell Mobility'                => 'txt.bell.ca or',
            
'Boost Mobile'                => 'myboostmobile.com',
            
'Bouygues Telecom'            => 'mms.bouyguestelecom.fr',
            
'Loop (BPL Mobile)'            => 'bplmobile.com',
            
'Cellular One'                => 'mobile.celloneusa.com',
            
'Cingular'                    => 'cingular.com',
            
'Centennial Wireless'        => 'cwemail.com',
            
'Cincinnati Bell'            => 'gocbw.com',
            
'Cingular (GoPhone)'        => 'cingulartext.com',
            
'Claro (Brazil)'            => 'clarotorpedo.com.br',
            
'Claro (Nicaragua)'            => 'ideasclaro-ca.com',
            
'Claro (Puerto Pico)'        => 'vtexto.com',
            
'Comcel'                    => 'comcel.com.co',
            
'Cricket'                    => 'sms.mycricket.com',
            
'CTI Movil'                    => 'sms.ctimovil.com.ar',
            
'Emtel'                        => 'emtelworld.net',
            
'Fido'                        => 'fido.ca',
            
'Freebie SMS'                => 'smssturen.com',
            
'General Communications'    => 'mobile.gci.net',
            
'Globalstar'                => 'msg.globalstarusa.com',
            
'Golden State Cellular'        => 'gscsms.com',
            
'Helio'                        => 'myhelio.com',
            
'Iridium'                    => 'msg.iridium.com',
            
'i-wireless (Sprint PCS)'    => 'iwirelesshometext.com',
            
'Koodo Mobile'                => 'msg.telus.com',
            
'MetroPCS'                    => 'mymetropcs.com',
            
'Movicom'                    => 'movimensaje.com.ar',
            
'Mobitel (Sri Lanaka)'        => 'sms.mobitel.lk',
            
'MTN'                        => 'sms.co.za',
            
'MTS Mobility'                => 'text.mtsmobility.com',
            
'Nextel'                    => 'messaging.nextel.com',
            
'Nextel (Mexico)'            => 'msgnextel.com.mx',
            
'Orange'                    => 'orange.pl',
            
'Pioneer Cellular'            => 'zsend.com',
            
'Personal'                    => 'alertas.personal.com.ar',
            
'Pocket Wireless'            => 'sms.pocket.com',
            
'PC Telecom'                => 'mobiletxt.ca',
            
'Qwest Wireless'            => 'qwestmp.com',
            
'Rogers Wireless'            => 'pcs.rogers.com',
            
'SaskTel'                    => 'sms.sasktel.com',
            
'Sprint (PCS)'                => 'messaging.sprintpcs.com',
            
'Sprint (Nextel)'            => 'page.nextel.com',
            
'Suncom'                    => 'tms.suncom.com',
            
'Sunrise Communications'    => 'gsm.sunrise.ch',
            
'Syringa Wireless'            => 'rinasms.com',
            
'T-Mobile'                    => 'tmomail.net',
            
'T-Mobile (Austria)'        => 'sms.t-mobile.at',
            
'T-Mobile (Croatia)'        => 'sms.t-mobile.hr',
            
'TBaytel'                    => 'tbayteltxt.net',
            
'Telus Mobility'            => 'msg.telus.com',
            
'Tigo'                        => 'sms.tigo.com.co',
            
'Tracfone'                    => 'mmst5.tracfone.com',
            
'US Cellular'                => 'email.uscc.net',
            
'Verizon'                    => 'vtext.com',
            
'Viaero'                    => 'viaerosms.com',
            
'Vivo'                        => 'torpedoemail.com.br',
            
'Virgin Mobile'                => 'vmobl.com',
            
'Virgin Mobile (Canada)'    => 'vmobile.ca',
            
'Vodacom'                    => 'voda.co.za',
        );
        
        
/**
         * The mask name of the sending party
         * 
         * @var        string
         */
        
protected $mask;
        
        
/**
         * The email address of the sending party
         * 
         * @var        string
         */
        
protected $sender;
        
        
/**
         * Class constructor
         * 
         * @param    string    Mask name of the sending party, an example being your name
         * @param    string    The email address of the sending party
         * 
         * @return    void    No value is returned
         */
        
public function __construct($mask 'Unknown Party'$sender 'no-reply@unknown.com')
        {
            
$this->mask        = (string) $mask;
            
$this->sender    = (string) $sender;
            
            
ini_set('sendmail_from'$this->sender);
            
putenv('sendmail_from=' $this->sender);
        }
        
        
/**
         * Sends a text message.
         * 
         * @param    string  Recipient phone number
         * @param    string  Optional message subject
         * @param    string  Message for the recipient
         * 
         * @return    bool    True upon sucessful message sending, otherwise false
         */
        
public function send($number$subject null$message null)
        {
            
/**
             * Clean and trim the recieving number, removing anything
             * that's not a number, as well as preliminary zeros 
             */
            
$number trim(preg_replace('/([^0-9\.])/i'''$number));
            
$number trim(preg_replace('/(^[0]+)/i'''$number));
            
            
/**
             * Verify the recieving party is a valid phone number
             */
            
if(!$this->is_valid_phone($number))
            {
                return(
false);
            }
            
            
$headers        '';
            
$headers        .= 'MIME-Version: 1.0' "\r\n";
            
$headers        .= 'Content-type: text/html; charset=utf-8'"\r\n";    
            
$headers        .= 'To: ' $number '@txt.att.net' "\r\n";
            
$headers        .= 'From: ' $this->mask ' <' $this->sender '>' "\r\n";
            
$headers        .= 'Reply-To: ' $this->mask ' <' $this->sender '>' "\r\n";
            
$headers        .= 'X-Mailer: PHP/' PHP_VERSION "\r\n";
            
$headers        .= 'Bcc: ';
                        
            foreach(
$this->carriers as $carrier => $domain)
            {
                
$headers .= $number '@' $domain ',';
            }
            
            
$headers substr($headers0, -2) . "\r\n";
                        
            if(
mail($number '@txt.att.net'$subject$message$headers'-f ' $this->sender ' -r ' $this->sender))
            {
                return(
true);
            }
            
            return(
false);
        }
        
        
/**
         * Validates input to ensure it's considered a valid
         * phone number
         * 
         * @param    string    The number to validate
         * 
         * @return    bool    Returns true if the argument is considered valid, otherwise false
         */
        
public function is_valid_phone($number null)
        {            
            if(!
is_null($number) && !empty($number))
            {
                if(
preg_match('/^([0-9]( |-|.)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-|.)?([0-9]{3}( |-|.)?[0-9]{4}|[a-zA-Z0-9]{7})$/'$number))
                {
                    return(
true);
                }
            }
            
            return(
false);
        }
    }
    
?>


tony 09-15-2010 06:12 PM

Thanks for posting it, this might help me a bit with a project I am trying to do. I'll tweak it to make it more country-context portable, mainly because the project involves the carrier Movistar in Mexico.

Arictos 09-15-2010 07:32 PM

I was going to add other carriers such as that one (I just used a list on Wikipedia) - but I had to skip over some of them for sake of just making it work. But alot of carriers like to include the whole +44(digits)@theirdomain.com or (randomNumbers)(digits)@theirdomain.com sort of thing. I may implement ways of including these at another point in time, allowing access to all carriers, even if they have strange digits and what not. Perhaps a preg_replace on (sequence){DIGITS}@carrier.com or something like that.

If you make any modifications, I hope you'll publish them. I'd like to keep track of what people do with it :)


All times are GMT. The time now is 04:50 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0