-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail-custom.go
67 lines (53 loc) · 1.75 KB
/
email-custom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package carrier
import (
"log"
"net/smtp"
"sync"
"github.com/gammazero/workerpool"
"github.com/hashicorp/go-multierror"
)
// CustomMailClient manage all Gomail client actions
type CustomMailClient struct {
client smtp.Auth
config *CustomMailConfigModel
}
var (
// customMailClientSessionMapping singleton pattern
customMailClientSessionMapping = make(map[string]*CustomMailClient)
// customMailClientMutex mutex for this service only
customMailClientMutex sync.Mutex
)
// NewCustomMailClient init new instance
func NewCustomMailClient(config *CustomMailConfigModel) IMail {
configHashed := hashObject(config)
currentCustomClientSession := customMailClientSessionMapping[configHashed]
if currentCustomClientSession == nil {
currentCustomClientSession = &CustomMailClient{nil, nil}
currentCustomClientSession.client = smtp.PlainAuth(config.Identity, config.Username, config.Password, config.Host)
currentCustomClientSession.config = config
customMailClientSessionMapping[configHashed] = currentCustomClientSession
log.Println("Custom mail client: Initialization")
}
return currentCustomClientSession
}
// Send message to recipient
func (cc *CustomMailClient) Send(subject, body string, recipients ...string) error {
var errs *multierror.Error
wp := workerpool.New(cc.config.PoolSize)
for _, recipient := range recipients {
recipient := recipient
wp.Submit(func() {
if err := smtp.SendMail(cc.config.Host+":"+cc.config.Port, cc.client, cc.config.Username, []string{recipient}, []byte(body)); err != nil {
customMailClientMutex.Lock()
errs = multierror.Append(errs, err)
customMailClientMutex.Unlock()
}
})
}
wp.StopWait()
// Return an error if any failed
if err := errs.ErrorOrNil(); err != nil {
return err
}
return nil
}