-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.go
69 lines (52 loc) · 1.6 KB
/
extension.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
package mandrill
import (
"github.com/keighl/mandrill"
"github.com/markdicksonjr/nibbler"
"github.com/markdicksonjr/nibbler/mail/outbound"
"github.com/pkg/errors"
"log"
)
type Extension struct {
nibbler.NoOpExtension
outbound.Sender
initialized bool
Key string
Client *mandrill.Client
}
func (s *Extension) Init(app *nibbler.Application) error {
if len(s.Key) == 0 {
s.Key = (*app.GetConfiguration().Raw).Get("mandrill", "api", "key").String("")
}
if len(s.Key) == 0 {
return errors.New("mandrill.api.key must be defined for nibbler mandrill extension")
}
err := s.Connect()
s.initialized = true
return err
}
func (s *Extension) Connect() error {
s.Client = mandrill.ClientWithKey(s.Key)
return nil
}
func (s *Extension) SendMail(from *outbound.Email, subject string, to []*outbound.Email, plainTextContent string, htmlContent string) (*outbound.Response, error) {
if !s.initialized {
return nil, errors.New("send grid extension used for sending without initialization")
}
if from == nil || len((*from).Address) == 0 {
return nil, errors.New("send grid extension requires 'from' field")
}
if len(to) == 0 || to[0] == nil || len((*to[0]).Address) == 0 {
return nil, errors.New("send grid extension requires at least one recipient")
}
message := &mandrill.Message{}
message.AddRecipient((*to[0]).Address, (*to[0]).Name, "to")
message.FromEmail = (*from).Address
message.FromName = (*from).Name
message.Subject = subject
message.HTML = htmlContent
message.Text = plainTextContent
if _, err := s.Client.MessagesSend(message); err != nil {
log.Fatal(err.Error())
}
return nil, nil
}