zelenin/smsru
Laravel/PHP client for sms.ru: send SMS, check delivery status, query balance, and manage sender names via the SMS.ru API. Lightweight wrapper with simple methods for common operations and integration into PHP apps.
Installation Add the package via Composer:
composer require zelenin/smsru
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
// ...
Zelenin\SmsRu\SmsRuServiceProvider::class,
],
Configuration Publish the config file:
php artisan vendor:publish --provider="Zelenin\SmsRu\SmsRuServiceProvider" --tag="config"
Update config/smsru.php with your SMS.ru API credentials:
'api_id' => env('SMSRU_API_ID'),
'api_password' => env('SMSRU_API_PASSWORD'),
First Use Case: Sending an SMS
Inject the SmsRu facade or service into a controller/console:
use Zelenin\SmsRu\Facades\SmsRu;
public function sendSms()
{
$response = SmsRu::send([
'to' => '79123456789',
'text' => 'Test message from Laravel',
]);
return $response->success ? 'SMS sent!' : 'Failed: ' . $response->error;
}
Sending SMS
SmsRu::send(['to' => '79123456789', 'text' => 'Hello']);
SmsRu::send([
'to' => '79123456789',
'text' => 'Hello',
'from' => 'YourBrand', // Sender ID
'translit' => true, // Transliterate Cyrillic
]);
Checking Balance
$balance = SmsRu::balance();
Retrieving Send Status
$status = SmsRu::status(['id' => 123456789]);
Batch Processing Use a loop for bulk sends (with rate-limiting awareness):
$phones = ['79123456789', '79123456790'];
foreach ($phones as $phone) {
SmsRu::send(['to' => $phone, 'text' => 'Batch message']);
sleep(1); // Avoid hitting API limits
}
use Zelenin\SmsRu\Jobs\SendSms;
SendSms::dispatch(['to' => '79123456789', 'text' => 'Async message']);
event(new SmsSent($response));
\Log::info('SMS Response', $response->toArray());
Deprecated Package
php-smsru.Rate Limiting
429 Too Many Requests.Phone Format
79123456789, not 9123456789).'phone' => 'required|regex:/^7\d{10}$/'
Character Limits
mb_strlen() for accurate counting:
if (mb_strlen($text, 'UTF-8') > 160) {
throw new \InvalidArgumentException('Message too long');
}
Error Handling
if (!$response->success) {
throw new \RuntimeException($response->error ?? 'Unknown error');
}
SmsRu::setDebug(true); // Logs raw API responses
SmsRu::setSandbox(true);
Custom Responses
Extend the Zelenin\SmsRu\Response class to add fields:
class ExtendedResponse extends \Zelenin\SmsRu\Response {
public function getCost() { return $this->data['cost'] ?? 0; }
}
API Wrapper
Replace the underlying Guzzle client for custom logic:
$client = new \GuzzleHttp\Client(['base_uri' => 'https://sms.ru/']);
SmsRu::setClient($client);
Laravel Notifications Integrate with Laravel’s notifications:
use Zelenin\SmsRu\Notifications\SendSmsNotification;
Notification::route('sms', '79123456789')
->notify(new SendSmsNotification('Hello'));
How can I help you explore Laravel packages today?