Send an SMS Text Message from the Command Line

Send a text message from the command line

When you think of sending out text messages you probably think of the iPhone or an Android, and the command line doesn’t cross your mind, but thanks to the ever-useful curl command, you can send out a SMS text message to any phone number right from the Terminal.

Yes, curl, the same command line tool for transferring data to and from URL’s, downloading files, getting HTTP header details, and so much more, can send text messages. This is done through a POST request sent to the TextBelt service, a free outgoing SMS API. Sure there are limits, but they’re fairly generous at 75 texts per day (per IP), and you can’t send a number more than 3 texts in three minutes to prevent abuse. Aside from that, keep in mind that you’ll be charged for incoming texts at the regular SMS / texting rate from your cell provider – this does not use the iMessage service – so don’t overuse this if you don’t have an unlimited traditional texting plan.

Sending a Text Message from the Command Line with curl

The basic syntax to use is as follows, be sure to replace the ########## with your own 10 digit phone number (10 digits = area code + phone number), and then replace the message= text with your own message to send:

curl -d number=########## -d "message=text goes here"

For example, to send a text saying “hello from” to the phone number 555-155-1555 (not a real number), you would use the following command string:

curl -d number=5551551555 -d "message=hello from"

Yes you could put another persons phone number in there too, but you probably should not do that without their permission.

If the text was successfully sent, the command line will return a message stating ‘{“success”:true}’, if it fails for whatever reason, it’ll look something like the following, which is usually indicative of an error in your command syntax: ‘{“success”:false,”message”: “Number and message parameters are required.”} curl: (6) Could not resolve host:’ Just review the command string and try again.

The text message should arrive to your iPhone or Android very quickly, though the expedience of the service likely depends on a queue and how much activity TextBelt is receiving from elsewhere. It’ll come through looking something like the following:

SMS texts on an iPhone as sent from TextBelt command line service

(If you’re wondering, responding to the texts goes nowhere and does nothing, it’s not a 2-way service)

This works to send texts from Mac OS X, Linux, and presumably whatever other OS or service has curl access. The recipient side should work with any mobile phone that accepts SMS, whether it’s an iPhone or an ancient brick Nokia.

Adding a Quick ‘Send Text’ Command to Bash

If you enjoy the convenience of sending yourself texts from the terminal and plan on using this often, you can create a simple bash script to shorten the command string by adding the following to your .bash_profile. Be sure to replace the number with your 10 digit phone number:

sendtext () { curl -d number=5551113333 -d "message=$1";echo message sent; }

With that in your bash_profile, you can simply type “sendtext your message goes here” to send out a text to yourself. This also allows for some fun and utility with double ampersands &&, like sending yourself SMS alerts when a software package has finished installing, or when a remote file is done downloading. Those with command line experience can probably think of a million and one other handy uses for this as well.

According to TextBelt, the service definitely works within the USA with the following cell networks: Alltel, Ameritech, AT&T Wireless, Boost, CellularOne, Cingular, Sprint PCS, Telus Mobility, T-Mobile, Metro PCS, Nextel, O2, Orange, Qwest, Rogers Wireless, US Cellular, Verizon, Virgin Mobile. This may be limited to the USA, but we’re unable to test networks outside of the region, let us know if you give it a try elsewhere.


Leave a Reply

Your email address will not be published. Required fields are marked *