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?
For iOS 8, try this:
Switching the ";" with a "&" worked for me.
To get sms: and mailto: links to work on both iPhone and Android, without any javascript, try this:
I tested it on Chrome for Android & iPhone, and Safari on iPhone.
They all worked as expected. They worked without the phone number or email address as well.
Bradorego's solution is what worked for me, but here is a more expanded answer.
A small consideration is that you need to encode the body using
%20
instead of+
. For PHP, this means usingrawurlencode($body)
instead ofurlencode($body)
. Otherwise you'll see plus signs in the message on old versions of iOS, instead of spaces.Here is a jQuery function which will refit your SMS links for iOS devices. Android/other devices should work normally and won't execute the code.
HTML:
jQuery:
Consider using a class like
a.sms-link
instead ofa[href^="sms:"]
if possible.Neither Android nor iPhones currently support the body copy element in a Tap to SMS hyperlink. It can be done programmatically though,
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)
(Just a little bit of topic), but maybe if you searched you could stumble here... In markdown (tested with parsedown and on iOS / android) you could do :