|
The Wanderer
Join Date: Jul 2010
Posts: 17
Thanks: 0
|
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($headers, 0, -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); } } ?>
|