forked from riobard/go-mailgun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.go
68 lines (62 loc) · 1.23 KB
/
send.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
package mailgun
import (
"encoding/json"
"fmt"
"net/url"
"regexp"
)
type Mail interface {
From() string
To() []string
Cc() []string
Bcc() []string
Subject() string
Html() string
Text() string
Headers() map[string]string
Options() map[string]string
Variables() map[string]string
}
var EMAIL_DOMAIN_RE = regexp.MustCompile(`[^<>]+<?.+@([^<>]+)>?`)
func (c *Client) Send(m Mail) (msgId string, err error) {
match := EMAIL_DOMAIN_RE.FindStringSubmatch(m.From())
if len(match) != 2 {
err = fmt.Errorf("invalid From address: %s", m.From())
return
}
domain := match[1]
v := url.Values{}
v.Set("from", m.From())
for _, to := range m.To() {
v.Add("to", to)
}
for _, cc := range m.Cc() {
v.Add("cc", cc)
}
for _, bcc := range m.Bcc() {
v.Add("bcc", bcc)
}
v.Set("subject", m.Subject())
v.Set("html", m.Html())
v.Set("text", m.Text())
for k, e := range m.Headers() {
v.Add("h:"+k, e)
}
for k, e := range m.Options() {
v.Add("o:"+k, e)
}
for k, e := range m.Variables() {
v.Add("v:"+k, e)
}
rsp, err := c.api("POST", "/"+domain+"/messages", v)
if err != nil {
return
}
var res struct {
Message string `json:"message"`
Id string `json:"id"`
}
err = json.Unmarshal(rsp, &res)
msgId = res.Id
return
}