-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmail.go
More file actions
210 lines (165 loc) · 4.7 KB
/
Email.go
File metadata and controls
210 lines (165 loc) · 4.7 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package emails
import (
"crypto/tls"
"encoding/json"
"log"
"strings"
"github.com/go-bolo/bolo"
"github.com/go-bolo/msgbroker"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
gomail "gopkg.in/mail.v2"
)
type Email struct {
Identifier string `json:"identifier"`
Template *string `json:"template"`
To string `json:"to"`
From string `json:"from"`
ReplyTo string `json:"replyTo"`
CC string `json:"cc"`
CCO string `json:"cco"`
Subject string `json:"subject"`
Text string `json:"Text"`
HTML string `json:"HTML"`
DeliveryAttempts int `json:"deliveryAttempts"`
}
func (r *Email) ToJSON() []byte {
jsonString, _ := json.MarshalIndent(r, "", " ")
return jsonString
}
func (r *Email) Send() error {
app := bolo.GetApp()
cfgs := app.GetConfiguration()
m := gomail.NewMessage()
userName := cfgs.GetF("SMTP_USER", "")
password := cfgs.GetF("SMTP_PASSWORD", "")
from := cfgs.GetF("SMTP_FROM", "")
host := cfgs.GetF("SMTP_HOST", "")
port := cfgs.GetIntF("SMTP_PORT", 587)
enableEmail := cfgs.GetF("ENABLE_EMAIL_DELIVERY", "")
if userName == "" || password == "" || from == "" || host == "" || port == 0 {
log.Println("Email.Send Email delivery configuration not found", r.To, r.Subject)
return nil
}
if enableEmail == "" {
logrus.WithFields(logrus.Fields{
"template": r.Template,
"to": r.To,
"subject": r.Subject,
"text": r.Text,
}).Warn("Email.Send Email delivery disabled, skiping")
return nil
}
// Set E-Mail sender
m.SetHeader("From", from)
recipients := strings.Split(r.To, ",")
addresses := make([]string, len(recipients))
for i, recipient := range recipients {
addresses[i] = m.FormatAddress(recipient, "")
}
// Set E-Mail receivers
m.SetHeader("To", addresses...)
if r.ReplyTo != "" {
m.SetHeader("ReplyTo", r.ReplyTo)
}
// Set E-Mail subject
m.SetHeader("Subject", r.Subject)
if r.HTML != "" {
m.SetBody("text/html", r.HTML)
} else {
m.SetBody("text/html", r.Text)
}
d := gomail.NewDialer(host, port, userName, password)
env := app.GetConfiguration().GetF("GO_ENV", "development")
d.TLSConfig = &tls.Config{InsecureSkipVerify: env != "production"}
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
// TODO! add support to only log with a configuration
if err := d.DialAndSend(m); err != nil {
log.Println("Email.Send Error on send email", err)
return err
}
return nil
}
func (r *Email) QueueToSend() error {
app := bolo.GetApp()
cfgs := app.GetConfiguration()
enableEmail := cfgs.GetF("ENABLE_EMAIL_DELIVERY", "")
if enableEmail == "" {
logrus.WithFields(logrus.Fields{
"template": r.Template,
"to": r.To,
"subject": r.Subject,
"text": r.Text,
"html": r.HTML,
}).Info("QueueToSend: Skipping email delivery")
return nil
} else {
logrus.WithFields(logrus.Fields{
"template": r.Template,
"to": r.To,
"subject": r.Subject,
}).Debug("QueueToSend: Will send email to")
}
msgBI := app.GetPlugin("msg-broker")
switch msgBrokerPlugin := msgBI.(type) {
case *msgbroker.MSGBrokerPlugin:
c := msgBrokerPlugin.Client
if r.ReplyTo == "" {
r.ReplyTo = cfgs.GetF("SMTP_REPLY_TO", "Monitor do Mercado <monitordomercado@linkysystems.com>")
}
// publish in rabbit mq ...
return c.Publish("notification-email-delivery", r.ToJSON())
default:
return r.Send()
}
}
func (r *Email) Requeue() error {
r.DeliveryAttempts++
if r.DeliveryAttempts < 3 {
log.Println("notification.Email requeuing email", r.To, r.DeliveryAttempts, r.Subject)
return r.QueueToSend()
} else {
log.Println("notification.Email max requeue limit, skiping requeue", r.To, r.DeliveryAttempts, r.Subject)
}
return nil
}
type EmailOpts struct {
To string `json:"to"`
From string `json:"from"`
ReplyTo string `json:"replyTo"`
CC string `json:"cc"`
CCO string `json:"cco"`
TemplateName string
Variables TemplateVariables
}
func NewEmailWithTemplate(opts *EmailOpts) (*Email, error) {
e := Email{
To: opts.To,
From: opts.From,
ReplyTo: opts.ReplyTo,
CC: opts.CC,
CCO: opts.CCO,
}
if opts.TemplateName == "" {
return &e, nil
}
template := EmailTemplateModel{}
err := TemplateFindOneByType(opts.TemplateName, &template)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.Wrap(err, "Email.NewEmail error on find email template by type")
}
if template.ID == 0 {
return &e, nil
}
logrus.WithFields(logrus.Fields{
"type": template.Type,
"text": template.Text,
"html": template.Html,
}).Debug("NewEmailWithTemplate tempplate found")
err = template.Render(opts.Variables, &e)
if err != nil {
return nil, errors.Wrap(err, "Email.NewEmail error on render email template")
}
return &e, nil
}