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
package rest
import (
"context"
"github.com/crusttech/crust/compose/internal/service"
"github.com/crusttech/crust/compose/rest/request"
"github.com/crusttech/crust/internal/mail"
"github.com/pkg/errors"
)
var _ = errors.Wrap
type (
Notification struct {
notification service.NotificationService
}
contentPayload struct {
Plain string `json:"plain"`
Html string `json:"html"`
}
)
func (Notification) New() *Notification {
return &Notification{
notification: service.DefaultNotification,
}
}
// EmailSend assembles Email Message and pushes message to notification service
func (ctrl *Notification) EmailSend(ctx context.Context, r *request.NotificationEmailSend) (interface{}, error) {
ntf := ctrl.notification.With(ctx)
msg := mail.New()
if err := ntf.AttachEmailRecipients(msg, "To", r.To...); err != nil {
return false, err
}
if err := ntf.AttachEmailRecipients(msg, "Cc", r.Cc...); err != nil {
return false, err
}
var cp = contentPayload{}
if err := r.Content.Unmarshal(&cp); err != nil {
return false, errors.Wrap(err, "Could not unmarshal content")
} else {
if len(cp.Html) > 0 {
msg.SetBody("text/html", cp.Html)
}
if len(cp.Plain) > 0 {
msg.SetBody("text/plain", cp.Plain)
}
}
msg.SetHeader("Subject", r.Subject)
if err := ctrl.notification.With(ctx).SendEmail(msg); err != nil {
return false, err
} else {
return true, nil
}
}