forked from socketlabs/socketlabs-go
/
sendvalidator.go
192 lines (163 loc) · 5.71 KB
/
sendvalidator.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package injectionapi
import (
"strconv"
"strings"
"github.com/socketlabs/socketlabs-go/injectionapi/message"
)
//SendValidator is used by the SocketLabsClient to conduct basic validation on the message before sending to the Injection API.
type sendValidator struct{}
//ValidateCredentials Validate the ServerId and Api Key pair prior before sending to the Injection API.
func (sendValidator) ValidateCredentials(serverID int, apiKey string) (response SendResponse) {
response = SendResponse{}
if len(strings.TrimSpace(apiKey)) == 0 || serverID < 0 {
response.Result = SendResultAUTHENTICATIONVALIDATIONFAILED
} else {
response.Result = SendResultSUCCESS
}
return
}
//ValidateBasicMessage Validate a basic email message before sending to the Injection API.
func (sendValidator) ValidateBasicMessage(basicMessage message.BasicMessage) (response SendResponse) {
response = SendResponse{Result: SendResultUNKNOWNERROR}
// ReplyTo is an optional property, so check to see if it is in use first
if len(basicMessage.ReplyTo.EmailAddress) > 0 { // ReplyTo is an optional property, so check to see if it is in use first
if !basicMessage.ReplyTo.IsValid() {
response.Result = SendResultRECIPIENTVALIDATIONINVALIDREPLYTO
return response
}
}
if !isValidSubject(basicMessage.Subject) {
response.Result = SendResultMESSAGEVALIDATIONEMPTYSUBJECT
} else if !basicMessage.From.IsValid() {
response.Result = SendResultEMAILADDRESSVALIDATIONINVALIDFROM
} else if !isValidBasicMessage(basicMessage) {
response.Result = SendResultEMPTYMESSAGE
} else if !isValidBasicMessageCustomHeaders(basicMessage) {
response.Result = SendResultMESSAGEVALIDATIONINVALIDCUSTOMHEADERS
} else {
response.Result = SendResultSUCCESS
validRecipientCount, allRecipients := isValidBasicRecipientCount(basicMessage)
if validRecipientCount {
for _, recipient := range allRecipients {
if !recipient.IsValid() {
response.Result = SendResultRECIPIENTVALIDATIONINVALIDRECIPIENTS
addressResult := AddressResult{
Accepted: false,
EmailAddress: recipient.EmailAddress,
ErrorCode: "InvalidAddress"}
response.AddressResults = append(response.AddressResults, addressResult)
}
}
} else {
response.Result = SendResultNOVALIDRECIPIENTS
}
}
return response
}
//ValidateBulkMessage Validate a basic email message before sending to the Injection API.
func (sendValidator) ValidateBulkMessage(bulkMessage message.BulkMessage) (response SendResponse) {
response = SendResponse{Result: SendResultUNKNOWNERROR}
// ReplyTo is an optional property, so check to see if it is in use first
if len(bulkMessage.ReplyTo.EmailAddress) > 0 {
if !bulkMessage.ReplyTo.IsValid() {
response.Result = SendResultRECIPIENTVALIDATIONINVALIDREPLYTO
return response
}
}
if !isValidSubject(bulkMessage.Subject) {
response.Result = SendResultMESSAGEVALIDATIONEMPTYSUBJECT
} else if !bulkMessage.From.IsValid() {
response.Result = SendResultEMAILADDRESSVALIDATIONINVALIDFROM
} else if !isValidBulkMessage(bulkMessage) {
response.Result = SendResultRECIPIENTVALIDATIONINVALIDREPLYTO
} else if !isValidBulkMessageCustomHeaders(bulkMessage) {
response.Result = SendResultMESSAGEVALIDATIONINVALIDCUSTOMHEADERS
} else {
response.Result = SendResultSUCCESS
validRecipientCount, allRecipients := isValidBulkRecipientCount(bulkMessage)
if validRecipientCount {
for _, recipient := range allRecipients {
if !recipient.IsValid() {
response.Result = SendResultRECIPIENTVALIDATIONINVALIDRECIPIENTS
addressResult := AddressResult{
Accepted: false,
EmailAddress: recipient.Email,
ErrorCode: "InvalidAddress"}
response.AddressResults = append(response.AddressResults, addressResult)
}
}
} else {
response.Result = SendResultNOVALIDRECIPIENTS
}
}
return response
}
func isValidSubject(val string) bool {
return !(len(strings.TrimSpace(val)) == 0)
}
func isValidFromAddress(val string) bool {
return !(len(strings.TrimSpace(val)) == 0)
}
func isValidBasicMessage(message message.BasicMessage) bool {
return isValidAPITemplate(message.ApiTemplate) ||
isValidHTMLBody(message.HtmlBody) ||
isValidTextBody(message.PlainTextBody)
}
func isValidBulkMessage(message message.BulkMessage) bool {
return isValidAPITemplate(message.ApiTemplate) ||
isValidHTMLBody(message.HtmlBody) ||
isValidTextBody(message.PlainTextBody)
}
func isValidAPITemplate(val string) bool {
// make sure its actually a number
templateID, err := strconv.Atoi(val)
if err != nil {
return false
}
return templateID != 0
}
func isValidHTMLBody(val string) bool {
return !(len(strings.TrimSpace(val)) == 0)
}
func isValidTextBody(val string) bool {
return !(len(strings.TrimSpace(val)) == 0)
}
func isValidBasicMessageCustomHeaders(message message.BasicMessage) bool {
if message.CustomHeaders == nil {
return true
}
if len(message.CustomHeaders) == 0 {
return true
}
for _, header := range message.CustomHeaders {
if !header.IsValid() {
return false
}
}
return true
}
func isValidBulkMessageCustomHeaders(message message.BulkMessage) bool {
if message.CustomHeaders == nil {
return true
}
if len(message.CustomHeaders) == 0 {
return true
}
for _, header := range message.CustomHeaders {
if !header.IsValid() {
return false
}
}
return true
}
func isValidBasicRecipientCount(message message.BasicMessage) (isValid bool, allRecipients []message.EmailAddress) {
allRecipients = append(message.To, message.Bcc...)
allRecipients = append(allRecipients, message.Cc...)
isValid = len(allRecipients) > 0
return
}
func isValidBulkRecipientCount(message message.BulkMessage) (isValid bool, allRecipients []message.BulkRecipient) {
allRecipients = message.To
isValid = len(allRecipients) > 0
return
}