Last active
June 1, 2017 15:01
-
-
Save ardichoke/f20bcbf0a0d75a519405d9f7574bf223 to your computer and use it in GitHub Desktop.
Ruby script to parse mail and send to Slack using slacktee
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 ruby | |
slacktee = '/usr/local/bin/slacktee.sh' | |
require 'mail' | |
require 'syslog' | |
require 'shellwords' | |
shortfields = ['to', 'from'] | |
longfields = ['subject', 'date'] | |
slackargs = "-p -a" | |
mail = Mail.new(STDIN.read) | |
mail.header.fields.to_a.each do |h| | |
if longfields.include? h.name.downcase | |
slackargs = "#{slackargs} -e #{h.name.shellescape} #{h.value.shellescape} " | |
elsif shortfields.include? h.name.downcase | |
slackargs = "#{slackargs} -s #{h.name.shellescape} #{h.value.shellescape} " | |
end | |
end | |
output = %x( echo #{mail.decoded.shellescape} | #{slacktee} #{slackargs} ) | |
Syslog.open("mailbridge", Syslog::LOG_CONS, Syslog::LOG_MAIL) | |
unless $?.success? | |
Syslog.log(Syslog::LOG_ERR, "Error sending mail to slack: #{output}") | |
exit 1 | |
else | |
Syslog.log(Syslog::LOG_INFO, "Mail to #{mail.to} sent to Slack") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment