E-mail to SMS

If you are dealing with network monitoring, you might consider using SMSes because they are completely independent. On the other hand, many monitoring systems don’t support  HTTP calls (which is the usual way for sending them) but most of them support sending e-mails. Here I want to describe how to configure your server to receive e-mails and transform them to SMSes.

You will need:

  • Linux server (in my case Debian based)
  • 4G/3G/2G USB modem plugged in with SIM with PIN disabled

Installation

  1. Install packages
    $ apt install postfix smstools usb-modeswitch
  2. disable PIN on your SIM card
  3. reinsert USB modem after installing usb_modeswitch
  4. create user sms
    $ adduser sms
  5. configure postfix
    1. setup local mail delivery the usual way
    2. $ systemctl enable postfix
    3. send a test e-mail to address sms+12345@<yourserver>
    4. check if it is received in $HOME/Mailbox or /var/spool/mail/sms
  6. configure smstools
    1. edit /etc/smsd.conf, in my case the following section was enough:
      [GSM1]
      device = /dev/ttyUSB0
      init = AT^CURC=0
      incoming = yes
      baudrate = 19200
    2. $ service smstools restart
    3. test if SMS sending works by creating SMS:
      $ cat << EOF > /var/spool/sms/outgoing/test
      To: +421903123456test sms
      EOF
    4. $ systemctl enable smstools
  7. login as sms user
    $ su sms
  8. create $HOME/.procmailrc file with the following contents:
    # store mail in default folder and continue after that
    :0c
    ${DEFAULT}
    
    :0
    | umask 000 ; $HOME/email-to-sms.py
  9. create $HOME/email-to-sms.py file with the following contents:
  10. chmod +x  email-to-sms.py
  11. edit email-to-sms.py to allow permitted senders
  12. send email to sms+destinationnumber@<yourserver> and watch the magic

How does it work?

In email systems, it’s common that you can use plus sign to deliver to same mailbox (the portion before +)  and to “tag” your e-mails (the portion after +) (Subaddressing). So all emails are delivered to user “sms”.

The next component we use is procmail which can do many sorts of useful filtering, in our case we let all e-mails to be delivered to the mailbox but then we also pipe them to a special script named email-to-sms.py

email-to-sms.py then parses the incoming e-mail, extracts sender and destination number, checks sender whitelist, extracts plain-text message content and creates SMS in outbound queue of smsd.

smsd then only simply sends the message via attached modem.

Easy, isn’t it? 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.