-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemail.go
178 lines (144 loc) · 4.7 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
package mailersend
import (
"context"
"net/http"
)
const emailBasePath = "/email"
type EmailService service
const (
DispositionInline = "inline"
DispositionAttachment = "attachment"
)
// Message structures contain both the message text and the envelop for an e-mail message.
type Message struct {
Recipients []Recipient `json:"to"`
From From `json:"from"`
CC []Recipient `json:"cc,omitempty"`
Bcc []Recipient `json:"bcc,omitempty"`
ReplyTo ReplyTo `json:"reply_to,omitempty"`
InReplyTo string `json:"in_reply_to,omitempty"`
Subject string `json:"subject,omitempty"`
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
TemplateID string `json:"template_id,omitempty"`
SendAt int64 `json:"send_at,omitempty"`
Tags []string `json:"tags,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
TemplateVariables []Variables `json:"variables"`
Personalization []Personalization `json:"personalization"`
}
// From - simple struct to declare from name/ email
type From = Recipient
// ReplyTo - simple struct to declare from name/ email
type ReplyTo = Recipient
// Recipient - you can set multiple recipients
type Recipient struct {
Name string `json:"name"`
Email string `json:"email"`
}
// Deprecated: Variables - you can set multiple Substitutions for each Recipient
type Variables struct {
Email string `json:"email"`
Substitutions []Substitution `json:"substitutions"`
}
// Deprecated: Substitution - you can set multiple Substitutions for each Recipient
type Substitution struct {
Var string `json:"var"`
Value string `json:"value"`
}
// Personalization - you can set multiple Personalization for each Recipient
type Personalization struct {
Email string `json:"email"`
Data map[string]interface{} `json:"data"`
}
// Attachment - you can set multiple Attachments
type Attachment struct {
Content string `json:"content"`
Filename string `json:"filename"`
Disposition string `json:"disposition,omitempty"`
ID string `json:"id,omitempty"`
}
// Deprecated: NewMessage - Setup a new message ready to be sent
func (ms *Mailersend) NewMessage() *Message {
return &Message{}
}
// NewMessage - Setup a new email message ready to be sent.
func (s *EmailService) NewMessage() *Message {
return &Message{}
}
// SetFrom - Set from.
func (m *Message) SetFrom(from From) {
m.From = from
}
// SetRecipients - Set all the recipients.
func (m *Message) SetRecipients(recipients []Recipient) {
m.Recipients = recipients
}
// SetCc - Set CC.
func (m *Message) SetCc(cc []Recipient) {
m.CC = cc
}
// SetBcc - Set Bcc.
func (m *Message) SetBcc(bcc []Recipient) {
m.Bcc = bcc
}
// SetReplyTo - Set ReplyTo.
func (m *Message) SetReplyTo(replyTo Recipient) {
m.ReplyTo = replyTo
}
// SetInReplyTo - Set InReplyTo.
func (m *Message) SetInReplyTo(inReplyTo string) {
m.InReplyTo = inReplyTo
}
// SetSubject - Set the subject of the email, required if not using a template.
func (m *Message) SetSubject(subject string) {
m.Subject = subject
}
// SetHTML - Set the html content of the email, required if not using a template.
func (m *Message) SetHTML(html string) {
m.HTML = html
}
// SetText - Set the text content of the email, required if not using a template.
func (m *Message) SetText(text string) {
m.Text = text
}
// SetTemplateID - Set the template ID.
func (m *Message) SetTemplateID(templateID string) {
m.TemplateID = templateID
}
// Deprecated: SetSubstitutions - Set the template substitutions.
func (m *Message) SetSubstitutions(variables []Variables) {
m.TemplateVariables = variables
}
// SetPersonalization - Set the template personalization.
func (m *Message) SetPersonalization(personalization []Personalization) {
m.Personalization = personalization
}
// SetTags - Set all the tags.
func (m *Message) SetTags(tags []string) {
m.Tags = tags
}
// AddAttachment - Add an attachment base64 encoded content.
func (m *Message) AddAttachment(attachment Attachment) {
m.Attachments = append(m.Attachments, attachment)
}
// SetSendAt - Set send_at.
func (m *Message) SetSendAt(sendAt int64) {
m.SendAt = sendAt
}
// Deprecated: Send - send the message
func (ms *Mailersend) Send(ctx context.Context, message *Message) (*Response, error) {
req, err := ms.newRequest(http.MethodPost, emailBasePath, message)
if err != nil {
return nil, err
}
return ms.do(ctx, req, nil)
}
// Send - send the message
func (s *EmailService) Send(ctx context.Context, message *Message) (*Response, error) {
req, err := s.client.newRequest(http.MethodPost, emailBasePath, message)
if err != nil {
return nil, err
}
return s.client.do(ctx, req, nil)
}