This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mail.go
159 lines (139 loc) · 3.54 KB
/
mail.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
package logrus_mail
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/mail"
"net/smtp"
"strconv"
"time"
"github.com/sirupsen/logrus"
)
const (
format = "20060102 15:04:05"
)
// MailHook to sends logs by email without authentication.
type MailHook struct {
AppName string
c *smtp.Client
}
// MailAuthHook to sends logs by email with authentication.
type MailAuthHook struct {
AppName string
Host string
Port int
From *mail.Address
To *mail.Address
Username string
Password string
}
// NewMailHook creates a hook to be added to an instance of logger.
func NewMailHook(appname string, host string, port int, from string, to string) (*MailHook, error) {
// Connect to the remote SMTP server.
c, err := smtp.Dial(host + ":" + strconv.Itoa(port))
if err != nil {
return nil, err
}
// Validate sender and recipient
sender, err := mail.ParseAddress(from)
if err != nil {
return nil, err
}
recipient, err := mail.ParseAddress(to)
if err != nil {
return nil, err
}
// Set the sender and recipient.
if err := c.Mail(sender.Address); err != nil {
return nil, err
}
if err := c.Rcpt(recipient.Address); err != nil {
return nil, err
}
return &MailHook{
AppName: appname,
c: c,
}, nil
}
// NewMailAuthHook creates a hook to be added to an instance of logger.
func NewMailAuthHook(appname string, host string, port int, from string, to string, username string, password string) (*MailAuthHook, error) {
// Check if server listens on that port.
conn, err := net.DialTimeout("tcp", host+":"+strconv.Itoa(port), 3*time.Second)
if err != nil {
return nil, err
}
defer conn.Close()
// Validate sender and recipient
sender, err := mail.ParseAddress(from)
if err != nil {
return nil, err
}
receiver, err := mail.ParseAddress(to)
if err != nil {
return nil, err
}
return &MailAuthHook{
AppName: appname,
Host: host,
Port: port,
From: sender,
To: receiver,
Username: username,
Password: password}, nil
}
// Fire is called when a log event is fired.
func (hook *MailHook) Fire(entry *logrus.Entry) error {
wc, err := hook.c.Data()
if err != nil {
return err
}
defer wc.Close()
message := createMessage(entry, hook.AppName)
if _, err = message.WriteTo(wc); err != nil {
return err
}
return nil
}
// Fire is called when a log event is fired.
func (hook *MailAuthHook) Fire(entry *logrus.Entry) error {
auth := smtp.PlainAuth("", hook.Username, hook.Password, hook.Host)
message := createMessage(entry, hook.AppName)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
hook.Host+":"+strconv.Itoa(hook.Port),
auth,
hook.From.Address,
[]string{hook.To.Address},
message.Bytes(),
)
if err != nil {
return err
}
return nil
}
// Levels returns the available logging levels.
func (hook *MailAuthHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
}
}
// Levels returns the available logging levels.
func (hook *MailHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
}
}
func createMessage(entry *logrus.Entry, appname string) *bytes.Buffer {
body := entry.Time.Format(format) + " - " + entry.Message
subject := appname + " - " + entry.Level.String()
fields, _ := json.MarshalIndent(entry.Data, "", "\t")
contents := fmt.Sprintf("Subject: %s\r\n\r\n%s\r\n\r\n%s", subject, body, fields)
message := bytes.NewBufferString(contents)
return message
}