forked from corestoreio/pkg
/
daemon_mandrill.go
105 lines (89 loc) · 3.41 KB
/
daemon_mandrill.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
// Copyright 2015-2016, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package email
import (
"bytes"
"errors"
"io"
"net/http"
"net/mail"
"github.com/juju/errgo"
"github.com/mattbaird/gochimp"
)
// @todo Add AmazonSES, Mailgun, MailJet, SendGrid, PostMark, etc
// https://github.com/aws/aws-sdk-go/blob/master/service%2Fses%2Fexamples_test.go
// MandrillOptions can be used as an argument to SetMandrill() function.
type MandrillOptions func(*gochimp.MandrillAPI)
// SetMandrillRoundTripper sets a round tripper to the MandrillAPI mainly for testing.
func SetMandrillRoundTripper(transport http.RoundTripper) MandrillOptions {
return func(m *gochimp.MandrillAPI) {
m.Transport = transport
}
}
// SetMandrill sets the Mandrill API for sending emails. This function is not
// recursive and returns nil. @todo
func SetMandrill(opts ...MandrillOptions) DaemonOption {
return func(da *Daemon) DaemonOption {
// this whole func is just a quick write down. no idea if it's working
// and refactor ... 8-)
apiKey := da.Config.String(PathSmtpMandrillAPIKey)
if apiKey == "" {
da.lastErrs = append(da.lastErrs, errors.New("Mandrill API Key is empty."))
return nil
}
md, err := gochimp.NewMandrill(apiKey)
if err != nil {
da.lastErrs = append(da.lastErrs, err)
return nil
}
for _, o := range opts {
o(md)
}
da.sendFunc = func(from string, to []string, msg io.WriterTo) error {
// @todo figure out if "to" contains To, CC and BCC addresses.
addr, err := mail.ParseAddress(from)
if err != nil {
PkgLog.Info("mail.daemon.Mandrill.ParseAddress", "err", err, "from", from, "to", to)
return errgo.Mask(err)
}
r := gochimp.Recipient{
Name: addr.Name,
Email: addr.Address,
}
var buf bytes.Buffer
if _, err := msg.WriteTo(&buf); err != nil {
PkgLog.Info("mail.daemon.Mandrill.MessageWriteTo", "err", err, "from", from, "to", to, "msg", buf.String())
return errgo.Mask(err)
}
resp, err := md.MessageSendRaw(buf.String(), to, r, false)
if err != nil {
PkgLog.Info("mail.daemon.Mandrill.MessageSendRaw", "err", err, "from", from, "to", to, "msg", buf.String())
return errgo.Mask(err)
}
if PkgLog.IsDebug() {
PkgLog.Debug("mail.daemon.Mandrill.MessageSendRaw", "resp", resp, "from", from, "to", to, "msg", buf.String())
}
// The last arg in MessageSendRaw means async in the Mandrill API:
// Async: enable a background sending mode that is optimized for bulk sending.
// In async mode, messages/send will immediately return a status of "queued"
// for every recipient. To handle rejections when sending in async mode, set
// up a webhook for the 'reject' event. Defaults to false for messages with
// no more than 10 recipients; messages with more than 10 recipients are
// always sent asynchronously, regardless of the value of async.
return nil
}
da.dialer = nil
return nil
}
}