@@ -35,6 +35,7 @@ const (
3535
3636// Email is an instance of the email logger
3737type Email struct {
38+ enabled bool
3839 formatter Formatter
3940 formatFunc FormatFunc
4041 timestampFormat string
@@ -52,6 +53,7 @@ type Email struct {
5253// New returns a new instance of the email logger
5354func New (host string , port int , username string , password string , from string , to []string ) * Email {
5455 e := & Email {
56+ enabled : true ,
5557 timestampFormat : log .DefaultTimeFormat ,
5658 host : host ,
5759 port : port ,
@@ -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
6971func (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"
98103func (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
104112func (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+ // SetEnabled enables or disables the email handler sending emails
134+ func (email * Email ) SetEnabled (enabled bool ) {
135+ email .rw .Lock ()
136+ defer email .rw .Unlock ()
137+
138+ email .enabled = enabled
139+ }
140+
122141func defaultFormatFunc (email * Email ) Formatter {
123142 b := new (bytes.Buffer )
124143
@@ -146,20 +165,28 @@ func defaultFormatFunc(email *Email) Formatter {
146165
147166// Log handles the log entry
148167func (email * Email ) Log (e log.Entry ) {
168+ email .rw .RLock ()
169+
170+ if ! email .enabled {
171+ email .rw .RUnlock ()
172+ return
173+ }
174+
149175 email .once .Do (func () {
150176 email .formatter = email .formatFunc (email )
151177 })
178+
179+ d := gomail .NewDialer (email .host , email .port , email .username , email .password )
180+
181+ email .rw .RUnlock ()
182+
152183 var s gomail.SendCloser
153184 var err error
154185 var open bool
155186 var alreadyTriedSending bool
156187 var message * gomail.Message
157188 var count uint8
158189
159- email .rw .RLock ()
160- d := gomail .NewDialer (email .host , email .port , email .username , email .password )
161- email .rw .RUnlock ()
162-
163190 for {
164191 count = 0
165192 alreadyTriedSending = false
0 commit comments