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.
Overview
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
[gammu] port = /dev/ttyUSB0 connection = at [smsd] Service = files RunOnReceive = sudo python /home/pi/rpi-sms-gateway/receivesms.py 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/gammu-smsd.pid --daemon
(also need to give gammu superuser permissions: visudo, see here https://stackoverflow.com/questions/29399588/gammu-run-on-receive-exit-status-1, and https://wammu.eu/docs/manual/smsd/smsd.html)
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 https://github.com/andy-pi/rpi-sms-gateway.git
6. Create config.py 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 (https://wammu.eu/docs/manual/smsd/run.html#gammu-smsd-run). 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: https://gist.github.com/rcoup/93460ea39b05e957e884. 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.