Skip to content

Commit

Permalink
Use mailyak to build MIME email
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbanham committed Mar 7, 2021
1 parent 6ca165f commit 5475885
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 61 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ go 1.12

require (
github.com/aws/aws-sdk-go v1.13.16
github.com/davidbanham/mailyak/v3 v3.3.2-0.20210307042809-139c74aea23a // indirect
github.com/davidbanham/required_env v0.0.0-20150902120453-a84628a4c244
github.com/go-ini/ini v1.33.0 // indirect
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/aws/aws-sdk-go v1.13.16 h1:cnDTVVkpO9ls15KnYL/2KVUV4XnDN+pTRjc5fHrbMG
github.com/aws/aws-sdk-go v1.13.16/go.mod h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidbanham/mailyak v2.1.0+incompatible h1:wlpxXpYTh9/SL5bDeOOpLftuA53QgNOCOSPJOJd5baA=
github.com/davidbanham/mailyak v2.1.0+incompatible/go.mod h1:unSe35boqc4uPXBPLvkpGi3TmH4v8quJiasW3v2jFWA=
github.com/davidbanham/mailyak/v3 v3.3.2-0.20210307042809-139c74aea23a h1:s9ujD/an8flMEcgCAoIuJx04d4Bx31O7Fgcv0EdwceM=
github.com/davidbanham/mailyak/v3 v3.3.2-0.20210307042809-139c74aea23a/go.mod h1:vEt91bwfcc2fAe1+IIxF+zzwIn1dIsYKTYpsmCLbPOQ=
github.com/davidbanham/required_env v0.0.0-20150902120453-a84628a4c244 h1:FgQ8KQ99RjcMqaTSqmdisj2quuDKlL1x62SoRtZQwqw=
github.com/davidbanham/required_env v0.0.0-20150902120453-a84628a4c244/go.mod h1:zJVA+kv43obXkwVzNtnVJK9rIUvzRtV+rSe1qXyf4xk=
github.com/go-ini/ini v1.33.0 h1:/0Y2X+/6jgfPYl2LOihvxikDfznXMufz0Zkr3mW+7Zg=
Expand Down
78 changes: 18 additions & 60 deletions notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/mailyak/v3"
"github.com/davidbanham/required_env"
)

Expand All @@ -34,51 +35,6 @@ func init() {
"AWS_SECRET_ACCESS_KEY": "",
})

var err error
tmpl, err = template.New("email").Parse(`From: {{.From}}
To: {{.To}}
{{ if .ReplyTo }}Reply-To: {{.ReplyTo}}{{ end }}
Subject: {{.Subject}}
MIME-Version: 1.0
Content-type: multipart/mixed;
boundary="NextPart"
--NextPart
Content-type: multipart/alternative;
boundary="AlternativePart"
{{ if .Text }}
--AlternativePart
Content-Type: text/plain
{{.Text}}
{{ end}}
{{ if .HTML }}
--AlternativePart
Content-Type: text/html
{{.HTML}}
{{ end }}
--AlternativePart--
`)

if err != nil {
log.Fatal(err)
}

attachmentTmpl, err = template.New("attachment").Parse(`--NextPart
Content-Type: {{.ContentType}};
Content-Disposition: attachment;
filename="{{.Filename}}"
`)

if err != nil {
log.Fatal(err)
}

sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
})
Expand All @@ -104,14 +60,6 @@ type Attachment struct {
Filename string
}

func (attachment Attachment) execute(data io.Writer) error {
if err := attachmentTmpl.Execute(data, attachment); err != nil {
return err
}
_, err := io.Copy(data, attachment.Data)
return err
}

func (attachment *Attachment) MarshalJSON() ([]byte, error) {
data, err := ioutil.ReadAll(attachment.Data)
if err != nil {
Expand Down Expand Up @@ -157,19 +105,29 @@ func SendEmail(email Email) error {
}
log.Println("INFO notifications sending email to", email.To, "from", email.From)

data := bytes.Buffer{}
yak := mailyak.New("", nil)

if err := tmpl.Execute(&data, email); err != nil {
return err
yak.To(email.To)
yak.From(email.From)
yak.ReplyTo(email.ReplyTo)
yak.Subject(email.Subject)
if email.HTML != "" {
yak.HTML().Set(email.HTML)
}
if email.Text != "" {
yak.Plain().Set(email.Text)
}

for _, attachment := range email.Attachments {
if err := attachment.execute(&data); err != nil {
return nil
}
yak.AttachWithMimeType(attachment.Filename, attachment.Data, attachment.ContentType)
}

buf, err := yak.MimeBuf()
if err != nil {
return err
}

return SendRawEmail(data.Bytes())
return SendRawEmail(buf.Bytes())
}

func SendRawEmail(data []byte) error {
Expand Down

0 comments on commit 5475885

Please sign in to comment.