GithubHelp home page GithubHelp logo

cakephp-sms's Introduction

CakePHP Sms Plugin

Build Status Minimum PHP Version License Total Downloads

Send SMS with CakePHP.

Usage

App::uses('CakeSms', 'Sms.Network/Sms');

$CakeSms = new CakeSms('default');
$CakeSms->to('+491234567890');
$CakeSms->from('+841234567890');
$CakeSms->send('Hello world!');

Installation via Composer

"require": {
	"fotografde/cakephp-sms": ">=1.0.0"
}

Configuration

Load plugin in Config/bootstrap.php

CakePlugin::load('Sms');

Create Config/sms.php

class SmsConfig {
	public $default = array(
		'transport' => 'Clickatell', // will use class ClickatellSmsTransport
	);
}

Implement a transport class under Lib/Network/Sms/. We recommend implementing Xi SMS, this way for example:

/**
 * Send SMS through SMS provider Clickatell
 */

use Xi\Sms\Gateway\ClickatellGateway;

App::uses('AbstractSmsTransport', 'Sms.Network/Sms');

class ClickatellSmsTransport extends AbstractSmsTransport {

	const CLICKATELL_API_ID = 'XXXX';
	const CLICKATELL_USER = 'YYYY';
	const CLICKATELL_PASSWORD = 'ZZZZ';

	/**
	 * Sends an SMS Through Clickatell
	 * We could also consider using this library: http://github.com/arcturial/clickatell
	 *
	 * @param CakeSms $sms
	 * @return bool Success
	 */
	public function send(CakeSms $sms) {
		$gw = new ClickatellGateway(
			self::CLICKATELL_API_ID,
			self::CLICKATELL_USER,
			self::CLICKATELL_PASSWORD
		);

		$service = new Xi\Sms\SmsService($gw);

		$msg = new Xi\Sms\SmsMessage(
			$sms->message(),
			self::parsePhoneNumber($sms->from()),
			self::parsePhoneNumber($sms->to())
		);

		$response = $service->send($msg);

		return !empty($response);
	}
	
	/**
	 * Parses a phone number to fit Clickatell requirements
	 * from +49123[...] to 49123[...]
	 *
	 * @param array|string $phoneNumber
	 * @return array|string|bool
	 */
	public static function parsePhoneNumber($phoneNumber) {
		if (is_array($phoneNumber)) {
			return array_map('self::parsePhoneNumber', $phoneNumber);
		}
		if (preg_match('/^\+([0-9]+)$/', (string) $phoneNumber, $matches)) {
			return $matches[1];
		}
		return false;
	}
}

cakephp-sms's People

Contributors

beinbm avatar felixmaier1989 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.