-
Notifications
You must be signed in to change notification settings - Fork 968
/
Copy pathcourier.go
188 lines (164 loc) · 4.43 KB
/
courier.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
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
package courier
import (
"context"
"crypto/tls"
"fmt"
"strconv"
"time"
"github.com/cenkalti/backoff"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/ory/herodot"
gomail "github.com/ory/mail/v3"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/x"
)
type (
smtpDependencies interface {
PersistenceProvider
x.LoggingProvider
}
Courier struct {
Dialer *gomail.Dialer
d smtpDependencies
c configuration.Provider
// graceful shutdown handling
ctx context.Context
shutdown context.CancelFunc
}
Provider interface {
Courier() *Courier
}
)
func NewSMTP(d smtpDependencies, c configuration.Provider) *Courier {
uri := c.CourierSMTPURL()
password, _ := uri.User.Password()
port, _ := strconv.ParseInt(uri.Port(), 10, 64)
ctx, cancel := context.WithCancel(context.Background())
var ssl bool
var tlsConfig *tls.Config
if uri.Scheme == "smtps" {
ssl = true
sslSkipVerify, _ := strconv.ParseBool(uri.Query().Get("skip_ssl_verify"))
// #nosec G402 This is ok (and required!) because it is configurable and disabled by default.
tlsConfig = &tls.Config{InsecureSkipVerify: sslSkipVerify, ServerName: uri.Hostname()}
}
return &Courier{
d: d,
c: c,
ctx: ctx,
shutdown: cancel,
Dialer: &gomail.Dialer{
/* #nosec we need to support SMTP servers without TLS */
TLSConfig: tlsConfig,
Host: uri.Hostname(),
Port: int(port),
Username: uri.User.Username(),
Password: password,
SSL: ssl,
Timeout: time.Second * 10,
RetryFailure: true,
},
}
}
func (m *Courier) QueueEmail(ctx context.Context, t EmailTemplate) (uuid.UUID, error) {
body, err := t.EmailBody()
if err != nil {
return uuid.Nil, err
}
subject, err := t.EmailSubject()
if err != nil {
return uuid.Nil, err
}
recipient, err := t.EmailRecipient()
if err != nil {
return uuid.Nil, err
}
message := &Message{
Status: MessageStatusQueued,
Type: MessageTypeEmail,
Body: body,
Subject: subject,
Recipient: recipient,
}
if err := m.d.CourierPersister().AddMessage(ctx, message); err != nil {
return uuid.Nil, err
}
return message.ID, nil
}
func (m *Courier) Work() error {
errChan := make(chan error)
defer close(errChan)
go m.watchMessages(m.ctx, errChan)
select {
case <-m.ctx.Done():
if errors.Is(m.ctx.Err(), context.Canceled) {
return nil
}
return m.ctx.Err()
case err := <-errChan:
return err
}
}
func (m *Courier) Shutdown(ctx context.Context) error {
m.shutdown()
return nil
}
func (m *Courier) watchMessages(ctx context.Context, errChan chan error) {
for {
if err := backoff.Retry(func() error {
if len(m.Dialer.Host) == 0 {
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Courier tried to deliver an email but courier.smtp_url is not set!"))
}
messages, err := m.d.CourierPersister().NextMessages(ctx, 10)
if err != nil {
if errors.Is(err, ErrQueueEmpty) {
return nil
}
return err
}
for k := range messages {
var msg = messages[k]
switch msg.Type {
case MessageTypeEmail:
from := m.c.CourierSMTPFrom()
gm := gomail.NewMessage()
gm.SetHeader("From", from)
gm.SetHeader("To", msg.Recipient)
gm.SetHeader("Subject", msg.Subject)
gm.SetBody("text/plain", msg.Body)
gm.AddAlternative("text/html", msg.Body)
if err := m.Dialer.DialAndSend(ctx, gm); err != nil {
m.d.Logger().
WithError(err).
WithField("smtp_server", fmt.Sprintf("%s:%d", m.Dialer.Host, m.Dialer.Port)).
WithField("smtp_ssl_enabled", m.Dialer.SSL).
// WithField("email_to", msg.Recipient).
WithField("message_from", from).
Error("Unable to send email using SMTP connection.")
continue
}
if err := m.d.CourierPersister().SetMessageStatus(ctx, msg.ID, MessageStatusSent); err != nil {
m.d.Logger().
WithError(err).
WithField("message_id", msg.ID).
Error(`Unable to set the message status to "sent".`)
return err
}
m.d.Logger().
WithField("message_id", msg.ID).
WithField("message_type", msg.Type).
WithField("message_subject", msg.Subject).
Debug("Courier sent out message.")
default:
return errors.Errorf("received unexpected message type: %d", msg.Type)
}
}
return nil
}, backoff.NewExponentialBackOff()); err != nil {
errChan <- err
return
}
time.Sleep(time.Second)
}
}