-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathses_manager.go
139 lines (121 loc) · 3.13 KB
/
ses_manager.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
129
130
131
132
133
134
135
136
137
138
139
package email
import (
"bytes"
"context"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ses"
"github.com/aws/aws-sdk-go-v2/service/ses/types"
"gopkg.in/gomail.v2"
"github.com/adwitiyaio/arka/cloud"
)
type sesManager struct {
clm cloud.Manager
region string
client *ses.Client
}
func (sm *sesManager) SendEmail(options Options) (interface{}, error) {
if len(options.Attachments) > 0 {
return sm.sendRawEmail(options)
}
input := &ses.SendEmailInput{
Destination: &types.Destination{
BccAddresses: options.Bcc,
CcAddresses: options.Cc,
ToAddresses: options.To,
},
Message: &types.Message{
Body: &types.Body{
Html: &types.Content{
Charset: aws.String("UTF-8"),
Data: aws.String(options.Html),
},
Text: &types.Content{
Charset: aws.String("UTF-8"),
Data: aws.String(options.Text),
},
},
Subject: &types.Content{
Charset: aws.String("UTF-8"),
Data: aws.String(options.Subject),
},
},
Source: aws.String(options.Sender),
ReplyToAddresses: []string{options.ReplyToAddress},
}
return sm.dispatch(input)
}
func (sm sesManager) dispatch(input *ses.SendEmailInput) (interface{}, error) {
if os.Getenv("CI") == "true" {
return nil, nil
}
resp, err := sm.client.SendEmail(context.TODO(), input)
if err != nil {
return nil, err
}
return resp, nil
}
func (sm sesManager) sendRawEmail(options Options) (interface{}, error) {
// Create raw message
msg := gomail.NewMessage()
// Set to
var recipients []string
for _, r := range options.To {
recipients = append(recipients, r)
}
// Set to emails
msg.SetHeader("To", options.To...)
// Set cc
if len(options.Cc) > 0 {
for _, r := range options.Cc {
recipients = append(recipients, r)
}
msg.SetHeader("cc", options.Cc...)
}
// Set Bcc
if len(options.Bcc) > 0 {
for _, r := range options.Bcc {
recipients = append(recipients, r)
}
msg.SetHeader("bcc", options.Bcc...)
}
// Set sender name
if options.SenderName == "" {
options.SenderName = options.Sender
}
msg.SetAddressHeader("From", options.Sender, options.SenderName)
msg.SetHeader("To", options.To...)
msg.SetHeader("Reply-To", options.ReplyToAddress)
msg.SetHeader("Subject", options.Subject)
msg.SetBody("text/html", options.Html)
if len(options.Attachments) > 0 {
for _, f := range options.Attachments {
msg.Attach(f)
}
}
// create a new buffer to add raw data
var emailRaw bytes.Buffer
_, err := msg.WriteTo(&emailRaw)
if err != nil {
return nil, err
}
// Create new raw message
message := &types.RawMessage{Data: emailRaw.Bytes()}
input := &ses.SendRawEmailInput{Source: &options.Sender, Destinations: recipients, RawMessage: message}
return sm.dispatchRawEmail(input)
}
func (sm sesManager) dispatchRawEmail(input *ses.SendRawEmailInput) (interface{}, error) {
if os.Getenv("CI") == "true" {
return nil, nil
}
resp, err := sm.client.SendRawEmail(context.TODO(), input)
if err != nil {
return nil, err
}
return resp, nil
}
func (sm *sesManager) initialize() {
config := sm.clm.GetConfig()
sm.client = ses.NewFromConfig(config)
sm.region = sm.clm.GetRegion()
}