Skip to content

Commit

Permalink
move files from private to public
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Crosby committed Dec 18, 2011
1 parent 16451b6 commit 7638410
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.6
_obj
6.out
_testmain.go
_test
6 changes: 6 additions & 0 deletions Makefile
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
90 changes: 90 additions & 0 deletions amzses.go
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
}

0 comments on commit 7638410

Please sign in to comment.