Slack hack: local mail delivery

Written by jakubroztocil | Published 2016/12/03
Tech Story Tags: slack | bots | devops | productivity | automation

TLDRvia the TL;DR App

Then one / Wired

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. And because they use 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 need to SSH to the server to get a cute You have new mail notification in the terminal.

The common solution is to enable forwarding to an external email address. But this is unreliable 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:

  1. 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 [email protected] > /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:

Mail from logwatch delivered to Slack


Published by HackerNoon on 2016/12/03