-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
aws_ses.go
98 lines (80 loc) · 2.25 KB
/
aws_ses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gomail
import (
"bytes"
"fmt"
"log"
"strings"
"github.com/domodwyer/mailyak"
)
// awsSesInterface is an interface for ses/mocking
type awsSesInterface interface {
SendRawEmail(raw []byte) (string, error)
}
// sendViaAwsSes sends an email using the AWS SES service
func sendViaAwsSes(client awsSesInterface, email *Email) (err error) {
// Create new mail message
mail := mailyak.New("", nil)
// Add the "to" recipients
mail.To(email.Recipients...)
// Add the "cc" recipients
if len(email.RecipientsCc) > 0 {
mail.Cc(email.RecipientsCc...)
}
// Add the "bcc" recipients
if len(email.RecipientsBcc) > 0 {
mail.WriteBccHeader(true)
mail.Bcc(email.RecipientsBcc...)
}
// Add the basics
mail.From(email.FromAddress)
mail.FromName(email.FromName)
mail.Subject(email.Subject)
// Add a custom reply to address
if len(email.ReplyToAddress) > 0 {
mail.ReplyTo(email.ReplyToAddress)
}
// Add plain text
if len(email.PlainTextContent) > 0 {
mail.Plain().Set(email.PlainTextContent)
}
// Add html
if len(email.HTMLContent) > 0 {
mail.HTML().Set(email.HTMLContent)
}
// Add any attachments
if len(email.Attachments) > 0 {
for _, att := range email.Attachments {
mail.Attach(att.FileName, att.FileReader)
}
}
// Add importance?
if email.Important {
mail.AddHeader("X-Priority", "1 (Highest)")
mail.AddHeader("X-MSMail-Priority", "High")
mail.AddHeader("Importance", "High")
}
// Warn about features that are set but not available
if email.TrackClicks {
log.Printf("warning: track clicks is enabled, but AWS SES does not offer this feature")
}
if email.TrackOpens {
log.Printf("warning: track opens is enabled, but AWS SES does not offer this feature")
}
if email.AutoText {
log.Printf("warning: auto text is enabled, but AWS SES does not offer this feature")
}
// Create the email buffer and pass to the ses service
var buf *bytes.Buffer
if buf, err = mail.MimeBuf(); err != nil {
return err
}
// Send the message post and check the response
var awsResponse string
awsResponse, err = client.SendRawEmail(buf.Bytes())
if err != nil {
return err
} else if !strings.Contains(awsResponse, "SendRawEmailResult") {
err = fmt.Errorf("aws ses did not return expected valid response: %s", awsResponse)
}
return
}