-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
229 lines (179 loc) · 4.28 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package visimail
import (
"encoding/json"
"errors"
)
var (
ErrFromRequired = errors.New("from is required")
ErrToRequired = errors.New("to is required")
ErrBodyRequired = errors.New("body is required")
ErrSubjectRequired = errors.New("subject is required")
)
type Email struct {
from Sender
to []Recipient
cc []Recipient
bcc []Recipient
body Content
subject string
replyTo Recipient
attachments []Attachment
tags []Tag
}
func (e *Email) Validate() error {
if _, ok := e.body.(TemplateContent); !ok {
if e.from == nil {
return ErrFromRequired
}
if len(e.subject) <= 0 {
return ErrSubjectRequired
}
}
if e.from != nil {
if err := e.from.Validate(); err != nil {
return err
}
}
if len(e.to) <= 0 {
return ErrToRequired
}
for _, r := range e.to {
if err := r.Validate(); err != nil {
return err
}
}
for _, r := range e.cc {
if err := r.Validate(); err != nil {
return err
}
}
for _, r := range e.bcc {
if err := r.Validate(); err != nil {
return err
}
}
if e.body == nil {
return ErrBodyRequired
}
if err := e.body.Validate(); err != nil {
return err
}
if e.replyTo != nil {
if err := e.replyTo.Validate(); err != nil {
return err
}
}
for _, a := range e.attachments {
if err := a.Validate(); err != nil {
return err
}
}
for _, t := range e.tags {
if t.IsEmpty() {
return ErrEmptyTag
}
}
return nil
}
func (e Email) copy() *Email {
return &Email{e.from, e.to, e.cc, e.bcc, e.body, e.subject, e.replyTo, e.attachments, e.tags}
}
func (e Email) MarshalJSON() ([]byte, error) {
bBody, err := json.Marshal(e.body)
if err != nil {
return nil, err
}
var mBody map[string]interface{}
if err := json.Unmarshal(bBody, &mBody); err != nil {
return nil, err
}
bEmail, err := json.Marshal(struct {
Sender Sender `json:"sender,omitempty"`
To []Recipient `json:"to"`
CC []Recipient `json:"cc,omitempty"`
BCC []Recipient `json:"bcc,omitempty"`
Subject string `json:"subject,omitempty"`
ReplyTo Sender `json:"replyTo"`
Attachments []Attachment `json:"attachment,omitempty"`
Tags []Tag `json:"tags,omitempty"`
}{
Sender: e.from,
To: e.to,
CC: e.cc,
BCC: e.bcc,
Subject: e.subject,
ReplyTo: e.replyTo,
Attachments: e.attachments,
Tags: e.tags,
})
if err != nil {
return nil, err
}
var mEmail map[string]interface{}
if err := json.Unmarshal(bEmail, &mEmail); err != nil {
return nil, err
}
for k, v := range mBody {
mEmail[k] = v
}
return json.Marshal(mEmail)
}
type EmailBuilder struct {
email *Email
}
func NewEmailBuilder() *EmailBuilder {
return &EmailBuilder{&Email{}}
}
func (b *EmailBuilder) From(sender Sender) *EmailBuilder {
b.email.from = sender
return b
}
func (b *EmailBuilder) AppendTo(recipient Recipient) *EmailBuilder {
b.email.to = append(b.email.to, recipient)
return b
}
func (b *EmailBuilder) AppendCC(recipient Recipient) *EmailBuilder {
b.email.cc = append(b.email.cc, recipient)
return b
}
func (b *EmailBuilder) AppendBCC(recipient Recipient) *EmailBuilder {
b.email.bcc = append(b.email.bcc, recipient)
return b
}
func (b *EmailBuilder) Body() *EmailBodyBuilder {
return &EmailBodyBuilder{*b}
}
func (b *EmailBuilder) Subject(subject string) *EmailBuilder {
b.email.subject = subject
return b
}
func (b *EmailBuilder) ReplyTo(recipient Recipient) *EmailBuilder {
b.email.replyTo = recipient
return b
}
func (b *EmailBuilder) AppendAttachment(attachment Attachment) *EmailBuilder {
b.email.attachments = append(b.email.attachments, attachment)
return b
}
func (b *EmailBuilder) AppendTag(tag Tag) *EmailBuilder {
b.email.tags = append(b.email.tags, tag)
return b
}
func (b *EmailBuilder) Build() *Email {
return b.email
}
type EmailBodyBuilder struct {
EmailBuilder
}
func (b *EmailBodyBuilder) HTML(html string) *EmailBodyBuilder {
b.email.body = NewHTMLContent(html)
return b
}
func (b *EmailBodyBuilder) PlainText(text string) *EmailBodyBuilder {
b.email.body = NewPlainTextContent(text)
return b
}
func (b *EmailBodyBuilder) Template(id int, opts ...TemplateContentOption) *EmailBodyBuilder {
b.email.body = NewTemplateContent(id, opts...)
return b
}