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

210,704

Solution 1

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)

Solution 2

I know this is an old thread but stumbled upon it and found that some parts are no longer relevant.

I've found that if you want to just per-populate the text without adding a phone number, you can do the following:

sms:?&body=/* message body here */

Solution 3

For iOS 8, try this:

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

Switching the ";" with a "&" worked for me.

Solution 4

Just put all of the symbols in this order (I only tested it in this order since it makes the most sense code-wise to me).

Notice for the body link... I just put... ;?&body=. Also, notice that I have found I needed to use %20 for any spaces.

I have tested it on my iphone (v. 9.2) and another android and it works just fine.

This will solve the issue with having to hack it for different devices. I have no artifacts when I tested it in the SMS.

<a href="sms:19131234567;?&body=Question%20from%20mywebsite.com.%20%20MY%20MESSAGE%20-%20" title="Click here to TEXT US gallery token needs updating!">Send me SMS</a>

Solution 5

There is not need for two separate anchor tags for Android and iOS. This should help.

// Without Contact Number
<a href="sms:?&body=message">Text Message</a>

// With Contact Number
<a href="sms:1234567890;?&body=message">Text Message</a>

// Works on both Android and iOS

Share:
210,704
merlincam
Author by

merlincam

Updated on July 08, 2022

Comments

  • merlincam
    merlincam almost 2 years

    How to use an html link to open the sms app with a pre-filled body?

    Everything I have read seems to indicate that sms:18005555555?body=bodyTextHere

    Should work, but on the iPhone, this doesn't work. If I take out the ?body=bodyTextHere, and just use sms:phonenumber, it works.

    I have seen several instances where QR codes do this through a safari link. How are they able to pre-populate the body text?

  • sparkyspider
    sparkyspider about 12 years
    Is that Objective C? How do you call that from JavaScript or use it in HTML? Sounds impossible?
  • ouah
    ouah over 10 years
    On iOS it looks like the format is: <a href="sms:[phone number];body=body text">Link</a>. Notice no square brackets on the body.
  • Brad Orego
    Brad Orego over 10 years
    Ah, whoops. The square brackets aren't part of the format - that's where you should be putting in those values. I changed them to comments so hopefully it's a little clearer
  • Harikrishnan
    Harikrishnan over 10 years
    Worked like a charm.Thank you so much for the live demonstration.+100
  • Brad Orego
    Brad Orego over 10 years
    Can this please be un-marked as accepted? This doesn't at all answer the question. I actually posted something that does work and does answer the question.
  • MMachinegun
    MMachinegun over 10 years
    seems to be working now. Hm weird. But cool that it's also possible for iOS
  • Brad Orego
    Brad Orego over 10 years
    Note: I haven't tested these on the latest OS versions (iOS7/the iPhone 5S/5C and Android 4.4). There are some reports that they might not be working anymore, especially with Android's switch to using Hangouts instead of a native SMS app. I'll look into it when I have the chance.
  • MMachinegun
    MMachinegun over 10 years
    I took a look now because it sometimes was working and sometimes not, which seemed weird. My findings: If you dial a number you have at least sent one SMS before, the link works fine with "body". If it's a complete new number you're out of luck and you only get the number :(
  • David C
    David C about 10 years
    Works on most android, and IOS 5 and 6. Does not work on IOS 7.
  • Kirk Hammett
    Kirk Hammett about 10 years
    this page is not accessible anymore
  • Uirri
    Uirri almost 10 years
    I did not get the body text even though it was a number I have sent plenty of SMSs to and have stored in Contacts. (Tested on iPhone 5S)
  • jefeman
    jefeman about 9 years
    The android one does not appear to work if your device opens to hangouts. It will pass in the phone number, but not the body.
  • Greg Johnson
    Greg Johnson about 9 years
    For those still looking, it turns out with iOS 8 the syntax changed to <a href="sms:[phone number]&body=body text">Link</a>, where [phone number] is optional. TL;DR: iOS change ; to &.
  • Greg Johnson
    Greg Johnson about 9 years
    That's no longer true with iOS8. You can send <a href="sms:[phone number]&body=body text">Link</a> - the ampersand precedes the body=. Syntactically confusing, yes, but it's working for me and the body populates with the desired message.
  • fregante
    fregante almost 9 years
    Big note: the app that handles the sms URI can be changed on Android and many manufacturers do this; therefore while it may work on a specific device/app, it may not work on another (or simply after an update). My suggestion is to never use this on Android unless you're only setting the phone number; only use it on iOS versions and non-Android platforms that you've tested.
  • Strikers
    Strikers over 8 years
    can I add new line char in the body
  • paaacman
    paaacman over 8 years
    @OrangeDog see my answer for iOS 7 stackoverflow.com/questions/6480462/…
  • paaacman
    paaacman over 8 years
    For iPhone4, see my answer bellow stackoverflow.com/questions/6480462/…
  • Manish Ahuja
    Manish Ahuja about 8 years
    @strikers I'm also having issue adding a new line character. Were you able to resolve that?
  • SIVAKUMAR.J
    SIVAKUMAR.J about 8 years
    This information i am searching for my project.Its very usefull for deeplink concepts in android
  • Bidstrup
    Bidstrup almost 8 years
    Do you know about windows phone?
  • Rob Dawson
    Rob Dawson over 7 years
    fwiw I've just tested this on iOS 10 and it still works :).
  • Nicodemuz
    Nicodemuz over 7 years
    Confirmed that iOS 10.2 doesn't support a "body" part as described in the link above.
  • Bruno Lemos
    Bruno Lemos almost 7 years
    Is there any way to the cursor start in the position 0? I don't want it in the end of the body text
  • ibex
    ibex almost 7 years
    Just removing the number and leaving the semicolon (sms:;?&body=Questi..) worked on android 7 native messenger, when I didn't want the number to pre-populate
  • DMEM
    DMEM over 6 years
    @BradleyOrego On iPhones: This works perfectly fine if the SMS app was already opened in the background. If it wasn't, I'm just getting the app to be opened on my phone, without phone number or message body. Any ideas?
  • DMEM
    DMEM over 6 years
    @landen This works perfectly fine if the SMS app was already opened in the background. If it wasn't, I'm just getting the app to be opened on my phone, without phone number or message body. Any ideas?
  • Lucbug
    Lucbug over 6 years
    Following on what @bfred.it said, the syntax even changes for different platforms. You can check the challenge section of this article.
  • DMEM
    DMEM over 6 years
    @Lucbug Thank you for sharing the link but unfortunately this article is not up to date and contains incorrect data. For example: it is saying: "iOS, for instance, will take a phone number after the sms: protocol, but will ignore everything – including the number – if body text is included" which is incorrect. Either way, the issue I asked about (SMS app must be opened in the background) is not even mentioned there as a problem.
  • Urosh T.
    Urosh T. over 6 years
    2017-11-17: Tried on Aquaris BQ, Nexus, iPhone 7 and LPhone and this works like a charm. Tried with various browsers (Chrome, Firefox, Opera, native). Thanks a lot!
  • Toskan
    Toskan over 6 years
    none of the links seem to work with body in iOS 11.1
  • Konrad G
    Konrad G over 5 years
    That's the answer that I've been looking for. Thanks for the help!
  • kjr1995
    kjr1995 over 5 years
    You can get it to work by using <a href=sms:1234567890;?&body=https%3A%2F%2Fwww.google.com%2F>l‌​ink</a>. I've tested it on android 8.0 and iOS 11.4.1.
  • morels
    morels over 5 years
    It seems not true anymore according to stackoverflow.com/questions/39529099/…
  • Joshua Ohana
    Joshua Ohana over 5 years
    works great but when done from OSX it opens up a text message To: ?, any idea how to make it show up blank? I tried sms:?body and sms:;?body, the first tries to send to ? and the second tries to send to ;
  • Ali
    Ali over 5 years
    @JoshuaOhana What happens if you try "sms:?0123456789&body=My Message"? Sorry, don't have access to any Apple devices on hand.
  • Duke Dougal
    Duke Dougal about 5 years
    Apple has removed the ability to include body.
  • aslum
    aslum about 5 years
    Apparently Android has also removed the ability to include a body. I had this working and it stopped after the last android update. Sad.
  • Dan Parker
    Dan Parker about 5 years
    Yes Android had made a change, see my longer response below but href="sms://15551235555?body=Hello" now seems to work for google message app
  • JCisar
    JCisar about 5 years
    Do you know if this works for all android or just certain versions? This is the version that worked for my Pixel 2, android 9
  • Dan Parker
    Dan Parker about 5 years
    I've had no problems with this on older androids I've tested as well.
  • JCisar
    JCisar about 5 years
    Also, it does not seem to work when you do not provide a phone number (I know that's not part of the original question but it might be useful to know)
  • Silas S. Brown
    Silas S. Brown almost 5 years
    On Android it depends on which SMS app is being used. For example, on Android 9 with the QKSMS app, this tries to send a message to ;? and the body is not included. (QKSMS is not the default app, but it may be in use by people who don't want the SMS to become an MMS after it reaches a certain size.) Unfortunately there is no way for the HTML page to determine which SMS app will be in use. For this reason I'm going to omit SMS links if I detect Android.
  • Ollie Khakwani
    Ollie Khakwani over 4 years
    Thanks for this, works great on iOS and Android! Watch out for the Without Contact Number version on MacOS - it sets the SMS to to ?
  • Michal
    Michal about 4 years
    Can't create button without phone number.
  • Albert Renshaw
    Albert Renshaw about 4 years
    New iOS one does work for me IFF the iMessage app was already running, but if it was launching fresh it just opens the app and doesn't create a new message. Android didn't work at all. Abandoning this feature in my web app for now
  • Doodlemeat
    Doodlemeat almost 4 years
    Doesn't work for Android devices. Only a ?body= does the trick.
  • Dom Ramirez
    Dom Ramirez over 2 years
    You crushed it here. Thanks for the modern era update. My HTML validator is giving me trouble with the ";?&" combo, which makes me think it might be problematic, but I have a short-term use case on my hands for now.
  • Vincent
    Vincent over 2 years
    Is it possible to populate iOS's optional subject field via sms?
  • Peter Knut
    Peter Knut about 2 years
    I've just tested one universal URL: sms:18005555555?&body=Body. Works on Android 10+, iOS 15, macOS 11.6+