How to Send SMS Messages Directly from Your Website

Do you want to send SMS messages from your web app, but feel daunted by the complexities of traditional messaging APIs? If so, you're not alone. Adding SMS integration to a web app can be a challenge, especially if you're not familiar with the technical details of SMS messaging. But fear not! We have a solution that's simple, effective, and doesn't require any cloud services or APIs.

Sending SMS and MMS Messages from a Web Page

In this article, we'll show you how to send SMS messages directly from a web page using the "sms:" prefix. That's right, no need for third-party services or APIs - just simple HTML. And the best part? This method works without needing an API on the phone, making it a hassle-free solution for developers looking to add SMS integration to their web apps.

By the end of this article, you'll have a simple and effective way to add SMS integration to your web app, without the need for third-party services or APIs. So, let's dive in and see how easy it is to send SMS messages from a web page!
Explanation of the Problem and the Need for SMS Integration in Web Apps

SMS messaging has been around for decades and is still one of the most popular methods of communication worldwide. It's a simple, reliable, and fast way to send short messages to individuals or groups, and it doesn't require an internet connection. However, integrating SMS messaging into a web app can be a challenge.

Traditional messaging APIs require developers to use a third-party service to send SMS messages, which can be costly and require complex integrations. Additionally, these APIs often require an internet connection, which may not be available or reliable in certain regions or situations.

But why would a web app need SMS integration in the first place? There are several reasons why web apps might want to integrate SMS messaging, such as:

User Verification: Many web apps require users to verify their identity using SMS authentication codes, which are sent via SMS.

Notifications: Web apps can use SMS messaging to send notifications to users, such as password reset alerts, appointment reminders, or package delivery updates.

Marketing: SMS messaging is a popular method for businesses to reach out to customers with promotional offers, product updates, or event invitations.

Fortunately, there's a simple solution that doesn't require any cloud services or APIs, and best of all it is FREE (at least for your application). In the next section, we'll explore this solution in more detail and show you how easy it is to add SMS integration to your web app.

Overview of the Solution: Using SMS Links With the "sms:" Prefix

The solution to integrating SMS messaging into your web app is to use SMS links with the "sms:" prefix. This approach allows you to create a hyperlink that, when clicked, opens the default SMS app on the user's device and pre-populates a message with the recipient's phone number.

To use an SMS link, simply create an "a" tag with the "href" attribute set to "sms:" followed by the phone number and message. For example, the following code snippet creates a hyperlink that opens the default SMS app with the recipient's phone number and message pre-populated:

<a href="sms:+1234567890?body=Hello%20world!">Send SMS</a>

In this example, the phone number is "+1234567890" and the message is "Hello world!". The "%20" characters represent spaces in the message since spaces are not allowed in URLs.

It's important to note that this approach works on both iOS and Android devices and doesn't require any special apps or APIs. It's a simple and effective way to integrate SMS messaging into your web app without relying on third-party services.

However, there are some limitations to using SMS links. For example, the user must have a default SMS app installed on their device, and some carriers may not support SMS links or may have character limits for messages.

Most major cellular carriers around the globe support 160 characters per message and up to 1600 in a concatenated message, your user will need to be aware of the potential costs. It's also worth noting that the maximum length of an SMS message is 160 characters. If you need to send a longer message, it will be split into multiple messages, and the recipient may be charged for each message received.

It's important to note that the "sms:" prefix used with SMS links is limited to sending text messages only. Therefore, you cannot use SMS links to send multimedia messages, such as images, videos, or audio files. However, if you need to send multimedia messages, you can use MMS (Multimedia Messaging Service) links instead.

Later, we'll cover how to send messages with media from your web app using MMS links with the "mms:" prefix. So, while SMS links are great for sending text messages, MMS links provide a solution for sending multimedia messages.

Despite these limitations, using SMS links with the "sms:" prefix is a great solution for integrating SMS messaging into your web app. It's easy to implement, reliable and doesn't require any external APIs or services. In the next section, we'll show you some examples of how to use SMS links in your web app for various use cases.

Basic SMS Link Syntax

The "sms:" prefix is a special kind of URL that is used to send SMS messages directly from a web page. When a user clicks on an "sms:" link, their phone's default messaging app will open with a new message pre-populated with the recipient's phone number and the message content specified in the link.

It's important to note that the "sms:" prefix does have some limitations. As mentioned earlier, it doesn't support sending images or other multimedia content. Additionally, it's not possible to send messages to multiple recipients using a single "sms:" link. However, it can still be a useful tool for integrating SMS functionality into your web app.

To send an SMS message using the "sms:" prefix, you need to specify two parameters in the URL: the recipient's phone number and the message content you want to send.

The phone number should be formatted in the international format, with the country code followed by the phone number, without any spaces or special characters. For example, if you want to send a message to a phone number in the United States with the area code 123, you would format the number like this: "+1-123-456-7890".

The message content can be added to the URL using the "?body=" parameter, followed by the message you want to send. It's important to note that any spaces or special characters in the message should be URL-encoded to ensure that they are properly transmitted in the URL. For example, a space should be encoded as "%20", and a comma should be encoded as "%2C".

For example, if you want to send the message "Hello, world!" your "sms:" link would look like this: "sms:+1-123-456-7890?body=Hello,%20world!".

By specifying the phone number and message content in the "sms:" link, you can seamlessly integrate SMS functionality into your web app, without the need for any third-party services or APIs.

Examples of Basic SMS Links with Different Parameters

There are several parameters you can include in an SMS link to customize the message. Here are some examples of basic SMS links with different parameters:

Simple SMS link:

<a href="sms:1234567890">Send SMS</a>

This will open the user's SMS app and pre-fill the phone number "1234567890".

SMS link with a custom message:

<a href="sms:1234567890?body=Hello%20World!">Send SMS</a>

This will open the user's SMS app and pre-fill the phone number "1234567890" and the message "Hello World!".

SMS link with multiple recipients:

<a href="sms:1234567890,0987654321">Send SMS</a>

This will open the user's SMS app and pre-fill the phone numbers "1234567890" and "0987654321". Be aware this is not a unique message, everyone will see the other phone numbers because it is a group message.

SMS link with a custom message and multiple recipients:

<a href="sms:1234567890,0987654321?body=Hello%20World!">Send SMS</a>

This will open the user's SMS app and pre-fill the phone numbers "1234567890" and "0987654321" as well as the message "Hello World!".

SMS link with a custom message and subject:

<a href="sms:1234567890?body=Hello%20World!&subject=Greetings">Send SMS</a>

This will open the user's SMS app and pre-fill the phone number "1234567890", the message "Hello World!", and the subject "Greetings".

These are just a few examples of the many parameters you can include in an SMS link to customize the message. By using these parameters, you can create SMS links that are tailored to your specific needs.

Using HTML and JavaScript to Generate SMS Links Dynamically

While manually creating SMS links may be useful in some cases, it is not a scalable solution for web applications that require the dynamic generation of SMS messages. In such cases, developers can use HTML and JavaScript to generate SMS links dynamically.

To generate an SMS link dynamically, the href attribute of the <a/> tag can be set using JavaScript. This can be done by concatenating the sms: prefix with the phone number and any additional parameters, and then assigning the resulting string to the href attribute.

Here is an example of how to generate an SMS link dynamically using JavaScript:

<!DOCTYPE html>
<html>
  <head>
    <title>SMS Link Example</title>
  </head>
  <body>
    <button id="send-sms">Send SMS</button>

    <script>
      const button = document.getElementById("send-sms");
      const phoneNumber = "1234567890";
      const message = "Hello, World!";

      button.addEventListener("click", () => {
        const link = document.createElement("a");
        link.href = `sms:${phoneNumber}?body=${encodeURIComponent(message)}`;
        link.click();
      });
    </script>
  </body>
</html>

In this example, a button with the id "send-sms" is created in the HTML document. When the button is clicked, a new <a/> tag is created and its href attribute is set to the SMS link generated using the phone number and message variables. The encodeURIComponent function is used to properly encode the message parameter.

Finally, the click method is called on the new <a/> tag, which opens the SMS application with the generated SMS message ready to be sent.

Using this approach, developers can dynamically generate SMS links based on user input or other data, providing a more flexible and scalable solution for SMS integration in web applications.

How to Send Media Using the mms: Prefix

While the sms: prefix is suitable for sending text-only messages, it doesn't support sending multimedia content like images, videos, or audio files. However, you can still use SMS links to send multimedia messages by using the mms: prefix instead.

Similar to the sms: prefix, the mms: prefix is used to create a link that launches the user's messaging app with pre-populated content. The difference is that the mms: prefix allows you to include multimedia attachments in your message.

Here's an example of how to send an MMS message using an SMS link:

<a href="mms:+1234567890?attachment=file:///path/to/image.jpg">Send MMS</a>

In this example, the recipient's phone number is +1234567890, and the multimedia attachment is a JPEG image located at /path/to/image.jpg on the device's file system.

It's worth noting that the syntax for attaching media files using the mms: prefix can vary depending on the device and operating system. Some devices may require you to use a different URL scheme, while others may require the media file to be hosted on a server.

Regardless of the syntax, the mms: prefix is a reliable way to send multimedia content through SMS links without relying on cloud services or APIs.

With the knowledge of the mms: prefix, you can now confidently send multimedia messages from your web app using SMS links.

Sending from a web app sort of implies you won't be working with the user's local file system. While possible, it is not common. No worries, you can send media hosted on a URL as well.

To send an image as a URL, you can simply include the URL as the value of the "attachment" parameter in your mms: link. For example:

<a href="mms:+1234567890?attachment=https://example.com/image.jpg">Send MMS</a>

In this example, the image.jpg file is hosted on a remote server at https://example.com, and is included as an attachment to the message.

Unfortunately, there is no way to attach an image directly from a webpage using HTML or JavaScript. However, you can provide a file input field and allow the user to select the file they wish to attach. Then, you can use JavaScript to generate the mms: link with the attachment parameter set to the selected file's URL or file path.

Detecting SMS Support on the Device Using JavaScript

Normally I always feature detect when utilizing an API that may not be supported across all platforms. SMS is not commonly supported on the desktop, so desktop users may not have a way to send text messages. I use the Microsoft Phone Link App on my desktop, but it sends messages through my phone. It is also not registered as an application that can send text messages, therefore does not respond to the hyperlink being pressed.

There is no foolproof way to detect SMS support on a device using JavaScript. Some techniques involve attempting to launch an SMS application and seeing if it succeeds, but this is not reliable and may not work on all devices.

One common approach is to use a combination of user agent detection and feature detection to make an educated guess about SMS support. For example, if the user agent is a mobile device and the canShare API is available and supports text sharing, it may be reasonable to assume that SMS is supported. However, this approach is not guaranteed to be accurate.

My advice is to include this in your web applications, but you may need to add some sort of disclaimer so users know this may not be available on their desktops.

Conclusion

Sending SMS messages from a web page is a powerful and efficient way to communicate with users or give your users an easy way to share messages from your application. With the use of the "sms:" prefix and HTML/JavaScript, developers can easily integrate SMS into their web apps without relying on third-party services or APIs.

I hope this article has been helpful in explaining the basics of sending SMS messages from a web page. If you found this article useful, please like and share it with your colleagues. And don't forget to follow me on C# Corner for more informative and practical articles on web development.


Love2Dev
We specialize in Progressive Web Apps and Web Performance Optimization