Skip to content

Commit 51af82c

Browse files
committed
chore: switch from parsing manually to mail library
1 parent 25986ea commit 51af82c

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ slendmail
33

44
# editor files
55
**.code-workspace
6+
Logs/

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Slendmail
22

3+
A portmanteau of slack and sendmail
4+
35
## sendmail meets slack
46

57
A cron compatible sendmail alternative that sends messages to slack instead of
@@ -22,6 +24,9 @@ slack_token = "xoxb-123456789012-12345678901234-l;iqwjecacwiejfQWERoifqjwQWE" #
2224
channel = "#notifications-cron"
2325
```
2426

27+
Alpine: Add `MAILTO` to /etc/crontabs/root
28+
Ubuntu: Add `MAILTO` to /etc/crontab
29+
2530
## TODO
2631

2732
* More compatibility

main.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
package main
33

44
import (
5+
"bytes"
56
"fmt"
67
"io"
78
"log"
89
"log/syslog"
10+
"net/mail"
911
"os"
10-
"strings"
1112

1213
"github.com/pelletier/go-toml/v2"
1314
"github.com/slack-go/slack"
@@ -21,8 +22,6 @@ type Config struct {
2122
}
2223

2324
func main() {
24-
var subject string
25-
var body []string
2625
var config Config
2726

2827
// set a default for syslogtag
@@ -44,6 +43,7 @@ func main() {
4443
// case of being called from crond
4544
sl, err := syslog.New(syslog.LOG_WARNING|syslog.LOG_MAIL, config.SyslogTag)
4645
if err != nil {
46+
_ = sl.Err(fmt.Sprintln("failed to setup syslog connection ", err))
4747
log.Fatal("failed to setup syslog connection ", err)
4848
}
4949

@@ -52,39 +52,48 @@ func main() {
5252
// callers. So far only tested with busybox/Alpine crond
5353
stdin, err := io.ReadAll(os.Stdin)
5454
if err != nil {
55-
log.Fatal("failed to read anything from stdin ", err)
55+
// this really shouldn't fail
56+
log.Fatal("failed to read stdin", err)
5657
}
57-
lines := strings.Split(string(stdin), "\n")
58-
for n, line := range lines {
59-
if strings.HasPrefix(line, "Subject: ") {
60-
subject = strings.SplitAfterN(line, " ", 2)[1]
61-
}
62-
if line == "" {
63-
body = lines[n:]
64-
break
65-
}
58+
_ = sl.Debug(string(stdin))
59+
msg, err := mail.ReadMessage(bytes.NewReader(stdin))
60+
if err != nil {
61+
_ = sl.Err(fmt.Sprintln("failed to read stdin email format ", err))
62+
log.Fatal("failed to read stdin email format", err)
63+
}
64+
body, err := io.ReadAll(msg.Body)
65+
if err != nil {
66+
_ = sl.Err(fmt.Sprintln("failed to read email body ", err))
67+
log.Fatal("failed to read email body", err)
6668
}
6769

6870
// setup slack message
71+
attach := new(slack.Attachment)
72+
attach.Text = string(body)
73+
hostname, _ := os.Hostname()
74+
subjText := slack.NewTextBlockObject("mrkdwn", "*Subject:* "+msg.Header.Get("Subject"), false, false)
75+
hostText := slack.NewTextBlockObject("mrkdwn", "*Hostname:* "+hostname, false, false)
76+
hdrBlock := make([]*slack.TextBlockObject, 0)
77+
hdrBlock = append(hdrBlock, subjText)
78+
hdrBlock = append(hdrBlock, hostText)
6979
smsg := slack.MsgOptionBlocks(
7080
slack.NewSectionBlock(
71-
slack.NewTextBlockObject("mrkdwn", "*Subject:* "+subject, false, false), nil, nil,
72-
),
73-
slack.NewDividerBlock(),
74-
slack.NewSectionBlock(
75-
slack.NewTextBlockObject("mrkdwn", strings.Join(body, "\n"), false, false), nil, nil,
81+
nil,
82+
hdrBlock,
83+
nil,
7684
),
77-
slack.NewDividerBlock(),
7885
)
7986

8087
msgchan, msgts, err := api.PostMessage(
8188
config.Channel,
8289
smsg,
90+
slack.MsgOptionAttachments(*attach),
8391
)
8492
if err != nil {
93+
_ = sl.Err(fmt.Sprintln("failed to post message ", err))
94+
log.Println("body", string(body))
8595
log.Fatal("failed to post message ", err)
8696
}
8797

88-
sl.Debug(fmt.Sprintf("channel: %s - ts: %s - argv: %v", msgchan, msgts, os.Args)) //nolint:errcheck
89-
sl.Debug(string(stdin)) //nolint:errcheck
98+
_ = sl.Debug(fmt.Sprintf("channel: %s - ts: %s - argv: %v", msgchan, msgts, os.Args)) //nolint:errcheck
9099
}

0 commit comments

Comments
 (0)