@@ -45,6 +45,7 @@ type Email struct {
4545 password string
4646 from string
4747 to []string
48+ rw sync.RWMutex
4849 once sync.Once
4950}
5051
@@ -104,33 +105,16 @@ func (email *Email) SetFormatFunc(fn FormatFunc) {
104105 email .formatFunc = fn
105106}
106107
107- // SetHost sets Email's host
108- func (email * Email ) SetHost (host string ) {
109- email .host = host
110- }
108+ // SetEmailConfig allows updating of the email config in flight and is thread safe.
109+ func (email * Email ) SetEmailConfig (host string , port int , username string , password string , from string , to [] string ) {
110+ email .rw . Lock ()
111+ defer email . rw . Unlock ()
111112
112- // SetPort sets Email's port
113- func (email * Email ) SetPort (port int ) {
113+ email .host = host
114114 email .port = port
115- }
116-
117- // SetUsername sets Email's username
118- func (email * Email ) SetUsername (username string ) {
119115 email .username = username
120- }
121-
122- // SetPassword sets Email's password
123- func (email * Email ) SetPassword (password string ) {
124116 email .password = password
125- }
126-
127- // SetFrom sets Email's email address to send from
128- func (email * Email ) SetFrom (from string ) {
129117 email .from = from
130- }
131-
132- // SetTo sets Email's email address(es) to send to
133- func (email * Email ) SetTo (to []string ) {
134118 email .to = to
135119}
136120
@@ -144,15 +128,15 @@ func defaultFormatFunc(email *Email) Formatter {
144128 to := make ([]string , len (email .to ))
145129 copy (to , email .to )
146130
147- template := email .Template ()
131+ tmpl := email .Template ()
148132 message := gomail .NewMessage ()
149133
150134 message .SetHeader ("From" , email .from )
151135 message .SetHeader ("To" , to ... )
152136
153137 return func (e log.Entry ) * gomail.Message {
154138 b .Reset ()
155- _ = template .ExecuteTemplate (b , "email" , e )
139+ _ = tmpl .ExecuteTemplate (b , "email" , e )
156140 message .SetHeader ("Subject" , e .Message )
157141 message .SetBody (contentType , b .String ())
158142 return message
@@ -171,7 +155,9 @@ func (email *Email) Log(e log.Entry) {
171155 var message * gomail.Message
172156 var count uint8
173157
158+ email .rw .RLock ()
174159 d := gomail .NewDialer (email .host , email .port , email .username , email .password )
160+ email .rw .RUnlock ()
175161
176162 for {
177163 count = 0
0 commit comments