Sending Laravel SMS notifications with Telnyx

Email icon on orange digital background
Development Laravel

Telnyx is a next-gen communications platform that offers APIs for provisioning phone numbers, sending and receiving SMS and MMS, and making and receiving phone calls.

The Laravel-Telnyx package provides an alternative to the default Laravel SMS notification provider Nexmo to send SMS from your Laravel application.

Why use Telnyx? 
Because it offers very low fees, probably the cheapest on the market.

MMS support
At the moment Telnyx only supports sending MMS in the United States and Canada, even if it’s expanding its global messaging footprint. MMS is likely to come into play at the end of Q2 next year.

Below follow a couple of examples that show how to send SMS and MMS notifications with the Laravel-Telnyx package.

 

Prerequisites

You first need to register a Telnyx account, generate a phone number*, a messaging profile, and an API key

*notice that at the moment just American phone numbers are allowed to send SMS, so you will need to generate an American number.

 

Installation

You can install the package via composer:

composer require agiledrop/laravel-telnyx

 

Publish the config file with:

php artisan vendor:publish 
--provider="AGILEDROP\LaravelTelnyx\LaravelTelnyxServiceProvider" 
--tag="config"

 

Publish and run the migrations with:

php artisan vendor:publish 
--provider="AGILEDROP\LaravelTelnyx\LaravelTelnyxServiceProvider" 
--tag="migrations"
php artisan migrate

 

Add the parameters to the configuration 

You should then add the values of the generated data to your .env file with the following parameters.

TELNYX_API_KEY=
TELNYX_FROM=
TELNYX_MESSAGING_PROFILE_ID=

 

Sending an SMS notification

This is an example of SMS notification.

Generate a notification with an artisan command:

php artisan make:notification SmsNotification

This will create for you the file at app/Notifications/SmsNotification.php.

Then paste in that the following code:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use AGILEDROP\LaravelTelnyx\Messages\TelnyxSmsMessage;
use Illuminate\Notifications\Notification;

class SmsNotification extends Notification
{
    use Queueable;

    private string $from;
    private string $content;

    /**
     * Create a new notification instance.
     *
     * @param string $from
     * @param string $content
     */
    public function __construct(string $from, string $content)
    {
        $this->from = $from;
        $this->content = $content;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable): array
    {
        return ['telnyx-sms'];
    }


    /**
     * Get the Telnyx / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return TelnyxSmsMessage
     */
    public function toTelnyx($notifiable): TelnyxSmsMessage
    {
        return (new TelnyxSmsMessage())
           ->from($this->from)
           ->content($this->content);
    }
}

Then to use this SMS notification, just import it to where you need it and run it like below:

use App\Notifications\Alerts\SmsNotification;
…
$from = env('TELNYX_FROM');
$content = 'The text of your sms…';
$admin->notify(new SmsNotification($from, $content));

 

Sending an MMS notification (available just for the U.S.)

This is an example of MMS notification.

Create a notification with an artisan command:

php artisan make:notification MmsNotification

This will create: /App/Notification/MmsNotification.php.

Then paste in that the following code:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use AGILEDROP\LaravelTelnyx\Messages\TelnyxMmsMessage;
use Illuminate\Notifications\Notification;

class MmsNotification extends Notification
{
    use Queueable;

    private string $content;
    private string $subject;
    private array $images;
    private string $from;

    /**
     * Create a new notification instance.
     *
     * @param string $content
     * @param string $subject
     * @param array $images
     * @param string $from
     */
    public function __construct(string $from, string $content, string $subject, array $images)
    {
        $this->from = $from;
        $this->content = $content;
        $this->subject = $subject;
        $this->images = $images;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable): array
    {
        return ['telnyx-mms'];
    }


    /**
     * Get the Telnyx / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return TelnyxMmsMessage
     */
    public function toTelnyx($notifiable): TelnyxMmsMessage
    {
        return (new TelnyxMmsMessage())
            ->from($this->from)
            ->content($this->content)
            ->subject($this->subject)
            ->images($this->images);
    }
}

Then to use this MMS notification, just import it to where you need it and run it like below: 

use App\Notifications\Alerts\MmsNotification;
…

$from = env('TELNYX_FROM');
$content = 'The text of your mms…';
$subject = 'The mms subject';
$photos = []; //Array with images urls
$member->notify(new MmsNotification($from, $content, $subject, $photos));

 

Conclusion

What are the PROs and CONs of using Telnyx instead of Nexmo?

The fees that Telnyx offers are probably the cheapest on the market.

At the moment it’s possible to send this SMS worldwide just from American phone numbers, so if you need to show your customers a phone number of a specific country, this is not possible yet.

You can find the package on GitHub.

If you have any questions, feel free to email me at davide.casiraghi@agiledrop.com and I’ll do my best to respond.