nnrudakov/yii2-sms

Send SMS via different services in Yii2.

v2.0.1 2022-06-21 19:38 UTC

README

Yii2 extension to send SMS via different services.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads License

Requirements

  • PHP >= 8.1 (use v1.3.1 if you need PHP earlier 8.0)
  • Yii2 >= 2.0.41

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist nnrudakov/yii2-sms

or add

"nnrudakov/yii2-sms": "^2.0"

to the require section of your composer.json file.

Configuration

Add the following in your config:

...
    'components' => [
        'sms' => [
            'class' => nnrudakov\sms\Sms::class,
                'services' => [
                    'beeline' => [
                        'class'    => nnrudakov\sms\services\beeline\Beeline::class,
                        'user'     => '',
                        'password' => ''
                    ]
                ]
        ],
        ...
    ],
...

You can override message translations:

...
    'components' => [
        'i18n' => [
            'translations' => [
                'sms' => [ // must be the same name
                    'class'            => yii\i18n\PhpMessageSource::class,
                    'basePath'         => '@app/messages',
                    'sourceLanguage'   => 'ru',
                    'forceTranslation' => true
                ],
            ],
        ],
        ...
    ],
...

As an example full list messages you can find in russian message file.

Usage

Once the extension is installed, simply use it in your code by:

 $service = Yii::$app->sms->getService('beeline');
 $service->send(['+7905XXXXXXX'], 'message');

Extension may throw exceptions in critical situations or fill up internal erorrs list. You can checkout errors by:

 $service->hasErrors();

To get full errors list just call:

 $service->getErrors();

To get error for certain phone number add number as parameter:

 $service->getErrors('+7905XXXXXXX');

Services

Extension implements these services:

  • Beeline. Signed contract and credentials to account are required. You should paste credentials in service config:
...
    'beeline' => [
        'class'    => nnrudakov\sms\services\beeline\Beeline::class,
        'user'     => 'beeline_user',
        'password' => 'beeline_password'
    ]
...

Tests

For tests uses Codeception.

vendor/bin/codecept run unit