Slack hack: local mail delivery
It’s not a secret that Slack is great for team communication. But what makes it shine is the ability to connect anything to your team’s channels.
“Botifying” a server
If you run any servers at all, chances are they occasionally send you system notifications. Because these are sent using the local mail delivery system, it’s likely you never get to read them or only when it’s too late; all local mail by default ends up in/var/spool/mail/root.
You will need to SSH to the server to learn about them from a cute You have new mail notification in the terminal.
The common solution is to enable forwarding to an external email address. This solution is unreliable, however, because messages get often marked as spam or rejected by the receiving server—unless you take extra care to make your host look like a legit mail server to the outer world.
Another issue is that Unix servers don’t come with a Share in Slack feature for some reason.
Let’s quickly fix both:
- Add a new email integration to your Slack team and copy the email address:
2. Install a mail transport agent on your server, for example:
$ yum -y install mailx postfix && systemctl enable postfix && systemctl start postfix
3. Enable forwarding of all root
mail to Slack:
$ echo your-token@your-team.slack.com > /root/.forward
Done!
You’ve just established a direct email line from your server to Slack. Let’s test it out:
$ echo 'Hello, Slack!' | mail -s 'Check 123' root
Or something a little bit more useful:
$ pg_dump | mail -s 'DB backup' root
…just kidding, don’t do that. But you get the idea.
As you can see, you can now send messages from your server to Slack simply by mailing to root
.
By the way, this is what system services—such as cron
, logwatch
, and others—already use for notifications about important events and problems. So you’ll get those automatically to Slack as well: