forked from jstemmer/rehook
/
email_action.go
147 lines (124 loc) · 3.98 KB
/
email_action.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
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"text/template"
"github.com/boltdb/bolt"
)
func init() {
RegisterComponent("email-action", EmailAction{})
}
// EmailAction is a component that sends an email using the Mailgun API.
type EmailAction struct {
}
// Name returns the name of this component.
func (EmailAction) Name() string { return "Send email (using Mailgun)" }
// Template returns the HTML template name of this component.
func (EmailAction) Template() string { return "email-action" }
// Params returns the currently stored configuration parameters for hook h
// from bucket b.
func (EmailAction) Params(h Hook, b *bolt.Bucket) map[string]string {
m := make(map[string]string)
for _, k := range []string{"token", "domain", "address", "subject", "template"} {
m[k] = string(b.Get([]byte(fmt.Sprintf("%s-%s", h.ID, k))))
}
return m
}
// Init initializes this component. It requires a Mailgun token, a valid
// address and template parameter to be present.
func (EmailAction) Init(h Hook, params map[string]string, b *bolt.Bucket) error {
// TODO: refactor the following
token, ok := params["token"]
if !ok {
return errors.New("token is required")
}
domain, ok := params["domain"]
if !ok {
return errors.New("domain is required")
}
address, ok := params["address"]
if !ok {
return errors.New("address is required")
}
subject, ok := params["subject"]
if !ok {
return errors.New("subject is required")
}
tpl, ok := params["template"]
if !ok {
return errors.New("template is required")
}
if _, err := template.New("email").Parse(string(tpl)); err != nil {
return fmt.Errorf("invalid template: %s", err)
}
if err := b.Put([]byte(fmt.Sprintf("%s-token", h.ID)), []byte(token)); err != nil {
return err
}
if err := b.Put([]byte(fmt.Sprintf("%s-domain", h.ID)), []byte(domain)); err != nil {
return err
}
if err := b.Put([]byte(fmt.Sprintf("%s-address", h.ID)), []byte(address)); err != nil {
return err
}
if err := b.Put([]byte(fmt.Sprintf("%s-subject", h.ID)), []byte(subject)); err != nil {
return err
}
return b.Put([]byte(fmt.Sprintf("%s-template", h.ID)), []byte(tpl))
}
// Process sends an email to the configured address using the template as email
// body.
func (EmailAction) Process(h Hook, r Request, b *bolt.Bucket) error {
token := b.Get([]byte(fmt.Sprintf("%s-token", h.ID)))
domain := b.Get([]byte(fmt.Sprintf("%s-domain", h.ID)))
address := b.Get([]byte(fmt.Sprintf("%s-address", h.ID)))
subject := b.Get([]byte(fmt.Sprintf("%s-subject", h.ID)))
tpl := b.Get([]byte(fmt.Sprintf("%s-template", h.ID)))
if token == nil || domain == nil || address == nil || subject == nil || tpl == nil {
return errors.New("email action not initialized")
}
t, err := template.New("email").Parse(string(tpl))
if err != nil {
return fmt.Errorf("could not parse template: %s", err)
}
data := struct {
Hook Hook
Request Request
}{h, r}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
return fmt.Errorf("could not execute template: %s", err)
}
return sendMail(string(token), string(domain), string(address), string(subject), buf.String())
}
func sendMail(token, domain, address, subject, text string) error {
form := url.Values{}
form.Set("from", "mail@"+domain)
form.Set("to", address)
form.Set("subject", subject)
form.Set("text", text)
req, err := http.NewRequest("POST", fmt.Sprintf("https://api.mailgun.net/v2/%s/messages", domain), strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.SetBasicAuth("api", token)
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
if body, err := ioutil.ReadAll(resp.Body); err == nil {
log.Printf("Mailgun API error response: %s", body)
}
return fmt.Errorf("send mail unexpected status code received: %d", resp.StatusCode)
}
return nil
}