Pular para o conteúdo principal

SMS Client

One interface to send SMS through any provider, and the right provider chosen by the phone number's country.

Sponsor Build Status Opensource ByJG Install MCP Server GitHub source GitHub license GitHub release

Why​

SMS providers differ by country: one is cheap or reliable in the US, another in Brazil. Each has its own SDK, so supporting two means two code paths, and the choice is spread wherever a message is sent.

SMS Client puts the providers behind one interface, chosen by the connection URI, and can route each message by the destination country:

ProviderFactory::registerServices("twilio://accountId:authToken@default", "+1");
ProviderFactory::registerServices("byjg://username:password@default", "+55");

ProviderFactory::createAndSend("+12221234567", new Message("Hello USA!")); // Twilio
ProviderFactory::createAndSend("+5521987654321", new Message("Olá Brasil!")); // ByJG

Phone numbers are validated and formatted per country, and a new provider is one class.

Features​

  • Low code - Simple, intuitive API for sending SMS
  • Provider agnostic - Support for multiple SMS providers
  • Extensible - Easy to implement custom providers
  • Phone formatting - Built-in phone number validation and formatting
  • Multi-provider support - Route messages to different providers based on country codes

Installation​

composer require byjg/sms-client

Quick Start​

use ByJG\SmsClient\Message;
use ByJG\SmsClient\Provider\ProviderFactory;
use ByJG\SmsClient\Provider\TwilioMessagingProvider;
use ByJG\Util\Uri;

// Register and create provider
ProviderFactory::registerProvider(TwilioMessagingProvider::class);
$provider = ProviderFactory::create(new Uri("twilio://$accountSid:$authToken@default"));

// Send message
$response = $provider->send(
"+12221234567",
(new Message("Hello World!"))->withSender("+12223217654")
);

// Check result
if ($response->isSent()) {
echo "Message sent successfully!";
}

Documentation​

Available Providers​

ProviderURI SchemeDocumentationRegion
Twilio Messagingtwilio://accountId:authToken@defaultTwilio SMSGlobal
Twilio Verifytwilio_verify://accountId:authToken@serviceSidTwilio VerifyGlobal
ByJG SMSbyjg://username:password@defaultByJGBrazil
Fake Senderfakesender://Testing onlyTesting

Multi-Provider Setup​

Route messages to different providers based on country codes:

use ByJG\SmsClient\Provider\ProviderFactory;
use ByJG\SmsClient\Provider\TwilioMessagingProvider;
use ByJG\SmsClient\Provider\ByJGSmsProvider;
use ByJG\SmsClient\Message;

// Register providers
ProviderFactory::registerProvider(TwilioMessagingProvider::class);
ProviderFactory::registerProvider(ByJGSmsProvider::class);

// Associate with country codes
ProviderFactory::registerServices("twilio://accountId:authToken@default", "+1");
ProviderFactory::registerServices("byjg://username:password@default", "+55");

// Automatically routes to the right provider
ProviderFactory::createAndSend("+12221234567", new Message("Hello USA!"));
ProviderFactory::createAndSend("+5521987654321", new Message("Olá Brasil!"));

Phone Number Formatting​

Format and validate phone numbers with country-specific rules:

use ByJG\SmsClient\Phone;
use ByJG\SmsClient\PhoneFormat\USPhoneFormat;

$phone = Phone::phone("2345678900", new USPhoneFormat())
->withPlusPrefix()
->withCountryCode();

echo $phone->hydrate(); // Output: +12345678900
echo $phone->format(); // Output: +1(234)567-8900

// Validate phone numbers
$isValid = $phone->validate(throwException: false);

Running Tests​

vendor/bin/phpunit

Open source ByJG