-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsesSendEmail.go
128 lines (101 loc) · 3.08 KB
/
sesSendEmail.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package awsses
import (
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ses"
)
type EmailInfo struct {
Sender string `json:"sender"`
RecipientTo []string `json:"to"`
RecipientCc []string `json:"cc"`
RecipientBcc []string `json:"bcc"`
Subject string `json:"subject"`
HtmlBody string `json:"htmlbody"`
TextBody string `json:"textbody"`
CharSet string `json:"charset"`
}
func CreateRecipientList(addresses []string) []*string {
recipientList := []*string{}
if len(addresses) > 0 {
for _, addr := range addresses {
recipientList = append(recipientList, aws.String(addr))
}
}
return recipientList
}
func CreateEmailContent(charset string, data string) *ses.Content {
content := &ses.Content{}
if len(data) > 0 {
data = strings.Replace(data, " ", "\n", -1)
content.SetCharset(charset)
content.SetData(data)
}
return content
}
func SendEmail(info EmailInfo) (*ses.SendEmailOutput, error) {
// Create a new session in the us-west-2 region.
// Replace us-west-2 with the AWS Region you're using for Amazon SES.
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-northeast-1")},
)
if err != nil {
log.Fatalf("create session failed : %s", err.Error())
return nil, err
}
// Create an SES session.
svc := ses.New(sess)
// Assemble the email.
input := &ses.SendEmailInput{}
// Set Email Destination
destination := &ses.Destination{}
if len(info.RecipientTo) > 0 {
destination.SetToAddresses(CreateRecipientList(info.RecipientTo))
}
if len(info.RecipientBcc) > 0 {
destination.SetBccAddresses(CreateRecipientList(info.RecipientBcc))
}
if len(info.RecipientCc) > 0 {
destination.SetCcAddresses(CreateRecipientList(info.RecipientCc))
}
input.Destination = destination
// Set Email Body
message := &ses.Message{}
body := &ses.Body{}
if len(info.HtmlBody) > 0 {
body.SetHtml(CreateEmailContent(info.CharSet, info.HtmlBody))
}
body.SetText(CreateEmailContent(info.CharSet, info.TextBody))
message.SetBody(body)
message.SetSubject(CreateEmailContent(info.CharSet, info.Subject))
input.Message = message
// Set Email Source
input.SetSource(info.Sender)
// Attempt to send the email.
result, err := svc.SendEmail(input)
// Display error messages if they occur.
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
log.Fatalln(ses.ErrCodeMessageRejected, aerr.Error())
case ses.ErrCodeMailFromDomainNotVerifiedException:
log.Fatalln(ses.ErrCodeMailFromDomainNotVerifiedException, aerr.Error())
case ses.ErrCodeConfigurationSetDoesNotExistException:
log.Fatalln(ses.ErrCodeConfigurationSetDoesNotExistException, aerr.Error())
default:
log.Fatalln(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Fatalln(err.Error())
}
return nil, err
}
log.Printf("Send Email : %#v", info)
log.Println(result)
return result, nil
}