Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbanham committed Mar 17, 2018
0 parents commit 5a14145
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
32 changes: 32 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"


[[constraint]]
name = "github.com/aws/aws-sdk-go"
version = "1.13.16"

[[constraint]]
branch = "master"
name = "github.com/davidbanham/required_env"
85 changes: 85 additions & 0 deletions notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package notifications

import (
"log"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
"github.com/davidbanham/required_env"
)

var svc *ses.SES
var testMode bool

func init() {
if os.Getenv("TEST_MOCKS_ON") == "true" {
testMode = true
return
}
required_env.Ensure(map[string]string{
"AWS_ACCESS_KEY_ID": "",
"AWS_SECRET_ACCESS_KEY": "",
})
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
})
if err != nil {
log.Fatal(err)
}
svc = ses.New(sess)
}

type Email struct {
To string
From string
ReplyTo string
Text string
HTML string
Subject string
}

func SendEmail(email Email) (err error) {
if testMode {
log.Println("INFO notifications TESTMODE dropping email to", email.To, "from", email.From)
return
}
log.Println("INFO notifications sending email to", email.To, "from", email.From)

body := &ses.Body{
Text: &ses.Content{
Data: aws.String(email.Text),
Charset: aws.String("UTF8"),
},
}

if email.HTML != "" {
body.Html = &ses.Content{
Data: aws.String(email.HTML),
Charset: aws.String("UTF8"),
}
}

params := &ses.SendEmailInput{
Destination: &ses.Destination{
ToAddresses: []*string{
aws.String(email.To),
},
},
Message: &ses.Message{
Body: body,
Subject: &ses.Content{
Data: aws.String(email.Subject),
Charset: aws.String("UTF8"),
},
},
Source: aws.String(email.From),
ReplyToAddresses: []*string{
aws.String(email.ReplyTo),
},
}
_, err = svc.SendEmail(params)

return
}
19 changes: 19 additions & 0 deletions notifications_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package notifications

import (
"testing"
)

func TestNotificationsLive(t *testing.T) {
err := SendEmail(Email{
To: "david@banham.id.au",
From: "testrun@takehome.io",
ReplyTo: "lolwut@takehome.io",
Text: "this is a test run",
HTML: "this <i>is a test</i> run",
Subject: "test run",
})
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 5a14145

Please sign in to comment.