-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathses.go
79 lines (69 loc) · 1.68 KB
/
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
package email
import (
"context"
"github.com/0xsequence/waas-authenticator/config"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/ses"
"github.com/aws/aws-sdk-go-v2/service/ses/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
)
type sesSender struct {
cfg config.SESConfig
awsCfg aws.Config
}
func NewSESSender(awsCfg aws.Config, cfg config.SESConfig) Sender {
sender := &sesSender{
cfg: cfg,
awsCfg: awsCfg,
}
return sender
}
func (s *sesSender) Send(ctx context.Context, msg *Message) error {
awsCfg := s.awsCfg
accessRoleARN := s.cfg.AccessRoleARN
if msg.AccessRoleARN != "" {
accessRoleARN = msg.AccessRoleARN
}
if accessRoleARN != "" {
stsClient := sts.NewFromConfig(awsCfg)
creds := stscreds.NewAssumeRoleProvider(stsClient, accessRoleARN)
awsCfg.Credentials = aws.NewCredentialsCache(creds)
}
if s.cfg.Region != "" {
awsCfg.Region = s.cfg.Region
}
client := ses.NewFromConfig(awsCfg)
source := &s.cfg.Source
if msg.Source != "" {
source = &msg.Source
}
sourceARN := &s.cfg.SourceARN
if msg.SourceARN != "" {
sourceARN = &msg.SourceARN
}
_, err := client.SendEmail(ctx, &ses.SendEmailInput{
Destination: &types.Destination{
ToAddresses: []string{msg.Recipient},
},
Message: &types.Message{
Body: &types.Body{
Html: &types.Content{
Data: &msg.HTML,
Charset: aws.String("UTF-8"),
},
Text: &types.Content{
Data: &msg.Text,
Charset: aws.String("UTF-8"),
},
},
Subject: &types.Content{
Data: &msg.Subject,
Charset: aws.String("UTF-8"),
},
},
Source: source,
SourceArn: sourceARN,
})
return err
}