Search by

hryvinskyi / magento2-sms

hryvinskyi

Text messages for Magento 2: a phone number value in E.164, a sender that hands one message to the transport configured for the store, a transport pool other modules register into, and a log transport for development.

Package info

github.com/hryvinskyi/magento2-sms

Type:magento2-module

pkg:composer/hryvinskyi/magento2-sms

Statistics

Installs: 1

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

1.0.0 2026-12-03 08:32 UTC

This package is auto-updated.

Last update: 2026-12-03 14:18:17 UTC


README

Sends one text message to one phone number through the transport a store's configuration names. It gives other modules a phone number value in international form, a parser for what a person typed, a sender, and a pool that a module talking to an SMS provider adds its transport to. It ships one transport, a log file for development, and no transport that delivers a message to a phone.

What it does for a store

  • A phone number in international form. A number is a plus sign followed by 8 to 15 digits, the country code first (E.164), and cannot exist in any other form. Its masked form shows the plus sign, the first two digits and the last four: +4512345678 is shown as +45 •••• 5678.
  • What a person types. The parser drops spaces, hyphens, dots and brackets, and reads a leading 00 as +. A number without its country code is refused: there is no default country. The refusal's message is fit to show the person: "Enter the phone number with its country code, for example +45 12 34 56 78."
  • One transport per store view. The sender hands a message to the transport whose code the store view's configuration names. When no registered transport has that code, the message is refused with "Text messages cannot be sent right now." It is never sent through another transport.
  • The log transport. The transport log writes one line, SMS to <number>: <text>, to var/log/sms.log, and sends nothing. It works only in developer mode. In default and production mode it writes nothing and refuses the message, so a store that has not installed a real transport sends no text message and logs no number.

Requirements

  • Magento Open Source or Adobe Commerce 2.4.x. The module is built and tested on 2.4.9.
  • PHP 8.1 to 8.5.
  • psr/log, and the platform's Store, Config and Backend modules.

Installation

Run from the Magento root.

composer require hryvinskyi/magento2-sms
bin/magento module:enable Hryvinskyi_Sms
bin/magento setup:upgrade
bin/magento setup:di:compile  # production mode only
bin/magento cache:flush

Verify: bin/magento module:status Hryvinskyi_Sms prints Hryvinskyi_Sms : Module is enabled.

The module creates no table. To deliver messages, install a module that adds a transport (see Extension points) and choose it in the configuration.

Configuration

Stores > Settings > Configuration > General > Text Messages > General

Field Path Scope Default Effect
Transport sms/general/transport default, website, store view log (Log file (development only)) The transport that delivers the store view's messages. The options are the registered transports, by their labels.

The setting stores the transport's code. To set it from the command line for one store view:

bin/magento config:set --scope=stores --scope-code=<store_view_code> sms/general/transport <transport_code>
bin/magento cache:flush config

Replace <store_view_code> with the store view's code and <transport_code> with the code of a registered transport.

Admin permission

Resource id Title Where
Hryvinskyi_Sms::config Text Messages the Text Messages configuration section

Give a role the resource under System > Permissions > User Roles > Role Resources, where it sits under Stores > Settings > Configuration.

Sending a message

Inject the parser, the message factory and the sender:

use Hryvinskyi\Sms\Api\PhoneNumberParserInterface;
use Hryvinskyi\Sms\Api\SmsMessageFactoryInterface;
use Hryvinskyi\Sms\Api\SmsSenderInterface;
use Hryvinskyi\Sms\Exception\InvalidPhoneNumberException;
use Hryvinskyi\Sms\Exception\SmsUnavailableException;

public function __construct(
    private readonly PhoneNumberParserInterface $phoneNumbers,
    private readonly SmsMessageFactoryInterface $messages,
    private readonly SmsSenderInterface $sender
) {
}

try {
    $number = $this->phoneNumbers->parse($typedNumber);
    $this->sender->send($this->messages->create($number, (string) __('Your order has shipped.')), $storeId);
} catch (InvalidPhoneNumberException | SmsUnavailableException $refusal) {
    // Both messages may be shown to the person as they are.
}
  • PhoneNumberParserInterface::parse() throws InvalidPhoneNumberException for anything that is not a number of 8 to 15 digits with its country code.
  • SmsSenderInterface::send($message, $storeId) reads the transport of that store view. It throws SmsUnavailableException when no registered transport has the configured code, or when the transport cannot send now.
  • A phone number can also be made with the generated Hryvinskyi\Sms\Api\Data\PhoneNumberInterfaceFactory (argument e164); it throws InvalidPhoneNumberException for a value that is not E.164.

Extension points

Every interface under Api/ and both exceptions are @api.

Add a transport

Implement Hryvinskyi\Sms\Api\SmsTransportInterface:

  • getCode(): the code the configuration stores, in lower-case letters, digits and underscores.
  • getLabel(): the translated name the Transport field shows.
  • send(SmsMessageInterface $message, int $storeId): deliver the message. Throw SmsUnavailableException when it cannot be sent now; its message is shown to a person, so keep the default one or write one for them. The store id lets the transport read its own settings for that store view.

Register it in your module's global etc/di.xml:

<type name="Hryvinskyi\Sms\Model\TransportPool">
    <arguments>
        <argument name="transports" xsi:type="array">
            <item name="acme" xsi:type="object">Vendor\Module\Model\Transport\AcmeTransport</item>
        </argument>
    </arguments>
</type>

The pool finds a transport by its getCode(), not by the item's name; name the item after the code anyway, and give each transport its own code. The transport then appears under Transport.

Add to the pool only in a global etc/di.xml. An area di.xml (etc/frontend/di.xml, etc/adminhtml/di.xml, …) that names transports replaces the whole list in that area instead of adding to it, and the transports of other modules are gone there.

Stored data

The module creates no table and stores no number. The log transport writes each number and text to var/log/sms.log, in developer mode only.

Limits

  • No transport delivers to a phone. Until a module adds one and a store view chooses it, every message outside developer mode is refused.
  • The log file holds personal data. In developer mode, var/log/sms.log keeps each number and the full text, a one-time code included. Keep it off shared and production machines, and clear it.
  • One attempt, in the request. The sender calls the transport once, in the caller's request. It does not queue, retry or limit how many messages are sent; a caller that lets visitors trigger messages limits them itself.
  • The text is passed as it is. The module does not check its length or split it into parts; the transport and the provider decide what a long text costs or becomes.
  • The number is checked for its form only. A number of the right form that no phone answers is accepted.

Registrations

Where What
Preferences PhoneNumberInterface, SmsMessageInterface, PhoneNumberParserInterface, SmsMessageFactoryInterface, SmsSenderInterface to their implementations under Model/.
Pool Hryvinskyi\Sms\Model\TransportPool Argument transports: log → Model\Transport\LogTransport.
Log Virtual types Hryvinskyi\Sms\Model\Log\Handler and …\Logger, file var/log/sms.log.
Configuration system.xml section sms; default in config.xml.
ACL Hryvinskyi_Sms::config.

The module registers no route, table, cron job, command, event or plugin.

Versioning

The package follows Semantic Versioning. What changed in each version is in CHANGELOG.md.

Tests

Run from the Magento root.

  • Unit: vendor/bin/phpunit -c app/code/Hryvinskyi/Sms/phpunit.xml.dist.
  • Integration: the platform's harness in dev/tests/integration, one run at a time against its own database. cd dev/tests/integration && php -d memory_limit=4G ../../../vendor/bin/phpunit -c phpunit.xml ../../../app/code/Hryvinskyi/Sms/Test/Integration.
  • Static analysis: PHPStan 2.x at level 10 with the package's phpstan.neon.dist (phpstan analyse -c app/code/Hryvinskyi/Sms/phpstan.neon.dist --level=10, after bin/magento setup:di:compile, because the configuration reads the generated code), and vendor/bin/phpcs --standard=app/code/Hryvinskyi/Sms/phpcs.xml.dist app/code/Hryvinskyi/Sms.

Translations

The wording ships in en_US and 25 more locales: the official languages of the European Union, Norwegian Bokmål and Ukrainian. The translations are machine-made; have them read by a native speaker before a store relies on them, and override a phrase in a theme or a language pack.