77 "fmt"
88 "net/mail"
99 "strings"
10+ "time"
1011
1112 jsoniter "github.com/json-iterator/go"
1213 "github.com/mailgun/mailgun-go/v5"
@@ -146,6 +147,7 @@ func (m *Mailgun) UnmarshalPosthook(body []byte) ([]mmailer.Posthook, error) {
146147 h .Event = mmailer .EventDelivered
147148 h .MessageId = e .Message .Headers .MessageID
148149 h .Email = e .Recipient
150+ h .Info = infoString (false , "" , "" , e .DeliveryStatus )
149151
150152 case * events.Opened :
151153 h .Event = mmailer .EventOpen
@@ -156,17 +158,16 @@ func (m *Mailgun) UnmarshalPosthook(body []byte) ([]mmailer.Posthook, error) {
156158 switch e .Severity {
157159 case "permanent" :
158160 h .Event = mmailer .EventBounce
159-
160- case "temporary" :
161- h .Event = mmailer .EventDeferred
162161 if e .Reason == "suppress-bounce" {
163162 h .Event = mmailer .EventDropped
164163 }
164+ case "temporary" :
165+ h .Event = mmailer .EventDeferred
165166 }
166-
167167 h .MessageId = e .Message .Headers .MessageID
168168 h .Email = e .Recipient
169- h .Info = fmt .Sprintf ("%s; %d; %s" , e .Reason , e .DeliveryStatus .Code , e .DeliveryStatus .Description )
169+ h .Info = infoString (true , e .Reason , e .Severity , e .DeliveryStatus )
170+
170171 default :
171172 logger .Warn (fmt .Sprintf ("received unsupported webhook event: %s" , h .Event ))
172173 return nil , nil
@@ -175,12 +176,62 @@ func (m *Mailgun) UnmarshalPosthook(body []byte) ([]mmailer.Posthook, error) {
175176 return []mmailer.Posthook {h }, nil
176177}
177178
179+ func infoString (fail bool , reason , severity string , st events.DeliveryStatus ) string {
180+ latency := time .Duration (st .SessionSeconds * float64 (time .Second )).Truncate (time .Millisecond )
181+
182+ var words []string
183+ if st .Code != 0 {
184+ words = append (words , fmt .Sprintf ("%d" , st .Code ))
185+ }
186+ if st .EnhancedCode != "" {
187+ words = append (words , st .EnhancedCode )
188+ }
189+ if ! fail {
190+ words = append (words , st .Message )
191+ }
192+ if st .MxHost != "" {
193+ words = append (words , st .MxHost )
194+ }
195+ if latency > time .Millisecond {
196+ words = append (words , latency .String ())
197+ }
198+ if reason != "" {
199+ words = append (words , reason )
200+ }
201+ if severity != "" {
202+ words = append (words , severity )
203+ }
204+
205+ var flags []string
206+ if st .AttemptNo > 0 {
207+ flags = append (flags , fmt .Sprintf ("attempt:%d" , st .AttemptNo ))
208+ }
209+ if st .Utf8 != nil && * st .Utf8 {
210+ flags = append (flags , "utf8" )
211+ }
212+ if st .TLS != nil && * st .TLS {
213+ flags = append (flags , "tls" )
214+ }
215+ if st .CertificateVerified != nil && * st .CertificateVerified {
216+ flags = append (flags , "certificate-verified" )
217+ }
218+ if len (flags ) > 0 {
219+ words = append (words , fmt .Sprintf ("[%s]" , strings .Join (flags , ", " )))
220+ }
221+
222+ msg := strings .Join (words , " " )
223+ if fail {
224+ msg += ": " + st .Message
225+ }
226+ return msg
227+ }
228+
178229type configurer struct {}
179230
180231func (s configurer ) SetIpPool (poolId string , message * mailgun.PlainMessage ) {
181232 // TODO?
182233}
183234
184235func (s configurer ) DisableTracking (message * mailgun.PlainMessage ) {
185- message .SetTracking (false )
236+ message .SetTracking (false ) // untested
186237}
0 commit comments