Plugging Away: Automated Text Messaging To Your Cell Phone

251

Suppose you have a Plug Computer (or any other Linux system) serving up Web pages and you want to make sure the thing is up and running. Wouldn’t it be great to get periodic messages about the health of your system, on your cell phone.

While there are lots of ways to email a message to a phone from Linux, I’ll cover how to use the sendEmail program today, specifically on the Plug Computer. SendEmail is a simple command line outbound SMTP email client that you can use to type in a note, run from a script, or kick a message off automatically from a cron job. It is easy to use and doesn’t require any other programs to function properly.

As soon as I get the Arduino microcontroller data feed working with the Plug Computer, I’ll start building applications where I read sensor data, interpret what is going on in the physical world and then send short, informative messages to my cell phone. Obvious applications could include physical security and fault monitoring, process completion acknowledgements, or simple broadcast messaging (think: push a couple of buttons on a box somewhere and I get a cell phone message).

Trivial examples? Perhaps. But remember… when in doubt, use the power of Linux to automate everything in sight.

Send A Message From The Command Line

Installing sendEmail on the Plug Computer couldn’t be easier, just use apt-get.

root@debian:/# sudo apt-get install sendemail

Remember that you’ll have to create the /var/cache/apt/archives/lock and /var/cache/apt/archives/partial directories, before apt-get will cooperate. The Plug Computer doesn’t retain those directories after a reboot. Surely, there is a good reason for this, but I don’t know what it is and don’t really need to spend time figuring it out. If you figure out how to make it permanent, send me a note.

In an effort to save space on the Plug Computer, man pages have not been installed, but you can get a good idea of the options using the –help option.

root@debian:/# sendEmail --help

Did you notice that the command is “sendEmail” and not “sendemail”. No sense in chasing your tail, too.

The basic form of the command is:

# sendEmail -f [from email address] -t [to email address] -u [mail topic]
-m [message body] -s [smtp server:port] -xu [your smtp server user name] -xp [your smpt server password]

Make sure the message summary and bodies are quoted, since they contain words with spaces. Obviously, your Plug Computer should be hooked up and running properly, on your network. You should also be signed up for text messaging through your cell phone provider.

The following line is a slightly modified (yes, I value my privacy) example of the command I used for a message from my Plug Computer.

# sendEmail -f 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  -t 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  -u "test message 2"
-m "this is another test" -s smtp.mynetwork.com:587 -xu This e-mail address is being protected from spambots. You need JavaScript enabled to view it -xp 5mypassword

If your first message doesn’t work, make sure of the to address and the port used by your SMTP server. I assumed that we were using an Internet Service Provider’s SMTP server. The default is usually 25, my ISP uses 587. Another reason a message may not go through is because your network router may be blocking outbound traffic on your particular port number. You may have to modify those settings.

I also noticed that whenever I sent a message that failed (through my ISP’s SMTP server), an error report showed up in my regular email. This might be helpful in troubleshooting any issue you may have with your command line.

More coolness can be had by sending messages to multiple email/cell phone addresses using the -cc option. All you have to do is add it into your command line using a space between each address. Here’s another example.

# sendEmail -f 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  -t 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  -cc 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  -u "test message 2"
-m "this is another test" -s smtp.mynetwork.com:587 -xu This e-mail address is being protected from spambots. You need JavaScript enabled to view it -xp 5mypassword

This option might be applicable if several sysadmins are on-call and they need to be notified if some backup is completed or a certain user logs into the system. I’m sure many of you will see how the sendEmail command can be used to make your life easier.

Be sure to take appropriate security precautions, when connecting to your SMTP server.

Running sendEmail In A Script

We start to get more automated when you put the sendEmail line in a shell script.

I opened a new file called messageout with VI and simply stuck the command on the first line. Next, I changed the message section to read the first command line argument.

# sendEmail -f 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  -t 
 This e-mail address is being protected from spambots. You need JavaScript enabled to view it
  -u "test message 2"
-m $1 -s smtp.mynetwork.com:587 -xu This e-mail address is being protected from spambots. You need JavaScript enabled to view it -xp 5mypassword

Lastly, change the permission on the messageout file to make it executable.

# chmod +x messageout

Now, you can execute the script and add your own message (see Figure 1).

# ./messageout "This message is new"

The script will run and send your message to your phone. I plan to package my Arduino microcontroller sensor readings into short messages that will feed my messageout script as the first argument. Figure 2 shows the results on my cell phone.

You can also integrate the messageout script into the cron facility. This allows collection of system data (from information on the system) or readings from a microcontroller attached sensor and then forward that information, in a message to you at a certain time. Maybe you’d want to get a sensor reading every hour, but you can’t or don’t want to stay logged into the Plug Computer, over SSH. A cron job running the messageout script with the sensor data number that sends the number to your phone, as a short message, might just do the trick.

Wrap Up

Collecting data and automatically sending emails to your cell phone is a natural with the Plug Computer or any Linux system. It’s small, doesn’t take many computing resources, and is easy to implement. I’ve outlined the basics of using the sendEmail program to accomplish the task. Now it’s up to you to go out and take advantage of the magic of automation, with Linux, sendEmail, and the Plug Computer.

This e-mail address is being protected from spambots. You need JavaScript enabled to view it
is a consultant and freelance technology writer. He is always interested in new writing projects, working with conferences, and delivering value to his clients. Visit his Web site at http://home.earthlink.net/~robreilly.