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 with TXT in the subject, and the actual text body in the email body,
  • This goes to your mail server, 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

sms 10800 IN MX 10

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

echo "deb 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 to smsuser

echo " 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

LOGFILE=$HOME/proclog   # recommended for debugging
SMSUSER=086XXXXXX # put your mobile number here
SMSPASS=yourwebpass # and the matching password here
CARRIER=o2 # this can be o2, vodafone, or meteor

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

* ^Subject: TXT
| 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;


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.

, ,

  1. #1 by Abu on November 10, 2007 - 4:16 am

    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 by Bricky on November 10, 2007 - 6:21 am

    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.

  3. #3 by D.Sully on January 20, 2009 - 4:52 pm

    excellent, just what I was looking for, must give this a while on a VM later on.
    could be a handy one to use with a IP webcam & motion detection

  4. #4 by chaminda on February 16, 2009 - 12:08 pm

    I setup every thing. But it’s not work.
    I want clarify something.

    1.What is that SMSPASS
    2. Am I have to send text messages from the sub domain of parent domain ( or

    Please help me


  5. #5 by Bricky on February 16, 2009 - 12:17 pm

    chaminda :

    I setup every thing. But it’s not work.
    I want clarify something.

    1.What is that SMSPASS

    SMSPASS is your web password on,, or (depending on which service you use).

    2. Am I have to send text messages from the sub domain of parent domain ( or

    In the configuration I describe above, you would have to use The reason it needs a distinct domain is so that all emails for that domain will get forwarded to one account (which is not a normal arrangement).

  6. #6 by carolyn joseph on June 10, 2010 - 11:38 am

    Is it possible to em bade this code in my website. I’m really impressed with its features.

  7. #7 by shoes on November 14, 2011 - 7:36 am

    I discovered this is a helpful and interesting article, Thank you for the efforts.

  8. #8 by Quenten Grasso on July 25, 2012 - 2:54 am

    Hi, I had some issues with the formatting of the phone number which i used this command instead,

    PHONENUMBER=`formail -z -x”To:” | sed -n -e “s/^[^<]*]*\)>.*/\1/p” | cut -d’@’ -f1`


  9. #9 by Rod Okoye on December 22, 2013 - 7:51 pm

    Thanks for the methodology, implementation was fairly simple and it worked well enough.

  10. #10 by Alejandro on January 21, 2014 - 8:59 am

    Very simple implementation.. works great! Thanks a lot!

(will not be published)