forked from mailtarget/mailtarget-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
133 lines (113 loc) · 2.98 KB
/
message.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
package layang
import (
"errors"
"fmt"
)
type Message struct {
Subject string `json:"subject"`
From Address `json:"from"`
ReplyTo []Address `json:"replyTo"`
To []Address `json:"to"`
Cc []Address `json:"cc"`
Bcc []Address `json:"bcc"`
BodyText string `json:"bodyText"`
BodyHtml string `json:"bodyHtml"`
Headers []Header `json:"headers"`
Attachment []Attachment `json:"attachment"`
Metadata map[string]string `json:"metadata"`
OptionsAttributes OptionsAttributes `json:"optionsAttributes"`
}
type Recipient struct {
Address Address `json:"address"`
}
type Address struct {
Email string `json:"email"`
Name string `json:"name"`
}
type Attachment struct {
MimeType string `json:"mimeType"`
Filename string `json:"filename"`
Value string `json:"value"`
}
type OptionsAttributes struct {
ClickTracking bool `json:"clickTracking"`
OpenTracking bool `json:"openTracking"`
}
type Header struct {
Name string `json:"name"`
Value string `json:"value"`
}
func (l *Layang) NewMessage(subject, text, html string, from Address, to []Address) *Message {
return &Message{
Subject: subject,
BodyText: text,
BodyHtml: html,
To: to,
From: from,
}
}
func (l *Message) IsValid() error {
if l == nil || (l == &Message{}) {
return errors.New("message is empty")
}
if l.From.Email == "" {
return errors.New("from is empty")
}
if l.BodyText == "" && l.BodyHtml == "" {
return errors.New("text or html is empty")
}
if !IsEmail(l.From.Email) {
return errors.New(l.From.Email + " is not valid email address")
}
if len(l.To) == 0 {
return errors.New("to is empty")
}
for i := range l.To {
if !IsEmail(l.To[i].Email) {
return errors.New(l.To[i].Email + " is not valid email address")
}
}
for i := range l.Cc {
if !IsEmail(l.Cc[i].Email) {
return errors.New(l.Cc[i].Email + " is not valid email address")
}
}
for i := range l.Bcc {
if !IsEmail(l.Bcc[i].Email) {
return errors.New(l.Bcc[i].Email + " is not valid email address")
}
}
for i := range l.ReplyTo {
if !IsEmail(l.ReplyTo[i].Email) {
return errors.New(l.ReplyTo[i].Email + " is not valid email address")
}
}
if !IsHTML(l.BodyHtml) {
fmt.Println("not valid")
return errors.New("not valid html")
} else {
fmt.Println("valid")
}
return nil
}
func (l *Message) SetReplyTo(replyTo []Address){
l.ReplyTo = replyTo
}
func (l *Message) SetCC(cc []Address) {
l.Cc = cc
}
func (l *Message) SetBCC(bcc []Address) {
l.Bcc = bcc
}
func (l *Message) SetHeaders(headers []Header) {
l.Headers = headers
}
func (l *Message) SetAttachment(attachment []Attachment) {
l.Attachment = attachment
}
func (l *Message) SetMetadata(metadata map[string]string) {
l.Metadata = metadata
}
func (l *Message) setOptionsAttributes(attributes OptionsAttributes) {
l.OptionsAttributes = attributes
}