Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usability improvements #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions amzses.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/stathat/jconfig"
"io/ioutil"
"log"
"net/http"
Expand All @@ -27,48 +26,50 @@ const (
endpoint = "https://email.us-east-1.amazonaws.com"
)

var accessKey, secretKey string
type Client struct {
accessKey string
secretKey string
}

func init() {
config := jconfig.LoadConfig("/etc/aws.conf")
accessKey = config.GetString("aws_access_key")
secretKey = config.GetString("aws_secret_key")
func NewClient(sesAccessKey, sesSecretKey string) *Client {
client := &Client{accessKey: sesAccessKey, secretKey: sesSecretKey}
return client
}

func SendMail(from, to, subject, body string) (string, error) {
func (c *Client) 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)
data.Add("AWSAccessKeyId", c.accessKey)

return sesPost(data)
return c.sesPost(data)
}

func SendMailHTML(from, to, subject, bodyText, bodyHTML string) (string, error) {
func (c *Client) SendMailHTML(from, to, subject, 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)
data.Add("AWSAccessKeyId", c.accessKey)

return sesPost(data)
return c.sesPost(data)
}

func authorizationHeader(date string) []string {
h := hmac.New(sha256.New, []uint8(secretKey))
func (c *Client) authorizationHeader(date string) []string {
h := hmac.New(sha256.New, []uint8(c.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)
auth := fmt.Sprintf("AWS3-HTTPS AWSAccessKeyId=%s, Algorithm=HmacSHA256, Signature=%s", c.accessKey, signature)
return []string{auth}
}

func sesGet(data url.Values) (string, error) {
func (c *Client) sesGet(data url.Values) (string, error) {
urlstr := fmt.Sprintf("%s?%s", endpoint, data.Encode())
endpointURL, _ := url.Parse(urlstr)
headers := map[string][]string{}
Expand All @@ -78,10 +79,10 @@ func sesGet(data url.Values) (string, error) {
date := now.Format("Mon, 02 Jan 2006 15:04:05 -0700")
headers["Date"] = []string{date}

h := hmac.New(sha256.New, []uint8(secretKey))
h := hmac.New(sha256.New, []uint8(c.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)
auth := fmt.Sprintf("AWS3-HTTPS AWSAccessKeyId=%s, Algorithm=HmacSHA256, Signature=%s", c.accessKey, signature)
headers["X-Amzn-Authorization"] = []string{auth}

req := http.Request{
Expand Down Expand Up @@ -112,7 +113,7 @@ func sesGet(data url.Values) (string, error) {
return string(resultbody), nil
}

func sesPost(data url.Values) (string, error) {
func (c *Client) sesPost(data url.Values) (string, error) {
body := strings.NewReader(data.Encode())
req, err := http.NewRequest("POST", endpoint, body)
if err != nil {
Expand All @@ -125,10 +126,10 @@ func sesPost(data url.Values) (string, error) {
date := now.Format("Mon, 02 Jan 2006 15:04:05 -0700")
req.Header.Set("Date", date)

h := hmac.New(sha256.New, []uint8(secretKey))
h := hmac.New(sha256.New, []uint8(c.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)
auth := fmt.Sprintf("AWS3-HTTPS AWSAccessKeyId=%s, Algorithm=HmacSHA256, Signature=%s", c.accessKey, signature)
req.Header.Set("X-Amzn-Authorization", auth)

r, err := http.DefaultClient.Do(req)
Expand Down