Skip to content

Commit

Permalink
using post instead of get, added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Crosby committed Jan 4, 2013
1 parent 3dd82b4 commit e35c9ff
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
43 changes: 41 additions & 2 deletions amzses.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"log"
"net/http"
"net/url"
"strings"
"time"
)

Expand All @@ -43,7 +44,7 @@ func SendMail(from, to, subject, body string) (string, error) {
data.Add("Message.Body.Text.Data", body)
data.Add("AWSAccessKeyId", accessKey)

return sesGet(data)
return sesPost(data)
}

func SendMailHTML(from, to, subject, bodyText, bodyHTML string) (string, error) {
Expand All @@ -56,7 +57,7 @@ func SendMailHTML(from, to, subject, bodyText, bodyHTML string) (string, error)
data.Add("Message.Body.Html.Data", bodyHTML)
data.Add("AWSAccessKeyId", accessKey)

return sesGet(data)
return sesPost(data)
}

func authorizationHeader(date string) []string {
Expand Down Expand Up @@ -110,3 +111,41 @@ func sesGet(data url.Values) (string, error) {

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
}
47 changes: 47 additions & 0 deletions send_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package amzses

import (
"flag"
"testing"
)

var to, from string

func init() {
flag.StringVar(&to, "to", "", "email recipient")
flag.StringVar(&from, "from", "", "email sender")
}

func checkFlags(t *testing.T) {
if len(to) == 0 {
t.Fatal("must specify recipient via -to flag.")
}
if len(from) == 0 {
t.Fatal("must specify sender via -from flag.")
}
}

func TestText(t *testing.T) {
checkFlags(t)
_, err := SendMail(from, to, "amzses text test", textBody)
if err != nil {
t.Fatal(err)
}
}

func TestHtml(t *testing.T) {
checkFlags(t)
_, err := SendMailHTML(from, to, "amzses html test", textBody, htmlBody)
if err != nil {
t.Fatal(err)
}
}

var textBody = `This is an example email body for the amzses go package.`

var htmlBody = `
This is an <b>html email</b>.
<br/>
<br/>
<img src="http://placehold.it/600x200/">
`

0 comments on commit e35c9ff

Please sign in to comment.