forked from gazebo-web/gz-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.go
73 lines (64 loc) · 1.67 KB
/
aws.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
package gz
import (
"errors"
"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"
)
// SendEmail using AWS Simple Email Service (SES)
// The following environment variables must be set:
//
// AWS_REGION
// AWS_ACCESS_KEY_ID
// AWS_SECRET_ACCESS_KEY
func SendEmail(sender, recipient, subject, body string) error {
// The character encoding for the email.
charSet := "UTF-8"
sess := session.Must(session.NewSession())
// Create an SES session.
svc := ses.New(sess)
// Assemble the email.
input := &ses.SendEmailInput{
Destination: &ses.Destination{
CcAddresses: []*string{},
ToAddresses: []*string{
aws.String(recipient),
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(charSet),
Data: aws.String(body),
},
},
Subject: &ses.Content{
Charset: aws.String(charSet),
Data: aws.String(subject),
},
},
Source: aws.String(sender),
}
// Attempt to send the email.
_, err := svc.SendEmail(input)
// Return error messages if they occur.
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
var code string
switch aerr.Code() {
case ses.ErrCodeMessageRejected:
code = ses.ErrCodeMessageRejected
case ses.ErrCodeMailFromDomainNotVerifiedException:
code = ses.ErrCodeMailFromDomainNotVerifiedException
case ses.ErrCodeConfigurationSetDoesNotExistException:
code = ses.ErrCodeConfigurationSetDoesNotExistException
default:
code = "Unknown AWS SES error"
}
return errors.New(code + " " + aerr.Error())
}
return errors.New(err.Error())
}
return nil
}