-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathses.go
164 lines (129 loc) · 4.09 KB
/
ses.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ses
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// NewAPI constructor for SES api
func NewAPI(endpoint, from, accessKeyID, secretAccessKey string) *Option {
op := new(Option)
op.endpoint = endpoint
op.source = from
op.accessKeyID = accessKeyID
op.secretAccessKey = []byte(secretAccessKey)
return op
}
// NewHTTPClientAPI constructor for SES api with given HTTP client
func NewHTTPClientAPI(client http.Client, endpoint, from, accessKeyID, secretAccessKey string) *Option {
SetDefaultHTTPClient(client)
return NewAPI(endpoint, from, accessKeyID, secretAccessKey)
}
// SetDefaultHTTPClient set the default http client for this package
func SetDefaultHTTPClient(client http.Client) {
defaultHTTPClient = client
}
var defaultHTTPClient = http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: time.Second * 30,
}
// Option SES api options
type Option struct {
endpoint string
source string
accessKeyID string
secretAccessKey []byte
}
// SendMail send text emails
func (p *Option) SendMail(subject, bodyText string, toAddresses []string) (string, error) {
b := make([]byte, base64.StdEncoding.EncodedLen(len(subject)))
base64.StdEncoding.Encode(b, []byte(subject))
var subBuf bytes.Buffer
subBuf.Grow(128)
subBuf.WriteString("=?UTF-8?B?")
subBuf.Write(b)
subBuf.WriteString("?=")
data := make(url.Values, 6+len(toAddresses))
data.Add("Action", "SendEmail")
data.Add("Source", p.source)
data.Add("Message.Subject.Data", subBuf.String())
data.Add("Message.Body.Text.Data", bodyText)
data.Add("AWSAccessKeyId", p.accessKeyID)
for idx, email := range toAddresses {
data.Add("Destination.ToAddresses.member."+strconv.Itoa(idx+1), email)
}
body, err := p.doRequest(data)
return string(body), err
}
// SendHTMLMail send html emails
func (p *Option) SendHTMLMail(subject, bodyHTML string, toAddresses []string) (string, error) {
b := make([]byte, base64.StdEncoding.EncodedLen(len(subject)))
base64.StdEncoding.Encode(b, []byte(subject))
var subBuf bytes.Buffer
subBuf.Grow(128)
subBuf.WriteString("=?UTF-8?B?")
subBuf.Write(b)
subBuf.WriteString("?=")
data := make(url.Values, 6+len(toAddresses))
data.Add("Action", "SendEmail")
data.Add("Source", p.source)
data.Add("Message.Subject.Data", subject)
data.Add("Message.Body.Html.Data", bodyHTML)
data.Add("AWSAccessKeyId", string(p.accessKeyID))
for idx, email := range toAddresses {
data.Add("Destination.ToAddresses.member."+strconv.Itoa(idx+1), email)
}
body, err := p.doRequest(data)
return string(body), err
}
func (p *Option) doRequest(data url.Values) ([]byte, error) {
request, err := http.NewRequest(
http.MethodPost,
p.endpoint,
strings.NewReader(data.Encode()),
)
if err != nil {
return nil, err
}
// set Header Content-Type
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// set Header Date
date := time.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05 -0700")
request.Header.Set("Date", date)
// calculate signature
h := hmac.New(sha256.New, p.secretAccessKey)
_, _ = h.Write([]byte(date))
signature := make([]byte, base64.StdEncoding.EncodedLen(h.Size()))
base64.StdEncoding.Encode(signature, h.Sum(nil))
// calculate Authorization
var authorizationBuf bytes.Buffer
authorizationBuf.Grow(128)
authorizationBuf.WriteString("AWS3-HTTPS AWSAccessKeyId=")
authorizationBuf.WriteString(p.accessKeyID)
authorizationBuf.WriteString(", Algorithm=HmacSHA256, Signature=")
authorizationBuf.Write(signature)
// set Header X-Amzn-Authorization
request.Header.Set("X-Amzn-Authorization", authorizationBuf.String())
resp, err := defaultHTTPClient.Do(request)
if err != nil {
return nil, fmt.Errorf("http request failed, err: %s", err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK || err != nil {
return nil, fmt.Errorf("http response failed, code: %d, body: %s, err: %v", resp.StatusCode, body, err)
}
return body, nil
}