BrickyBox

09 Nov

Email to SMS gateway

This is something I’ve implemented several times over the last few years. The principle is simple:

  • You send an email to mobile_number@sms.yourdomain.com with TXT in the subject, and the actual text body in the email body,
  • This goes to your mail server mail.yourdomain.com, and gets forwarded to the user smsuser
  • A procmail recipe in smsuser’s home dir strips out the mobile number and the body of the message, and, using o2sms, sends a regular sms to mobile_number
  • The output from o2sms is sent back to you via email, so you know it all worked out ok.

It’s all free, and reliable.

This is how I went about setting it up on ubuntu:

First, sort out the dns entry. This may take a while to propogate, so getting it sorted first is a good idea. Add the following line to the zone file for yourdomain.com

sms 10800 IN MX 10 mail.yourdomain.com.

Next, install o2sms. The following will work for any debian derivative (eg. ubuntu)

echo "deb http://apt.mackers.com/ unstable contrib" >> /etc/apt/sources.list
apt-get update
apt-get install o2sms

Now add the smsuser

adduser smsuser

If you don’t have procmail installed, install it and tell postfix to use it

apt-get install procmail
postconf -e "mailbox_command = /usr/bin/procmail -p"

The -p flag tells procmail to preserve the environment in which it was called (it would normally clear it). This is useful later.

Next we do some tweaking so that postfix will send all mail for sms.yourdomain.com to smsuser

echo "@sms.yourdomain.com smsuser" >> /etc/postfix/virtual
postmap /etc/postfix/virtual
postconf -e "virtual_alias_maps = hash:/etc/postfix/virtual"
postfix reload

Create the following file /home/smsuser/.procmailrc

SHELL=/bin/sh
PATH=/bin:/usr/bin
LOGABSTRACT=all
LOGFILE=$HOME/proclog   # recommended for debugging
VERBOSE=on
SMSUSER=086XXXXXX # put your mobile number here
SMSPASS=yourwebpass # and the matching password here
CARRIER=o2 # this can be o2, vodafone, or meteor
REPLYFROM=sms-results@sms.yourdomain.com

PHONENUMBER=`formail -z -x"To:" | cut -d'@' -f1 | sed -e 's/[^0-9\+]//g'`

:0c
* ^Subject: TXT
{
:0ci
| echo "Subject: SMS send log\n\n" > $HOME/smslog; \
  formail -k -x"Dummy:" | \
  o2sms -u $SMSUSER -p $SMSPASS -C $CARRIER --embedded $PHONENUMBER >> $HOME/smslog 2>&1; \
  /usr/sbin/sendmail -i -f $REPLYFROM $SENDER < $HOME/smslog;

:0
.SentMsgs/
}

Change SMSUSER, SMSPASS, REPLYFROM, and CARRIER above to match your details.

Note: the formail -k -x"Dummy:" is because I can’t get formail -k to work without at least a -x header. If anyone knows a better way, please do tell.

If you’ve problems,

tail -f /home/smsuser/proclog

will tell you alot.

Full credit of course goes to David McNamara for writing and maintaining o2sms, without whose help none of this would be possible.

Related Post

  • No Related Post

2 Responses to “Email to SMS gateway”

  1. 1
    Abu Says:

    The 02SMS service seems very nifty and free but, in my opinion, can hardly be considered as reliable.

    From what I read, it simulates a web browser which, according to my understanding:

    1. Opens up the network’s login page.
    2. Logs in and maintains the seesion.
    3. Opens up the page with the form used by human users to send sms.
    4. Submits the form to send the sms.

    If my understanding is right, the network simply needs to change the location of either pages or change the names of any input variables on either pages and the whole thing falls apart.

    Please correct me if i’m wrong.

  2. 2
    Bricky Says:

    You’re absolutely right Abu, reliable was the wrong word to use. It implies ‘assured delivery’, which this setup does not provide.

    However, it should be pointed out that this solution does provide ‘assured notification of failure’ (since it emails you the output of o2sms), which is usually more than you’d get from a similar solution using tap, gsm modems, gnokii etc.

    Also note that this setup is limited to 250/300 messages per month (depending on provider) and so does not compete with the services you offer.

Leave a Reply

© 2009 BrickyBox | Entries (RSS) and Comments (RSS)

Powered by Wordpress, based on a design by Web4 Sudoku, which is in turn based on Pinkline by GPS Gazette