forked from stathat/amzses
/
amzses.go
162 lines (131 loc) · 4.26 KB
/
amzses.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
// Copyright 2011-2013 Numrotron Inc.
// Use of this source code is governed by an MIT-style license
// that can be found in the LICENSE file.
//
// Developed at www.stathat.com by Patrick Crosby
// Contact us on twitter with any questions: twitter.com/stat_hat
// amzses is a Go package to send emails using Amazon's Simple Email Service.
package amzses
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
"os"
"stathat.com/c/jconfig"
)
const (
endpoint = "https://email.us-east-1.amazonaws.com"
)
var accessKey, secretKey string
func init() {
config := jconfig.LoadConfig( os.Getenv("GOPATH") + "/assets/config/aws.json")
accessKey = config.GetString("aws_access_key")
secretKey = config.GetString("aws_secret_key")
}
func SendMail(from, to, subject, replyto, body string) (string, error) {
data := make(url.Values)
data.Add("Action", "SendEmail")
data.Add("Source", from)
data.Add("Destination.ToAddresses.member.1", to)
data.Add("Message.Subject.Data", subject)
data.Add("Message.Body.Text.Data", body)
data.Add("AWSAccessKeyId", accessKey)
if (replyto != "") {
data.Add("ReplyToAddresses.member.1", replyto)
}
return sesPost(data)
}
func SendMailHTML(from, to, subject, replyto, bodyText, bodyHTML string) (string, error) {
data := make(url.Values)
data.Add("Action", "SendEmail")
data.Add("Source", from)
data.Add("Destination.ToAddresses.member.1", to)
data.Add("Message.Subject.Data", subject)
data.Add("Message.Body.Text.Data", bodyText)
data.Add("Message.Body.Html.Data", bodyHTML)
data.Add("AWSAccessKeyId", accessKey)
if (replyto != "") {
data.Add("ReplyToAddresses.member.1", replyto)
}
return sesPost(data)
}
func authorizationHeader(date string) []string {
h := hmac.New(sha256.New, []uint8(secretKey))
h.Write([]uint8(date))
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
auth := fmt.Sprintf("AWS3-HTTPS AWSAccessKeyId=%s, Algorithm=HmacSHA256, Signature=%s", accessKey, signature)
return []string{auth}
}
func sesGet(data url.Values) (string, error) {
urlstr := fmt.Sprintf("%s?%s", endpoint, data.Encode())
endpointURL, _ := url.Parse(urlstr)
headers := map[string][]string{}
now := time.Now().UTC()
// date format: "Tue, 25 May 2010 21:20:27 +0000"
date := now.Format("Mon, 02 Jan 2006 15:04:05 -0700")
headers["Date"] = []string{date}
h := hmac.New(sha256.New, []uint8(secretKey))
h.Write([]uint8(date))
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
auth := fmt.Sprintf("AWS3-HTTPS AWSAccessKeyId=%s, Algorithm=HmacSHA256, Signature=%s", accessKey, signature)
headers["X-Amzn-Authorization"] = []string{auth}
req := http.Request{
URL: endpointURL,
Method: "GET",
ProtoMajor: 1,
ProtoMinor: 1,
Close: true,
Header: headers,
}
r, err := http.DefaultClient.Do(&req)
if err != nil {
log.Printf("http error: %s", err)
return "", err
}
resultbody, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
if r.StatusCode != 200 {
log.Printf("error, status = %d", r.StatusCode)
log.Printf("error response: %s", resultbody)
return "", errors.New(string(resultbody))
}
return string(resultbody), nil
}
func sesPost(data url.Values) (string, error) {
body := strings.NewReader(data.Encode())
req, err := http.NewRequest("POST", endpoint, body)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
now := time.Now().UTC()
// date format: "Tue, 25 May 2010 21:20:27 +0000"
date := now.Format("Mon, 02 Jan 2006 15:04:05 -0700")
req.Header.Set("Date", date)
h := hmac.New(sha256.New, []uint8(secretKey))
h.Write([]uint8(date))
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
auth := fmt.Sprintf("AWS3-HTTPS AWSAccessKeyId=%s, Algorithm=HmacSHA256, Signature=%s", accessKey, signature)
req.Header.Set("X-Amzn-Authorization", auth)
r, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("http error: %s", err)
return "", err
}
resultbody, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
if r.StatusCode != 200 {
log.Printf("error, status = %d", r.StatusCode)
log.Printf("error response: %s", resultbody)
return "", errors.New(fmt.Sprintf("error code %d. response: %s", r.StatusCode, resultbody))
}
return string(resultbody), nil
}