Created
June 13, 2018 04:10
-
-
Save alexlouden/dafaac119b56e037e5c5f405e0a51dc4 to your computer and use it in GitHub Desktop.
Python program that pretends to be sendmail, sends to Slack webhook instead. Move to /usr/lib/sendmail and make executable (`chmod +x`)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import json | |
import requests | |
slack_url = "https://hooks.slack.com/services/<webhook URL>" | |
def get_subject(lines): | |
try: | |
# Find subject | |
for line in lines: | |
if line.startswith('Subject:'): | |
subject = line.split(':', 1)[1] | |
return subject.strip() | |
except Exception as e: | |
print('get_subject exception', e) | |
return '' | |
def get_body(lines): | |
try: | |
# Find body | |
for i, line in enumerate(lines): | |
# first blank line | |
if line == '\n': | |
body_lines = lines[i + 1:] | |
# list to string | |
return ''.join(body_lines) | |
except Exception as e: | |
print('get_body exception', e) | |
return '' | |
# Read from stdin until there is no more | |
lines = [] | |
for line in sys.stdin: | |
lines.append(line) | |
subject = get_subject(lines) | |
body = get_body(lines) | |
text = f'*{subject}*\n{body}' | |
slack_data = {'text': text} | |
# Fire and forget | |
requests.post( | |
slack_url, data=json.dumps(slack_data), | |
headers={'Content-Type': 'application/json'} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment