How to send SMS Messages

5.4 version
Maintained Unmaintained
Symfony 5.4 is backed by Private Packagist.

How to send SMS MessagesΒΆ

New in version 5.0: The Notifier component was introduced in Symfony 5.0.

The Symfony\Component\Notifier\TexterInterface class allows you to send SMS messages:

// src/Controller/SecurityController.php
namespace App\Controller;

use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\TexterInterface;
use Symfony\Component\Routing\Annotation\Route;

class SecurityController
{
    /**
     * @Route("/login/success")
     */
    public function loginSuccess(TexterInterface $texter)
    {
        $sms = new SmsMessage(
            // the phone number to send the SMS message to
            '+1411111111',
            // the message
            'A new login was detected!'
        );

        $sentMessage = $texter->send($sms);

        // ...
    }
}

The send() method returns a variable of type Symfony\Component\Notifier\Message\SentMessage which provides information such as the message ID and the original message contents.

New in version 5.2: The SentMessage class was introduced in Symfony 5.2.

See also

Read the main Notifier guide to see how to configure the different transports.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.