/ root / pages / text_from_cli.html

You're using an old link! - Thankfully, you no longer need to specify a nonstandard port (8080) to access my site. You could've used the more standard: http://pbrisbin.com/pages/text_from_cli.html.

Text From CLI


This is a short but extensible script to allow text messaging (to verizon customers) straight from the commandline.

Setup requires simply a means to send email from the commandline along with a small script to pass the message off to <number>@vtext.com.

If you already have a CLI mailing solution you can just copy the script and go ahead and change the mail command to mutt, ssmtp, mailx, or whatever you're using.

Email from CLI

I use msmtp to send mails in mutt so it was easy for me to adapt that into a CLI mailing solution.

Here's a ~/.msmtprc for gmail:

  # msmtp config file

  # gmail
  account gmail 
  host smtp.gmail.com
  port 587
  protocol smtp
  auth on
  from username@gmail.com
  user username@gmail.com
  password gmail_password
  tls on
  tls_nocertcheck

  account default : gmail

Right now, as-is, it's possible for you to echo "Some text" | msmtp someone@somewhere.com and it'll email just fine. I'd like to make things a little more flexible.

By dropping a file in ~/.mailrc we can change the mail command to use whatever binary we want instead of the default /usr/bin/sendmail

  set sendmail=/usr/bin/msmtp

Now, anytime your system mails anything on your behalf, it'll use msmtp.

The Script

#!/bin/bash

if [[ $# -lt 2 ]]; then
  echo "usage: $0 [number] [some message]"
  exit 1
fi

number="$1"; shift

echo "$*" | mail "$number@vtext.com"

Now you've got this little sendtext.sh script in your back pocket that can be called from remind, cron, rtorrent, or any other script to notify you (or other people) of whatever you want.

  sendtext.sh 1234567890 'This is a test text, did it work?'

Sure did.

Update!

I've changed my approach slightly. I now use a function instead of an all out script, and I check for a valid number, nonzero message length, less than 160 message length, and the function can accept the message on stdin. Feel free to rm sendtext.sh and just add the following to your ~/.bashrc.

# send someone a text, verizon only
sendtext() {
  number="$1"; shift; message="$*"

  # try stdin
  [[ -z "$message" ]] && message="$(cat /dev/stdin)"

  if [[ -n "${number//[0-9]/}" ]]; then
    echo "not sending; invalid number $number"
    return 1
  fi

  if [[ -z "$message" ]]; then
    echo "not sending; no message to send"
    return 1
  fi

  if [[ $(wc -c <<< "$message") -gt 160 ]]; then
    echo "not sending; message greater than 160 characters"
    return 1
  fi

  echo $message | mail "$number@vtext.com"
}

You can then use it with any of the following:

  sendtext 1234567890 "this is the message"

  sendtext 1234567890 < ./file_with_a_message

  anycommand | sendtext 1234567890

Enjoy!

Update 2

Well, Ghost1227 was bored again. He took my sendtext script and ran with it. Added loads of carriers and some new option handling.

I took his update of my script and re-updated it myself. Mainly syntactical changes and minor options handling, just to tailor it to my needs.

The new version with my and ghost's changes can be downloaded here.

I also added simple phone book support. When sending a message to someone, pass -s <number> <name> and the contact will be saved to a text file. After that, you can sendtext <name> and the most recent match out of this text file will be used. The service is saved as well (either the default or the one passed as an argument at the time of -s).

Comments





pbrisbin dot com 2010