Forwarding Emails to Slack
You receive notification emails often and would rather send them to Slack instead of having them clutter your inbox. Here’s a DIY solution that takes around 10 minutes to set up. Things you’ll need:
Let’s get started.
Copy this file to your server. It’s a tiny Flask app that listens for incoming messages and forwards them to Slack. Edit the values of
SLACK_CHANNELto contain your Slack API token and the desired channel, respectively.
Install the app’s library dependencies with
pip install Flask requests. (Optionally, use a virtual environment.)
Start the app with
python router.py. It’s now listening on port 8003.
Go to the Routes section of your Mailgun control panel and create a new route with filter expression
example.comwith the public address of your server. (If your server isn’t already accessible from the internet, ngrok may come in handy. I use it to host from a Raspberry Pi within my office’s LAN.)
Go to the Domains section of your Mailgun control panel and click on the sandbox entry. Take note of the “Default SMTP Login”, which looks something like
Go into Gmail (or whatever email client you use) and set up email filters that forward incoming messages to the Mailgun address obtained in the previous step.
Any incoming email that matches your filter(s) will now be forwarded to Mailgun, which will in turn forward it to the Flask app running on your server, which will finally forward it to Slack.
Messages from the same sender will always have the same avatar. The message body will consist of the original email’s subject line by default; you can customize this behavior in the Flask app.