@@ -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
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+ // 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+
122141func 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
148167func (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 })
0 commit comments