We build custom web applications
to grow your business.

cakePHP text message and email function in appcontroller

If you are using your application or website to send emails or text messages from different controllers, it would be easier for you to set this up just once in the appcontroller and then call these function from any controller.

In your appcontroller add the following

// send mail
	public function sendMail($emailto, $subject, $message){
		if(is_array($emailto)) {
			foreach($emailto as $email) {
				$Email = new CakeEmail();
				$Email->to($email);
				$Email->subject($subject);
				$Email->replyTo('no_reply@yourwebsite.com');
				$Email->from('no_reply@yourwebsite.com');
				$Email->emailFormat('html');
				$Email->template('default');		
				$Email->send($message);
				$Email = '';
			}
		} else {
			$Email = new CakeEmail();
			$Email->to($emailto);
			$Email->subject($subject);
			$Email->replyTo('no_reply@yourwebsite.com');
			$Email->from('no_reply@yourwebsite.com');
			$Email->emailFormat('html');
			$Email->template('default');		
			$Email->send($message);
		}
	}	
	
	//send text message 
	public function textMessage($phoneNumber, $carrier, $message) {
		$ms = wordwrap($message, 65);
		$carrier_list = Array(
			'att'           => 'txt.att.net',
			'verizon'       => 'vtext.com',
			'tmobile'       => 'tmomail.net',
			'sprint'        => 'messaging.sprintpcs.com',
			'nextel'        => 'messaging.nextel.com'
 		 );
		mail($phoneNumber.'@'.$carrier_list[$carrier], '', $ms); 
	} 

To send out text messages add this in any controller you would like to send out a message
$message = 'your message';
$this->textMessage('5555555555', 'sprint', $message);

To send out email just add the following in your controller
$this->sendMail($email, 'subject', $message);

$email can be one email or an array of email addresses.

// ADDING UNIQUE DATA TO DATABASE

if ($this->Demo->isUnique(array(
					'User.first_name'=>$this->request->data['User']['first_name'], 
					'User.last_name'=>$this->request->data['User']['last_name']					
					), false
					)) {
					$this->Demo->Save($this->request->data['Demo']);
					unset($this->request->data['Demo']);
				}

// REMOVING VALIDATION ON CERTAIN ACTIONS OR FIELDS

unset($this->Customer->validate['ended']); // for field

novalidate' => true // in form field

if($this->action =='edit') { // in beforeFilter
$this->Security->validatePost = false;
$this->Security->enabled = false;
$this->Security->csrfCheck = false; }