forked from thecubed/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputemail.go
81 lines (71 loc) · 2.07 KB
/
outputemail.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
package outputemail
import (
"context"
"crypto/tls"
"strings"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
"gopkg.in/gomail.v2"
)
// ModuleName is the name used in config file
const ModuleName = "email"
// OutputConfig holds the configuration json fields and internal objects
type OutputConfig struct {
config.OutputConfig
Address string `json:"address"`
Attachments []string `json:"attachments"`
From string `json:"from"`
To string `json:"to"`
Cc string `json:"cc"`
Subject string `json:"subject"`
Port int `json:"port"`
UseTLS bool `json:"use_tls"`
UserName string `json:"username"`
Password string `json:"password"`
}
// DefaultOutputConfig returns an OutputConfig struct with default values
func DefaultOutputConfig() OutputConfig {
return OutputConfig{
OutputConfig: config.OutputConfig{
CommonConfig: config.CommonConfig{
Type: ModuleName,
},
},
Port: 25,
UseTLS: false,
Cc: "",
UserName: "",
Password: "",
Attachments: nil,
}
}
// InitHandler initialize the output plugin
func InitHandler(ctx context.Context, raw *config.ConfigRaw) (config.TypeOutputConfig, error) {
conf := DefaultOutputConfig()
if err := config.ReflectConfig(raw, &conf); err != nil {
return nil, err
}
return &conf, nil
}
// Output event
func (t *OutputConfig) Output(ctx context.Context, event logevent.LogEvent) (err error) {
message := gomail.NewMessage()
message.SetHeader("From", t.From)
message.SetHeader("To", strings.Split(t.To, ";")...)
if t.Cc != "" {
message.SetHeader("Cc", strings.Split(t.Cc, ";")...)
}
message.SetHeader("Subject", t.Subject)
if t.Attachments != nil && len(t.Attachments) > 0 {
for _, v := range t.Attachments {
message.Attach(v)
}
}
message.SetBody("text/html", event.GetString("message"))
dialer := gomail.NewDialer(t.Address, t.Port, t.UserName, t.Password)
if t.UseTLS == true {
dialer.TLSConfig = &tls.Config{InsecureSkipVerify: true}
}
err = dialer.DialAndSend(message)
return
}