Sending an SMS from the CLI via adb

Someone asked me if I had a good way to do this the other day. So I spent a few minutes and knocked this up.

send_sms.sh:

#!/bin/bash

to="$1"

text=""

usage() {
	echo -e "Usage:\n $0 "
	echo -e "\nSends an SMS message via adb."
	echo -e "\nNumber must be specified on the command-line, and you may need to include the country code,\n  e.g '+61414123456', not '0414123456'"
	echo -e "\nContent for sms message comes from stdin.\n"
}

if [ -z "$to" ]; then
	usage
	exit 0
fi

if [ -t 0 ]; then
	echo "Enter SMS content, ctrl-d to end."
fi

while read ln; do
	if [ -n "$text" ]; then text="$text\n"; fi
	text="$text$ln"
done

len=$(echo "$text" | wc -c)

echo -e "Sending sms ($len bytes) to '$to'..."

adb shell service call isms 7 i32 0 s16 "com.android.mms.service" s16 "$to" s16 "null" s16 "'$text'" s16 "null" s16 "null"

Usage:

$ echo "This is a message" | ./send_sms.sh +61405678901
Sending sms (18 bytes) to '+61405678901'...
Result: Parcel(00000000 '....')

$ ./send_sms.sh +61405678901
Enter SMS content, ctrl-d to end.
this is a message
Sending sms (18 bytes) to '+61405678901'...
Result: Parcel(00000000 '....')

You may find that you’ll need adb installed and your phone connected, but I didn’t test that, I suppose you might get lucky.

Leave a Reply