Sending SMS with API's
The documentation herein is for the legacy/deprecated v1, v2, and v3 versions of SMS Framework.
Versions 4 and later have been re-architected. New documentation can be found in Communications Framework
Equivalent documentation for sending messages in Notifier & SMS Framework v4 can be found at Sending to users, entities, or objects.
When you are sending SMS, you will either be sending an SMS to an entity or directly to a number. Usually you intend to send an SMS to a user entity.
Sending SMS to an Entity
You will need to load in the entity you wish to send to.
// Along the lines of
$entity = MyEntity::load(123);
// Or usually, with a user entity:
$entity = \Drupal\user\Entity\User::load(123);
Create a reference to the entity phone number service.
/** @var \Drupal\sms\Provider\PhoneNumberProviderInterface $phone_number_service */
$phone_number_service = \Drupal::service('sms.phone_number');
Typically this is done using dependency injection.
Construct your SMS message. There are a variety of properties you can set on the the SMS message. When sending through the phone number service you are only required to set the message contents.
$sms = (new \Drupal\sms\Message\SmsMessage())
->setMessage('Foobar'); // Set the message.
Then send the message. Be sure to catch exceptions, as the method will throw them as needed.
try {
$phone_number_service->sendMessage($entity, $sms);
}
catch (\Drupal\sms\Exception\NoPhoneNumberException $e) {
// Thrown if entity does not have a phone number.
}
catch (\Exception $e) {
// Other exceptions can be thrown.
}
The message will be queued for transmission.
Sending directly to a phone number
It is highly preferred to use the entity phone number service above, but in cases where you only have raw phone numbers then this is the method for you.
Note: It is advised that you do not use the send() method of the SMS provider. Always use the queue() method since the site has better control over timing and resources. Using send() will cause pages to load slower, and in some cases will cause SMS transmission to halt unexpectedly.
Create a reference to the phone number service. Typically this is done using dependency injection.
/** @var \Drupal\sms\Provider\SmsProviderInterface $sms_service */
$sms_service = \Drupal::service('sms.provider');
Construct your SMS message. There are a variety of properties you can set on the the SMS message. When sending through the SMS service you are required to set the message contents, direction (outgoing), and the recipient phone number.
$sms = (new \Drupal\sms\Message\SmsMessage())
->setMessage('Foobar') // Set the message.
->addRecipient('123456789') // Set recipient phone number
->setDirection(\Drupal\sms\Direction::OUTGOING);
Then send the message. Be sure to catch exceptions, as the method will throw them as needed.
try {
$sms_service->queue($sms);
}
catch (\Drupal\sms\Exception\RecipientRouteException $e) {
// Thrown if no gateway could be determined for the message.
}
catch (\Exception $e) {
// Other exceptions can be thrown.
}
The message will be queued for transmission.
If, however, you need to send an SMS directly, you can do it with the following:
$sms_service->send($sms);Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion