Yii2 component - Sms Sender
Description
Yii2 component which allow simple send sms using multiply providers.
Switching between providers supports using yii config file (better), (*1)
Common params
return [
//...
'components' => [
//...
'sms' => [
'class' => 'demmonico\sms\Sender',
'senderNumber' => 'name' or 'number',
'provider' => [
'class' => 'demmonico\sms\Nexmo',
'apiKey' => '***',
'apiSecret' => '***',
],
],
],
];
or DI, (*2)
$component = \Yii::createObject('demmonico\sms\Nexmo', [
[
'class' => 'demmonico\sms\Sender',
'senderNumber' => 'name' or 'number',
'provider' => [
'class' => 'demmonico\sms\Nexmo',
'apiKey' => '***',
'apiSecret' => '***',
],
],
]);
or using config component's bootstrap method (see https://github.com/demmonico/yii2-config), (*3)
in config file, (*4)
return [
//...
'components' => [
//...
'sms' => [
'class' => 'demmonico\sms\Sender',
'senderNumber' => 'name' or 'number',
'provider' => [
'class' => 'demmonico\sms\Nexmo',
'apiKey' => [
'component' => 'config',
'sms.Nexmo.apiKey',
],
'apiSecret' => [
'component' => 'config',
'sms.Nexmo.apiSecret',
],
],
],
],
];
and in local params file, (*5)
return [
//...
'sms.Nexmo.apiKey' => '******',
'sms.Nexmo.apiSecret' => '******',
];
Now available Nexmo provider only. But you can add any external class with your custom provider.
New providers can be added by creating class, which will implements demmonico\sms\SmsProviderInterface
and extends (optional) demmonico\sms\BaseProvider
., (*6)
Debug params
For debug you can use redirect option and dummy option. They can use separately or together.
If redirect option redirectNumber
is set then all messages will be send to this number.
If dummy option dummyNumbers
is set and field $to matches to any of dummyNumbers elements then process send will be skip and all sms fields will be logged, (*7)
return [
//...
'components' => [
//...
'sms' => [
//...
'debug' => [
'redirectNumber' => 'number',
'dummyNumbers' => [
'number',
//...
],
],
],
],
];
Usage
Send sms
Yii::$app->sms->sendSms('Hello, world!', 'number');
or, (*8)
Yii::$app->sms->sendSms('Hello, world!', 'recipientNumber', 'senderNumber');
Numbers must be in E.164 format.
Method returns number (integer) of sent sms., (*9)
Get account balance
Yii::$app->sms->getBalance();
Method returns balance value (float)., (*10)
Get account numbers
Yii::$app->sms->getNumbers();
Method returns array of numbers which are associate with account., (*11)