With over 2 billion websites registered across the internet and over 9 million mobile apps, it’s difficult to keep people engaged with any one product or service. Developers are turning notifications as a way to keep users engaged. Text messaging, typically via SMS, is one of the last “clean” channels to reach users quickly and is often used in combination with push notifications and in-app messaging or can even be used with channels that are internal to an organization like Slack and Pagerduty.
PHP is one of the go-to options for server-side developmentThis article explains three different options for sending SMS from your PHP web app.
- SMTP (build everything from scratch): Build the SMS infrastructure by connecting an SMTP service to a carrier’s SMS gateway and then build all the notification infrastructure and logic yourself.
- SMS API (build notifications from scratch): use an off-the-shelf SMS API service and build all the notification infrastructure and logic yourself.
- Notifications API (fully composable): use a notification development platform like Courier that has robust multi-channel notification infrastructure and all the notification logic available as API primitives.
1. Using SMTP
Simple Mail Transfer Protocol(SMTP) is typically associated with sending 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.
Most importantly, this method involves building the entire SMS infrastructure from scratch, requiring a deep understanding of SMTP and of carrier SMS infrastructure. It then requires building all the infrastructure and logic to use SMS for notifications - gathering and respecting user preferences, sequencing across other channels (eg. push, in-app messaging, and email), regional regulations and compliance, template management, dynamic/personalized content and data, and so on
2. Using SMS API
SMS API provides the basic no-frills functionality for integrating text messages into your software application.
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 notifications API (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.
Most importantly, this method involves building all the infrastructure and logic to use SMS for notifications - gathering and respecting user preferences, sequencing across other channels, regional regulations and compliance, template management, advanced send logic (eg. build sending, batching, digesting, and throttling), dynamic/personalized content and data, and so on
3. Using a Notifications API
While sending an SMS via an SMS API is quite simple, an automated notification requires robust notification infrastructure and logic to provide a good experience for users. A Notification API like Courier solves this by working with an SMS API and providing all the added notification functionality through well defined API primitives - so you don't have to build it yourself. It also has real-time logs and analytics where you can monitor the performance across all the channels in a notification sequence. And because it operates at one level above the SMS API provider, you can even use it to failover between providers in the event that one goes down.
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 a notification API service
In this tutorial, I will implement SMS messaging using Courier. It’s free to use for up to 10,000 notifications per month.
At first, you need to go to the website, create an account, and do the initial setup.
- Sign up
- Give a name to your workspace
- Select SMS as the channel
- 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.
For more information on using Courier with PHP, see the following PHP Quickstart.
Advantages of using a notifications API services
If you're using SMS for notifications, a notification API service is really a must. It provides all the features required to build good text (or multi-channel) notifications, while keeping your codebase clean. And the infrastructure requirements around security, user preferences, failover, logging, you name it - are all handled and maintained for you.
Secondly, services like Courier allow you to access a drag and drop template builder, which can save loads of time.
Disadvantages of using a notifications API services
If you're not using SMS for a notification use case, then it's possible that a Notification API won't be appropriate. You can try Courier for your use case with the free plan to verify if it meets your needs.
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 a notification API, and you can decide what method to use depending on your time and budget.