-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
209 lines (167 loc) · 5.21 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
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
package gomail
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"log"
"mime/multipart"
"strings"
"unicode/utf8"
)
var (
NewLine string = "\r\n"
)
type Message struct {
From string
ReplyTo string
To []string
Cc []string
Bcc []string
Subject string
PlainBody *bytes.Buffer
HtmlBody *bytes.Buffer
}
// NewTextMessage create a plain text message
func NewTextMessage(to []string, subject string, body string) *Message {
return &Message{To: to, Subject: subject, PlainBody: bytes.NewBufferString(body)}
}
// NewHtmlMessage create a html message
func NewHtmlMessage(to []string, subject string, body string) *Message {
return &Message{To: to, Subject: subject, HtmlBody: bytes.NewBufferString(body)}
}
// NewTextAndHtmlMessage create a message contains both plain text and html message
func NewTextAndHtmlMessage(to []string, subject string, plainBody string, htmlBody string) *Message {
return &Message{To: to, Subject: subject, PlainBody: bytes.NewBufferString(plainBody), HtmlBody: bytes.NewBufferString(htmlBody)}
}
// RenderData render the whole email body
func (m *Message) RenderData() (data []byte, err error) {
if m.HtmlBody == nil && m.PlainBody == nil {
err = errors.New("HtmlBody and PlainBody can not both be blank")
return
}
switch {
case m.HtmlBody == nil, m.HtmlBody.Len() == 0:
data, err = m.renderSingleContentType("text/plain", m.PlainBody)
case m.PlainBody == nil, m.PlainBody.Len() == 0:
data, err = m.renderSingleContentType("text/html", m.HtmlBody)
default:
data, err = m.renderPlainAndHtmlText()
}
return
}
// RenderTemplate renders the message body from the template and input parameters, the change is inline the message
func (m *Message) RenderTemplate(templatePath string, args map[string]interface{}) error {
m.HtmlBody = m.renderViewTemplate(templatePath+".html", args)
m.PlainBody = m.renderViewTemplate(templatePath+".txt", args)
if m.HtmlBody == nil && m.PlainBody == nil {
return errors.New("Both HTML body and Plain body are blank.")
}
return nil
}
func (m *Message) renderViewTemplate(templateFilePath string, args map[string]interface{}) *bytes.Buffer {
var b bytes.Buffer
err := MailTemplate.ExecuteTemplate(&b, templateFilePath, args)
if err != nil {
log.Panicf("execution failed: %s", err)
}
return &b
}
func (m *Message) renderSingleContentType(contentType string, bodyText *bytes.Buffer) ([]byte, error) {
var b bytes.Buffer
m.writeRecipient(&b)
if err := m.writeMIME(&b); err != nil {
return nil, err
}
if err := m.writeContentType(&b, contentType); err != nil {
return nil, err
}
if _, err := b.Write(bodyText.Bytes()); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (m *Message) renderPlainAndHtmlText() ([]byte, error) {
writer := multipart.NewWriter(bytes.NewBufferString(""))
var b bytes.Buffer
m.writeRecipient(&b)
if err := m.writeMIME(&b); err != nil {
return nil, err
}
if err := m.writeMultipartStart(&b, writer); err != nil {
return nil, err
}
if err := m.writeContentTypeWithBoundary(&b, writer, "text/plain"); err != nil {
return nil, err
}
if _, err := b.Write(m.PlainBody.Bytes()); err != nil {
return nil, err
}
if err := m.writeContentTypeWithBoundary(&b, writer, "text/html"); err != nil {
return nil, err
}
if _, err := b.Write(m.HtmlBody.Bytes()); err != nil {
return nil, err
}
if err := m.writeMultipartEnd(&b, writer); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (m *Message) writeRecipient(b *bytes.Buffer) {
if m.From != "" {
fmt.Fprintf(b, "From: %s %s", m.From, NewLine)
}
if m.ReplyTo != "" {
fmt.Fprintf(b, "Reply-To: %s %s", m.ReplyTo, NewLine)
}
if len(m.To) > 0 {
fmt.Fprintf(b, "To: %s %s", strings.Join(m.To, ", "), NewLine)
}
if len(m.Cc) > 0 {
fmt.Fprintf(b, "Cc: %s %s", strings.Join(m.Cc, ", "), NewLine)
}
if m.Subject != "" {
subjectBytes := []byte(m.Subject)
if len(subjectBytes) == utf8.RuneCount(subjectBytes) {
fmt.Fprintf(b, "Subject: %s %s", m.Subject, NewLine)
} else {
fmt.Fprintf(b, "Subject: =?UTF-8?B?%s?= %s", base64.StdEncoding.EncodeToString(subjectBytes), NewLine)
}
}
}
func (m *Message) writeMIME(b *bytes.Buffer) error {
_, err := b.WriteString("MIME-Version: 1.0")
return err
}
func (m *Message) writeContentType(b *bytes.Buffer, contentType string) error {
contentTypeFormat := `
Content-Type: %s; charset="UTF-8";
Content-Transfer-Encoding: 8bit
`
_, err := b.WriteString(fmt.Sprintf(contentTypeFormat, contentType))
return err
}
func (m *Message) writeMultipartStart(b *bytes.Buffer, writer *multipart.Writer) error {
multipart := `
Content-Type: multipart/alternative; charset="UTF-8"; boundary="%s"
`
_, err := b.WriteString(fmt.Sprintf(multipart, writer.Boundary()))
return err
}
func (m *Message) writeMultipartEnd(b *bytes.Buffer, writer *multipart.Writer) error {
multipart := `
--%s--
`
_, err := b.WriteString(fmt.Sprintf(multipart, writer.Boundary()))
return err
}
func (m *Message) writeContentTypeWithBoundary(b *bytes.Buffer, writer *multipart.Writer, contentType string) error {
contentTypeFormat := `
--%s
Content-Type: %s; charset=UTF-8;
Content-Transfer-Encoding: 8bit
`
_, err := b.WriteString(fmt.Sprintf(contentTypeFormat, writer.Boundary(), contentType))
return err
}