In the past couple of years, internet users have grown massively, and today we cannot live without the internet. As of 2021, over 1.8 billion websites are registered on the internet, and it's pretty hard to maintain customer retention. So developers use techniques to keep the users updated; integrating text messages is one of them.
PHP is one of the go-to options for server-side developments because of the steep learning curve, massive community support, open-source nature, and many more. This article explains three different options for sending SMS from your PHP web app. I'll mention the pros and cons of each method, and it's up to you to decide what suits you most.
This article will provide an overview of using SMTP for SMS, SMS API, and multi-channel notification services. Then we will dive into a technical part where I explain the existing options and how to implement them into your application and, finally, discuss the advantages and disadvantages of each method.
1. Using SMTP
Simple Mail Transfer Protocol(SMTP) is the most common way to send system-generated emails. Depending on the backend programming language, different libraries provide SMTP services. eg: Nodemailer for Node.js applications and PHPMailer and SwiftMailer for PHP applications.
But this tutorial is for discussing the ways to send SMS messages. I will be using 3rd party application to act as the middleman between the Mail server and the mobile phone.
The above illustration shows a 2-way communication line in blue and green arrows; The blue arrow shows you how the SMS comes in from the mobile network and how it is sent email client (such as Outlook) through an Email server using the SMTP protocol. Of course, if you are interested in only email forwarding, you can ignore the blue arrows.
Tutorial: How to send SMS using Email
It requires two things:
- The phone number that you want to reach.
- Carrier's name (Many can be found through this list).
The following standard way can be followed for most carriers: (Might not work for some countries).
If your requirement is sending an SMS, you do not need PHP. For example, let's assume your number is 5555551234, and your network provider is Verizon Wireless Network(A large network provider in US and Canada). Then you can type firstname.lastname@example.org in your email client and send the message. The text message will receive to the phone number +1 (555) 555-1234.
To implement the same using PHP you can use PHPMailer or PHP's mail function. For this example, I'll take PHP's mail function (You can check the documentation for more details). The function has the following signature:
Now run the edit function signature and add it to our PHP code:
This is the simplest way, but when you receive the text message in the message header, it will display the message is from Gmail. You can go with an alternate SMS Gateway like Ozeki SMS Gateway to avoid that.
Advantages of using SMTP to SMS
The main advantage of using SMTP to SMS service is sending text messages using your email client. This can be implemented with minimal coding, and this won't take more than 10 minutes to implement.
Disadvantages of Using SMTP to SMS
You need to know the carrier (the wireless network's domain name) associated with the mobile number (Many can be found through this list). Secondly, you'll want to be careful with how long your messages get when sending the message. If you exceed the character limit(around 55), your message can be split into multiple texts or lost.
Additionally, this is a bit slow process because SMPT requires sending messages back and forth, and in this case, messages need to go through the SMS gateway. So there is a chance for failed deliveries.
2. Using SMS API
SMS API allows you to integrate text messages into your software application. It can be implemented for 2-way communications as well. You can use this to keep your business live 24/7 and quickly provide notifications and information to the customer.
Tutorial: How to send SMS using MessageBird API
There are many SMS API providers out there. Twilio, Nexmo, and MessageBird are some of the examples. I will consider MessageBird API for the PHP web application for the tutorial. To get started, you need to:
- Create a free account at MessageBird. When you create an account, please provide your phone number (It offers you ten free messages for a free trial).
- Go to the developer section, and you can find two API keys. One for testing and one for actual use.
3. To interact with the MessageBird API provider, we need to install a package.
composer require messagebird/php-rest-api
4. To get the recipient number and email, you can include an HTML form, but since we are focusing on the backend, I will provide it manually.
After testing the code with the test API key, you can use the Live API key to receive the actual message. To check the delivery status of the message, you can print the $responce object, which contains the API response.
Advantages of using SMS API
Whatever SMS API you use, it's easy to integrate with your application. Minimal coding is needed for implementation, and the services provide detailed documentation about the API and usage. Additionally, some provide extra features such as analytics and click-through rate. Additionally, it's fast compared to the SMTP to SMS method.
Disadvantages of using SMS API
All the API providers are paid, and the cost depends on what you choose and the number of text messages you want to send.
Another disadvantage of using an SMS API rather than a multi-channel notifications provider (which I discuss below) is that if your app must notify users via other channels, you'd have to connect each new channel separately. For example, you'd have to integrate mobile and web push, SMS, and chat apps like Slack and WhatsApp separately. It's up to you to decide the worth of all the extra time and effort.
3. Using Multi-Channel notification API
As the name implies, multi-channel notification services (MNS) are for sending notifications across many channels(emails, text messages, push notifications, WhatsApp, Slack, etc.) using a uniform API. Courier is designed to do that. It has a uniform API to notify users of all the channels, and it has real-time logs and analytics where you can monitor the performance of all the channels.
Even though this tutorial is about sending text messages, there are some factors we need to take into consideration when we develop scalable applications. However, as shown in the above illustration, this can scale into other notification channels whenever new requirements arise.
Tutorial: How to send SMS using multi-channel notification services
In this tutorial, I will implement SMS messaging using Courier. They have an excellent free service with up to 10000 notifications per month.
At first, you need to go to the website, create an account, and do the initial setup.
- Sign in using your Gmail account
- Give workspace name
- Select SMS
- Select the backend programming language you use
- Select SMS Provider(I select MessageBird)
- Give the associated number with MessageBird, and the Access Key, and then Install the provider(If you use windows, run the given snippet in your PowerShell).
If you do the initial setup correctly, you will get redirected to the following window.
2. Create a new notification, and then you can preview how the notification will appear on the recipient's mobile.
3. Install Courier SDK.
composer require trycourier/courier
- Courier will automatically generate the PHP snippet for your notification. You can copy-paste it from the Send tab. It will look like this,
To check the status of the SMS message, you can head into the Logs in Courier dashboard, and there, you can see the queued, sent, delivered, opened, clicked, and undeliverable statuses. In addition, the Courier will also tell you if there are any errors and when they occurred in the delivery pipeline.
Advantages of using multi-channel notification services
Nowadays, it's critical to make your application scalable. Multi-channel notification services give that scalability while keeping your codebase clean. This is a massive plus as technologies and business requirements change over time.
Secondly, services like Courier allow you to access the drag and drop template builder, which can save loads of time. Finally, real-time logs and analytics also help businesses keep track and analyze performance.
Disadvantages of using multi-channel notification services
The disadvantages of the multi-channel notification services are similar to the disadvantages discussed in the SMS APIs. All the benefits come with an associated cost (But in Courier, they have free plans up to 10000 notifications per month). Additionally, again it's third-party involvement. You rely on a third-party service to send notifications all across the channels.
This article discussed three methods for sending text messages in a PHP web application. We have discussed the pros and cons of SMTP to SMS, SMS API, and multi-channel notification services, and you can decide what method to use depending on your time and budget.