This is part 3 of our series in building an SMS to email gateway. Now we’re setup with the hardware tested and working, it’s time to write some python code for receiving an SMS and forwarding it to an email address.

I’m planning to use Mailgun, an online transaction mail service rather than setting up and email server on the Pi itself. Basically, on receiving a text message, gammu smsd program should run a python script which forwards the message content and number to my email address using the Mailgun API. Gammu smsd is the daemon of our previously installed gammu program, which manages the sending and receiving of sms.

Gammu smsd setup
1. Installation

sudo apt-get install gammu-smsd

2. Config file – /etc/gammu-smsdrc

port = /dev/ttyUSB0
connection = at
Service = files
RunOnReceive = sudo python /home/pi/rpi-sms-gateway/
debuglevel = 4
logfile = syslog
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/

3. Setup gammu daemon to run as a linux service and add to crontab

sudo crontab -e
@reboot gammu-smsd --config /etc/gammu-smsdrc --pid /var/run/ --daemon

(also need to give gammu superuser permissions: visudo, see here, and

Note – once this daemon is running the standard gammu commands (post 1 in this series won’t work, you’ll need to stop the daemon before using them, or run sudo gammu-smsd-monitor for a quick check)

4. Setup Mailgun
Mailgun is an email relay provider that can be accessed by an API, and can also trigger a webhook – aka to allow a web application to send an receive emails rather than delivering it to an inbox. So, first thing, you’ll need to setup an account, add a new domain, and then add the Mailgun TXT record to your DNS records.

5. Install the python code

git clone

6. Create in rpi-sms-gateway directory with the following info from your mailgun account:

mailgun_user = 'xxxxxxxxxxxxxxxxx'
mailgun_api_key = 'xxxxxxxxxxxxxxxxx'
mailgun_request_url = 'xxxxxxxxxxxxxxxxx'

7. Now is the time to send a test sms to your number and check that it forwards to the email!

Problems encountered

1. It would be preferable to install this into a python virtualenv, but for some reason I could only get the python-gammu bindings installed into the root version of python…

2. The second issue I encountered was that the sms number and sms text were not being saved as an environment variable as assumed by the official python example ( Since we are saving the sms to a file (possible to use a database if you wish to set it up), we can parse these files for the information instead. My code is a simplified version of this: Basically, the gammu run on receive command sends the filename in the inbox of the text message received as a command line argument. We parse this

Next time we’ll look at the (more complicated) sending of an sms from an email.