How to Pre-Populate the Sms Body Text Via an HTML Link

How to pre-populate the sms body text via an html link

It turns out this is 100% possible, though a little hacky.

If you want it to work on Android you need to use this format:

<a href="sms:/* phone number here */?body=/* body text here */">Link</a>

If you want it to work on iOS, you need this:

<a href="sms:/* phone number here */;body=/* body text here */">Link</a>

Live demo here: http://bradorego.com/test/sms.html (note the "Phone and ?body" and "Phone and ;body" should autofill both the to: field and the body text. View the source for more info)

UPDATE:

Apparently iOS8 had to go and change things on us, so thanks to some of the other commenters/responders, there's a new style for iOS:

<a href="sms:/* phone number here */&body=/* body text here */">Link</a>

(phone number is optional)

Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

I reported a bug to Apple a few years ago and I believe they fixed it since. To support older iOS versions, I just applied a simple script that will hit the link, wait half a second and then hit it again:

<html>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<a href="#" onclick="window.open('openSMS.php?number=5556667777&text=TEXT_TO_PRE_POPULATE');
wait(500);
window.open('openSMS.php?number=5556667777&text=TEXT_TO_PRE_POPULATE');">
Click here to open the SMS app
</a>

</body>
</html>

where openSMS.php is:

<?php

// Include and instantiate the class.
require_once 'Mobile-Detect-2.8.25/Mobile_Detect.php';
$detect = new Mobile_Detect;

// Get parameters
$number = $_GET['number'];
$text = $_GET['text'];
$url;

// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){
$url = "sms:" . $number . "&body=" . $text;
} else if( $detect->isAndroidOS() ){
$url = "sms:" . $number . "?body=" . $text;
} else if ($detect->isMobile()){
$url = "sms:" . $number . "?body=" . $text;
} else {

}

header('Location: '.$url);
?>

Pre-populate the sms body text with an https url

As you are already in a link, you need to encode the URL (the : and slashes / to be specific).

As example, https://example.com would become https%3A%2F%2Fexample.com

In your case, you'd need to change your URL to https%3A%2F%2Fnat5.com%2Fworkout.
Full link:

<a href="sms:&body=Look at this awesome link: https%3A%2F%2Fnat5.com%2Fworkout">Send SMS</a>

Reference to URL encoding: https://www.w3schools.com/tags/ref_urlencode.asp

And just as a side note: Both ways are looking to work on Android (At least on Android 7.0)



Related Topics



Leave a reply



Submit