-
Notifications
You must be signed in to change notification settings - Fork 13
/
sender.go
152 lines (115 loc) · 3.73 KB
/
sender.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 mail
import (
"encoding/json"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
errorlogger "github.com/coretrix/hitrix/service/component/error_logger"
)
type ISender interface {
GetTemplateKeyFromConfig(templateName string) (string, error)
SendTemplate(ormService *beeorm.Engine, message *Message) error
SendTemplateWithAttachments(ormService *beeorm.Engine, message *MessageAttachment) error
GetTemplateHTMLCode(templateName string) (string, error)
}
type Message struct {
From string
FromName string
ReplyTo string
To string
Subject string
TemplateName string
TemplateData interface{}
}
type Attachment struct {
ContentType string
Filename string
Base64Content string
}
type MessageAttachment struct {
Message
Attachments []Attachment
}
type Sender struct {
ConfigService config.IConfig
ClockService clock.IClock
ErrorLoggerService errorlogger.ErrorLogger
Provider IProvider
}
func (s *Sender) GetTemplateKeyFromConfig(templateName string) (string, error) {
return s.Provider.GetTemplateKeyFromConfig(s.ConfigService, templateName)
}
func (s *Sender) SendTemplate(ormService *beeorm.Engine, message *Message) error {
if message.From == "" {
message.From = s.Provider.GetDefaultFromEmail()
}
if message.FromName == "" {
message.FromName = s.Provider.GetDefaultFromName()
}
mailTrackerEntity, err := s.createTrackingEntity(ormService, message)
if err != nil {
s.ErrorLoggerService.LogError(err)
return err
}
err = s.Provider.SendTemplate(message)
if err != nil {
mailTrackerEntity.SenderError = err.Error()
mailTrackerEntity.Status = entity.MailTrackerStatusError
ormService.Flush(mailTrackerEntity)
s.ErrorLoggerService.LogError(err)
return err
}
mailTrackerEntity.Status = entity.MailTrackerStatusSuccess
ormService.Flush(mailTrackerEntity)
return nil
}
func (s *Sender) SendTemplateWithAttachments(ormService *beeorm.Engine, message *MessageAttachment) error {
mailTrackerEntity, err := s.createTrackingEntity(ormService, &Message{
From: message.From,
FromName: message.FromName,
ReplyTo: message.ReplyTo,
To: message.To,
Subject: message.Subject,
TemplateName: message.TemplateName,
TemplateData: message.TemplateData,
})
if err != nil {
s.ErrorLoggerService.LogError(err)
return err
}
err = s.Provider.SendTemplateWithAttachments(message)
if err != nil {
mailTrackerEntity.SenderError = err.Error()
mailTrackerEntity.Status = entity.MailTrackerStatusError
ormService.Flush(mailTrackerEntity)
s.ErrorLoggerService.LogError(err)
return err
}
mailTrackerEntity.Status = entity.MailTrackerStatusSuccess
ormService.Flush(mailTrackerEntity)
return nil
}
func (s *Sender) GetTemplateHTMLCode(templateName string) (string, error) {
return s.Provider.GetTemplateHTMLCode(templateName)
}
func (s *Sender) createTrackingEntity(ormService *beeorm.Engine, message *Message) (*entity.MailTrackerEntity, error) {
mailTrackerEntity := &entity.MailTrackerEntity{
Status: entity.MailTrackerStatusNew,
From: message.From,
To: message.To,
Subject: message.Subject,
TemplateFile: message.TemplateName,
CreatedAt: s.ClockService.Now(),
}
templateDataAsByte, err := json.Marshal(message.TemplateData)
if err != nil {
mailTrackerEntity.SenderError = err.Error()
mailTrackerEntity.Status = entity.MailTrackerStatusError
ormService.Flush(mailTrackerEntity)
s.ErrorLoggerService.LogError(err)
return mailTrackerEntity, err
}
mailTrackerEntity.TemplateData = string(templateDataAsByte)
return mailTrackerEntity, nil
}