-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrick Crosby
committed
Dec 18, 2011
1 parent
16451b6
commit 7638410
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.6 | ||
_obj | ||
6.out | ||
_testmain.go | ||
_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
include $(GOROOT)/src/Make.inc | ||
|
||
TARG=amzses | ||
GOFILES=amzses.go | ||
|
||
include $(GOROOT)/src/Make.pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package amzses | ||
|
||
import ( | ||
"crypto/hmac" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"github.com/stathat/jconfig" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
const ( | ||
endpoint = "https://email.us-east-1.amazonaws.com" | ||
) | ||
|
||
var accessKey, secretKey string | ||
|
||
func init() { | ||
config := jconfig.LoadConfig("/etc/aws.conf") | ||
accessKey = config.GetString("aws_access_key") | ||
secretKey = config.GetString("aws_secret_key") | ||
} | ||
|
||
func SendMail(from, to, subject, 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) | ||
|
||
return sesGet(data) | ||
} | ||
|
||
func authorizationHeader(date string) []string { | ||
h := hmac.NewSHA256([]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.NewSHA256([]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 | ||
} |