/
service.go
108 lines (91 loc) · 2.66 KB
/
service.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
package emailer
import (
"github.com/cozy/cozy-stack/model/instance"
"github.com/cozy/cozy-stack/model/job"
"github.com/cozy/cozy-stack/pkg/mail"
)
// EmailerService allows to send emails.
//
// This broker send the emails via anrasynchronous job.
type EmailerService struct {
jobBroker job.Broker
}
// NewEmailerService instantiates an [EmailerService].
func NewEmailerService(jobBroker job.Broker) *EmailerService {
return &EmailerService{jobBroker}
}
// TransactionalEmailCmd contains the information to send a transactional email
// to the instance owner.
type TransactionalEmailCmd struct {
TemplateName string
TemplateValues map[string]interface{}
}
// SendEmail sends a mail to the instance owner.
func (s *EmailerService) SendEmail(inst *instance.Instance, cmd *TransactionalEmailCmd) error {
if cmd.TemplateName == "" || cmd.TemplateValues == nil {
return ErrMissingTemplate
}
msg, err := job.NewMessage(map[string]interface{}{
"mode": "noreply",
"template_name": cmd.TemplateName,
"template_values": cmd.TemplateValues,
})
if err != nil {
return err
}
_, err = s.jobBroker.PushJob(inst, &job.JobRequest{
WorkerType: "sendmail",
Message: msg,
})
return err
}
// SendPendingEmail sends a mail to the instance owner on their new pending
// email address. It is used to confirm that they can receive emails on the new
// email address.
func (s *EmailerService) SendPendingEmail(inst *instance.Instance, cmd *TransactionalEmailCmd) error {
if cmd.TemplateName == "" || cmd.TemplateValues == nil {
return ErrMissingTemplate
}
msg, err := job.NewMessage(map[string]interface{}{
"mode": "pending",
"template_name": cmd.TemplateName,
"template_values": cmd.TemplateValues,
})
if err != nil {
return err
}
_, err = s.jobBroker.PushJob(inst, &job.JobRequest{
WorkerType: "sendmail",
Message: msg,
})
return err
}
// CampaignEmailCmd contains the information required to send a campaign email
// to the instance owner.
type CampaignEmailCmd struct {
Parts []mail.Part
Subject string
}
// SendCampaignEmail sends a campaign email to the instance owner with the
// given cmd content via the dedicated campaign mail server.
func (s *EmailerService) SendCampaignEmail(inst *instance.Instance, cmd *CampaignEmailCmd) error {
if cmd.Subject == "" {
return ErrMissingSubject
}
if cmd.Parts == nil {
return ErrMissingContent
}
msg, err := job.NewMessage(map[string]interface{}{
"mode": mail.ModeCampaign,
"subject": cmd.Subject,
"parts": cmd.Parts,
})
if err != nil {
return err
}
_, err = s.jobBroker.PushJob(inst, &job.JobRequest{
WorkerType: "sendmail",
Message: msg,
})
return err
}