-
Notifications
You must be signed in to change notification settings - Fork 1
/
email.go
130 lines (108 loc) · 3.2 KB
/
email.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
package azurecomms
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
)
type EmailContent struct {
Subject string `json:"subject"`
PlainText string `json:"plainText"`
Html string `json:"html"`
}
type EmailAddress struct {
Address string `json:"address"`
DisplayName string `json:"displayName"`
}
type EmailAttachment struct {
Name string `json:"name"`
ContentType string `json:"contentType"`
ContentInBase64 string `json:"contentInBase64"`
}
type EmailRecipients struct {
To []EmailAddress `json:"to"`
Cc []EmailAddress `json:"cc,omitempty"`
Bcc []EmailAddress `json:"bcc,omitempty"`
}
type Email struct {
sender *EmailSender
SenderAddress string `json:"senderAddress"`
Content EmailContent `json:"content"`
Recipients EmailRecipients `json:"recipients"`
Attachments []EmailAttachment `json:"attachments,omitempty"`
ReplyTo []EmailAddress `json:"replyTo,omitempty"`
}
type EmailSender struct {
connString string
csEndpoint string
csAccessKey string
senderAddress string
replyTo EmailAddress
}
func NewEmailSender(connString string, senderAddress string, replyToName string, replyToEmailAddress string) (*EmailSender, error) {
csm, err := parseConnectionString(connString, "endpoint", "accesskey")
if err != nil {
return nil, err
}
es := &EmailSender{connString: connString, senderAddress: senderAddress, replyTo: EmailAddress{Address: replyToEmailAddress, DisplayName: replyToName}}
es.csEndpoint = csm["endpoint"]
es.csAccessKey = csm["accesskey"]
return es, nil
}
func (es *EmailSender) NewMail() *Email {
return &Email{
sender: es,
SenderAddress: es.senderAddress,
ReplyTo: []EmailAddress{es.replyTo},
}
}
func (m *Email) AddTo(emailAddress string, displayName string) {
m.Recipients.To = append(m.Recipients.To, EmailAddress{Address: emailAddress, DisplayName: displayName})
}
func (m *Email) AddCc(emailAddress string, displayName string) {
m.Recipients.Cc = append(m.Recipients.Cc, EmailAddress{Address: emailAddress, DisplayName: displayName})
}
func (m *Email) AddBcc(emailAddress string, displayName string) {
m.Recipients.Bcc = append(m.Recipients.Bcc, EmailAddress{Address: emailAddress, DisplayName: displayName})
}
func (m *Email) SetSubject(subject string) {
m.Content.Subject = subject
}
func (m *Email) SetContent(html string, text string) {
m.Content.Html = html
m.Content.PlainText = text
}
type ErrorResponse struct {
Error struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
func (m *Email) Send() error {
content, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
var bodyBytes bytes.Buffer
bodyBytes.Write(content)
req, err := http.NewRequest(http.MethodPost, m.sender.csEndpoint+"emails:send?api-version=2023-03-31", &bodyBytes)
if err != nil {
return err
}
err = signRequest(m.sender.csAccessKey, req, content)
if err != nil {
return err
}
rsp, err := http.DefaultClient.Do(req)
if rsp.StatusCode != 202 {
var rerr ErrorResponse
rd, _ := io.ReadAll(rsp.Body)
err = json.Unmarshal(rd, &rerr)
if err != nil {
return err
}
return errors.New(rerr.Error.Message)
}
return nil
}