Sending SMS and Email Messages
A fully functional sendmail and SMS in Unix (FreeBSD 5.2)
Posted 01.10.2005 | Updated 23.05.2006 | Contributed by Andy Mallett


Unix is built with security in mind and this is the main reason why FreeBSD does not natively have a proper, fully-blown SMTP email facility.

If it did, every Unix server would be a potential spam machine.

With IMAP it is usual to email system logs and other information to users who have accounts on the server. Users may pull the email across for reading onto a separate workstation, using an email client like Eudora or Outlook.

But don't confuse this with SMTP mail; the emails are still residing on the Unix server itself.

Now most of the time this situation is fine. You log-in, get your info and messages and log out again. I've written an Email Alerter Script for my DHCP server at work to mail me if a new system attempts to join the network.

This works well, although it does rely on my Outlook 2003 email client connecting to the Unix DHCP Server every five minutes to check for messages. And of course, I have to be there to read the emails.

An Outing Changes Everything

Some of my Cert IV Projects team and I visited UTAS last week to check out the IT students' presentations of their final projects to the public. M'mann Brad 'Robbi' Robinson was with us and we met a student whom Brad knew, by the name of Mark Wise. Mark's project was to conduct health surveys for a real world client and he's devised a way to do this using SMS. Specifically he showed us how he was sending the SMS surveys from a PC using email.

As soon as it clicked with us what Mark was doing, Brad and I looked at each other with huge grins, our imaginations buzzing with the immense possibilities of a system like this. We pressed in closer, trying not to dribble too much.

Mark makes use of an 'SMS Gateway' service from a company called Messagenet.com.au, which converts emails into SMS messages. It really is as easy as sending an email addressed as mobilenumber@messagenet.com.au and the rest is taken care of by MessageNet, who charge a reasonable 25c a message to shoot it off to the specified recipient's mobile. You can sign up for ten free messages and I recommend you try the system out.

Deliberations..

Brad and I thought about how useful it would be to be able to SMS ourselves from our Unix servers (or other systems), in the event of..well..erm, some event we wished to be informed of. Somebody trying to hack into the system, for example.

I thought of my own email alerter system for the DHCP service and decided it would be a good guinea pig for trying this SMS thing out.

Message from Unix
How then to get my Unix box to email out that SMS message. Well, I spent a week looking at various PERL scripts and my colleague Mike Capstick and I had a play with some possibilities, but we couldn't get the emails to be acceptable for transmission. Specifically the recipient email server checks the domain of the originating message to make sure it's real. I was emailing from a Unix box on IT.NET and the world's Mail Servers don't know about this little place we call paradise.

SendEmail

I ran it past m'mann Nick 'Frat-Boy' Fratangelo and he mentioned an application he knew of called sendEmail which apparently does the full SMTP thing and may do the job. SendEmail has been put together by a bloke called Brandon Zehm who runs a site called Caspian's Homepage where this little script can be downloaded.

Version used: sendEmail-v1.52.tar.gz [22K]

SendEmail is written in PERL (you will need to be running PERL) and is a great little piece of work. This version 1.52 is the result of many contributors who have gradually polished up the code. And it's really easy to use. After extracting the tarball, copy the single sendEmail script to the /usr/local/sbin directory, which being in the system path, is then invokable from anywhere in the directory tree, just like the native Unix sendmail.

Then all that's needed is to write a little script to send off a test mail. The sendEmail script comes with clearly documented Usage Instructions and examples. Here is an example of my own shell script - sms.sh, used for sending off an SMS-email..

#!/bin/sh
#Andy's Emailer using sendEmail

sendEmail -f sender@domain.com \
          -t 04091234567@messagenet.com.au \
          -s sendmail.yoursmtpserver.com \
          -xu secure_smtp_username \
          -xp secure_smtp_passwd \
          -u "66666 Test SMS" \
          -m "This is a test SMS from your Unix Server.." \


In the above example, sendEmail is invoked with the following parameters:

-f  (from:) - specifies the sender's email address
-t  (to:) - destination address, in this case a mobile number at messagenet.com.au
-s  (smtp) - name of SMTP server
-xu (secure username) - authorised username for secure SMTP servers
-xp (secure passwd) - authorised user's password for secure SMTP servers
-u   (subject) - subject line
-m   (message) - message

Note that the subject line contains the number, 66666. This is the password number for the messagenet service, which must be included in the subject line or message body. It is used to prevent SMS spamming and other abuse of the SMS-email system and is stripped out of the final SMS message.

Don't forget to make the original sendEmail PERL script and your own email sending script executable before running it..

From Unixbox to Mobile

So this system is basically the same as sending an email from Outlook and still requires the use of a bona fide SMTP Server to send the email out. I use one of my web hosting service's Mail Servers at sendmail.mallett.net, but your ISP's Mail Server should do the job.

Although the Usage Instructions imply the -xu and -xp switches are not normally used (listed, amusingly, under the Paranormal section), I would suggest that most mail servers these days use secure SMTP to reduce spam and will require an authorised user's credentials to send an email. Naturally this whole system can be used to send normal emails too.

SUMMARY
  1. Sign up for a free trial account with MessageNet
  2. Download and extract the sendEmail PERL script to /usr/local/sbin
  3. Create an SMS/Email script using the parameters in the Usage Instructions
  4. Include the MessageNet password code in the Subject line or message body
  5. Make sure all scripts are executable before running


Applications

- DHCP Service sends an administrative SMS if new system joins..