-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverutils.go
47 lines (40 loc) · 1.1 KB
/
serverutils.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
package main
import (
"sync"
log "github.com/Sirupsen/logrus"
emailLib "github.com/jordan-wright/email"
)
func SendBulkEmail(emailAccounts []*EmailAccount, sendBulkEmailReq *SendBulkEmailRequest, emailPool *emailLib.Pool) (failedIds []string) {
failedIdsChan := make(chan string)
var total int
wg := new(sync.WaitGroup)
for i, email := range emailAccounts {
if sendBulkEmailReq.SecureOnly && !email.HasPubKey() {
failedIds = append(failedIds, email.Id)
continue
}
total++
wg.Add(1)
go func(i int, email *EmailAccount) {
var maybeFailedId string
log.Debugf("Sending bulk email #%v", i+1)
err := email.Send(sendBulkEmailReq.EmailData, emailPool)
if err != nil {
log.Errorf("Error sending (instance of bulk) email: %v", err)
maybeFailedId = email.Id
}
wg.Done()
failedIdsChan <- maybeFailedId
}(i, email)
}
log.Debugf("SendBulkEmailRequest: waiting for %v email(s) to send", total)
wg.Wait()
var failedId string
for i := 0; i < total; i++ {
failedId = <-failedIdsChan
if failedId != "" {
failedIds = append(failedIds, failedId)
}
}
return failedIds
}