Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ManabuSeki committed Jan 24, 2019
0 parents commit 6ff007f
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# RootMailSlackNotifier

## DESCRIPTION
Notify slack of mail addressed to Root.

## HOW TO USE

1. Please download the file suitable for your environment.
2. Unzip the downlaod file.
3. Move to this binary file to `/usr/local/bin/slack-notifier`
4. Create file in `/etc/postfix/slack_notice.json`
```json
{
"WebhookURL":"Slack_Webhook_URL",
"Username":"Username",
"Channel":"general",
"IconEmoji":":email:",
"Color":"warning"
}
```
5. postfix aliase setting
Add the following setting to the bottom line.
```
# vi /etc/aliases

root: "| /usr/local/bin/slack-notify -config /etc/postfix/slack_notice.json"
```
6. Update alias
```
# newaliases
```
106 changes: 106 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/mail"
"os"
"strings"

slack "github.com/ashwanthkumar/slack-go-webhook"
)

// Config is configuration struct
type Config struct {
WebhookURL string `json:"WebhookURL"`
Username string `json:"Username"`
Channel string `json:"Channel"`
IconEmoji string `json:"IconEmoji"`
Color string `json:"Color"`
HostName string `json:"HostName"`
}

// flag
var (
s string
)

func main() {
flag.StringVar(&s, "config", "/etc/postfix/slack_notice.json", "config filepath")
flag.Parse()

// JSON Input
jsonString, err := ioutil.ReadFile(s)
if err != nil {
log.Fatal(err)
}

var config Config

if err := json.Unmarshal(jsonString, &config); err != nil {
log.Fatal(err)
}
// WebhookURL
if config.WebhookURL == "" {
log.Fatal("WebhookURL is not setting")
}

// Username
if config.Username == "" {
config.Username = "RootMail"
}

// Channel
if config.Channel == "" {
config.Channel = "#general"
}

// IconEmoji
if config.IconEmoji == "" {
config.IconEmoji = ":email:"
}

// HostName
if config.HostName == "" {
config.HostName, _ = os.Hostname()
}
mailInput, _ := ioutil.ReadAll(os.Stdin)
m, err := mail.ReadMessage(strings.NewReader(string(mailInput)))

if err != nil {
log.Fatal(err)
}
header := m.Header
body, err := ioutil.ReadAll(m.Body)
if err != nil {
log.Fatal(err)
}

txt := "*ホスト:*\n" +
config.HostName + "\n" +
"*件名:*\n" +
"```" +
header.Get("Subject") +
"```\n\n" +
"*本文:*\n" +
"```" +
string(body) +
"```"
attachment := slack.Attachment{Text: &txt, Color: &config.Color}
payload := slack.Payload{
Text: "*Received an email addressed to root.*",
Username: config.Username,
Channel: config.Channel,
IconEmoji: config.IconEmoji,
Attachments: []slack.Attachment{attachment},
}

errs := slack.Send(config.WebhookURL, "", payload)

if len(errs) > 0 {
fmt.Printf("error: %s\n", errs)
}
}

0 comments on commit 6ff007f

Please sign in to comment.