Skip to content

Commit 2fe2d7b

Browse files
committed
handlers/email: Ability to toggle sending emails or not
1 parent 91a5908 commit 2fe2d7b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

handlers/email/email.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Email struct {
4545
password string
4646
from string
4747
to []string
48+
send bool
4849
rw sync.RWMutex
4950
once sync.Once
5051
}
@@ -59,6 +60,7 @@ func New(host string, port int, username string, password string, from string, t
5960
password: password,
6061
from: from,
6162
to: to,
63+
send: true,
6264
formatFunc: defaultFormatFunc,
6365
}
6466
e.SetTemplate(defaultTemplate)
@@ -67,6 +69,9 @@ func New(host string, port int, username string, password string, from string, t
6769

6870
// SetTemplate sets Email's html template to be used for email body
6971
func (email *Email) SetTemplate(htmlTemplate string) {
72+
email.rw.Lock()
73+
defer email.rw.Unlock()
74+
7075
// parse email htmlTemplate, will panic if fails
7176
email.template = template.Must(template.New("email").Funcs(
7277
template.FuncMap{
@@ -96,12 +101,18 @@ func (email *Email) Template() *template.Template {
96101
// SetTimestampFormat sets Email's timestamp output format
97102
// Default is : "2006-01-02T15:04:05.000000000Z07:00"
98103
func (email *Email) SetTimestampFormat(format string) {
104+
email.rw.Lock()
105+
defer email.rw.Unlock()
106+
99107
email.timestampFormat = format
100108
}
101109

102110
// SetFormatFunc sets FormatFunc each worker will call to get
103111
// a Formatter func
104112
func (email *Email) SetFormatFunc(fn FormatFunc) {
113+
email.rw.Lock()
114+
defer email.rw.Unlock()
115+
105116
email.formatFunc = fn
106117
}
107118

@@ -119,6 +130,14 @@ func (email *Email) SetEmailConfig(host string, port int, username string, passw
119130
email.formatter = email.formatFunc(email)
120131
}
121132

133+
// SetSend enables or disables the email handler sending emails
134+
func (email *Email) SetSend(send bool) {
135+
email.rw.Lock()
136+
defer email.rw.Unlock()
137+
138+
email.send = send
139+
}
140+
122141
func defaultFormatFunc(email *Email) Formatter {
123142
b := new(bytes.Buffer)
124143

@@ -146,6 +165,9 @@ func defaultFormatFunc(email *Email) Formatter {
146165

147166
// Log handles the log entry
148167
func (email *Email) Log(e log.Entry) {
168+
if !email.send {
169+
return
170+
}
149171
email.once.Do(func() {
150172
email.formatter = email.formatFunc(email)
151173
})

handlers/email/email_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func TestEmailHandler(t *testing.T) {
104104
email.SetTimestampFormat("MST")
105105
email.SetTemplate(defaultTemplate)
106106
email.SetEmailConfig("localhost", 3041, "", "", "[email protected]", []string{"[email protected]"})
107+
email.SetSend(true)
107108
// email.SetFormatFunc(testFormatFunc)
108109
log.AddHandler(email, log.InfoLevel, log.DebugLevel)
109110

0 commit comments

Comments
 (0)