-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendmail.go
180 lines (152 loc) · 4.29 KB
/
sendmail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2023, RetailNext, Inc.
// This software may be modified and distributed under the terms
// of the BSD license. See the LICENSE file for details.
// All rights reserved.
package sendmail
import (
"bufio"
"fmt"
"io"
"mime/quotedprintable"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/getsentry/raven-go"
)
var (
// GitCommit specifies the git commit sha, set by the compiler.
GitCommit = ""
// Version specifies Semantic versioning increment (MAJOR.MINOR.PATCH).
Version = "0.0.0"
)
var (
configPath = "/etc/sentry-sendmail.conf"
recipients = ""
)
// Config structure is where the config file options get de-serialized.
type Config struct {
SentryDSN string `toml:"DSN"`
Environment string
}
// SentryConfig sets the Sentry DSN and Environment by reading values from
// environment variables and the config file.
func SentryConfig() error {
var conf Config
_, err := toml.DecodeFile(configPath, &conf)
if os.Getenv("SENTRY_DSN") != "" {
conf.SentryDSN = os.Getenv("SENTRY_DSN")
}
if os.Getenv("SENTRY_ENVIRONMENT") != "" {
conf.Environment = os.Getenv("SENTRY_ENVIRONMENT")
}
// Error parsing the config file and we still don't have a DSN
if _, isFileErr := err.(*os.PathError); conf.SentryDSN == "" && err != nil && !isFileErr {
return fmt.Errorf("Can not read Sentry DSN from %s: %v", configPath, err)
}
if conf.SentryDSN == "" {
return fmt.Errorf("Sentry DSN not set. Please set SENTRY_DSN environment variable or enter DSN in the config file: %s", configPath)
}
err = raven.SetDSN(conf.SentryDSN)
if err != nil {
return fmt.Errorf("Sentry DSN [%s] error: %v", conf.SentryDSN, err)
}
if conf.Environment != "" {
raven.SetEnvironment(conf.Environment)
}
return nil
}
func getExtra(headers map[string]string) map[string]interface{} {
return map[string]interface{}{
"ppid": os.Getppid(),
"headers": headers,
}
}
// SentrySend sends the message to Sentry
func SentrySend(message string, headers map[string]string) error {
strLevel := "error"
pkt := raven.NewPacketWithExtra(message, getExtra(headers))
pkt.Level = raven.Severity(strLevel)
eventID, ch := raven.Capture(pkt, nil)
if eventID != "" {
err := <-ch
return err
}
return fmt.Errorf("Capture returned empty eventID")
}
// ReadData reads data envelope from an input stream and parses the message.
func ReadData(reader *bufio.Reader) (map[string]string, string, string) {
raw := ""
body := ""
headers := make(map[string]string)
var logFile *os.File
if opts.LogFile != "" {
logFile, _ = os.OpenFile(opts.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
fmt.Fprintln(logFile, time.Now().UTC().Format("2006-01-02T15:04:05.999Z"), "-----------")
defer logFile.Close()
}
// Read headers first
inHeaders := true
prevKey := ""
for {
line, err := reader.ReadString('\n')
if logFile != nil {
fmt.Fprint(logFile, line)
}
raw += line
if err != nil {
body += line
// If EOF here, line has not been processed and can be the body
return headers, body, raw
}
if !opts.IgnoreDot && len(line) == 2 && line[0] == '.' {
break
}
// Empty line indicates message body
if len(line) == 1 {
inHeaders = false
}
if inHeaders {
index := strings.Index(line, ":")
if index >= 0 || len(prevKey) > 0 {
if index < 0 {
headers[prevKey] += "\n" + strings.TrimSpace(line)
} else {
key := strings.ToLower(line[:index])
value := strings.TrimSpace(line[index+1:])
headers[key] = value
prevKey = key
}
} else {
inHeaders = false
}
}
if !inHeaders {
body += line
}
}
return headers, body, raw
}
// BuildMessage creates a Sentry ready message from an email envelope
func BuildMessage(headers map[string]string, body string) (string, error) {
message := headers["subject"] + "\n"
if !opts.ExtractRecipients {
headers["to"] = recipients
}
if len(opts.LegacyFrom) > 0 {
headers["from"] = opts.LegacyFrom
}
if len(opts.SenderAddress) > 0 {
headers["from"] = opts.SenderAddress
}
if len(headers["from"]) == 0 {
return message, fmt.Errorf("Sender must be specified")
}
if headers["content-transfer-encoding"] == "quoted-printable" {
decoded, _ := io.ReadAll(quotedprintable.NewReader(strings.NewReader(string(body))))
message += string(decoded)
} else {
message += string(body)
}
return message, nil
}