-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.go
152 lines (114 loc) · 3.23 KB
/
mailer.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package makeless_go_mailer_mailgun
import (
"context"
"encoding/json"
"github.com/mailgun/mailgun-go/v4"
"github.com/makeless/makeless-go/mailer"
"github.com/makeless/makeless-go/queue"
"github.com/makeless/makeless-go/queue/basic"
"sync"
)
type Mailer struct {
Handlers map[string]func(data map[string]interface{}, locale string) (makeless_go_mailer.Mail, error)
Queue makeless_go_queue.Queue
Mailgun *mailgun.MailgunImpl
ApiBase string
Domain string
ApiKey string
*sync.RWMutex
}
func (mailer *Mailer) GetHandlers() map[string]func(data map[string]interface{}, locale string) (makeless_go_mailer.Mail, error) {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Handlers
}
func (mailer *Mailer) GetHandler(name string) (func(data map[string]interface{}, locale string) (makeless_go_mailer.Mail, error), error) {
mailer.RLock()
defer mailer.RUnlock()
handler, exists := mailer.Handlers[name]
if !exists {
return nil, makeless_go_mailer.MailNotExistsErr
}
return handler, nil
}
func (mailer *Mailer) SetHandler(name string, handler func(data map[string]interface{}, locale string) (makeless_go_mailer.Mail, error)) {
mailer.Lock()
defer mailer.Unlock()
mailer.Handlers[name] = handler
}
func (mailer *Mailer) GetQueue() makeless_go_queue.Queue {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Queue
}
func (mailer *Mailer) GetMail(name string, data map[string]interface{}, locale string) (makeless_go_mailer.Mail, error) {
handler, err := mailer.GetHandler(name)
if err != nil {
return nil, err
}
return handler(data, locale)
}
func (mailer *Mailer) GetMailgun() *mailgun.MailgunImpl {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Mailgun
}
func (mailer *Mailer) SetMailgun(mailgun *mailgun.MailgunImpl) {
mailer.Lock()
defer mailer.Unlock()
mailer.Mailgun = mailgun
}
func (mailer *Mailer) GetApiBase() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.ApiBase
}
func (mailer *Mailer) GetDomain() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.Domain
}
func (mailer *Mailer) GetApiKey() string {
mailer.RLock()
defer mailer.RUnlock()
return mailer.ApiKey
}
func (mailer *Mailer) Init() error {
mg := mailgun.NewMailgun(mailer.GetDomain(), mailer.GetApiKey())
mg.SetAPIBase(mailer.GetApiBase())
mailer.SetMailgun(mg)
return nil
}
func (mailer *Mailer) Send(ctx context.Context, mail makeless_go_mailer.Mail) error {
var message = mailer.GetMailgun().NewMessage(
mail.GetFrom(),
mail.GetSubject(),
string(mail.GetMessage()),
mail.GetTo()...,
)
message.SetHtml(string(mail.GetHtmlMessage()))
for _, cc := range mail.GetCc() {
message.AddCC(cc)
}
for _, bcc := range mail.GetBcc() {
message.AddCC(bcc)
}
for key := range mail.GetHeaders() {
message.AddHeader(key, mail.GetHeaders().Get(key))
}
for _, attachment := range mail.GetAttachments() {
message.AddBufferAttachment(attachment.GetFilename(), attachment.GetData())
}
_, _, err := mailer.GetMailgun().Send(ctx, message)
return err
}
func (mailer *Mailer) SendQueue(mail makeless_go_mailer.Mail) error {
bytes, err := json.Marshal(mail)
if err != nil {
return err
}
return mailer.GetQueue().Add(&makeless_go_queue_basic.Node{
Data: bytes,
RWMutex: new(sync.RWMutex),
})
}