/
constructor.go
150 lines (127 loc) · 5.81 KB
/
constructor.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package email
import (
"io"
"io/ioutil"
"mime"
"path/filepath"
)
// NewMessage will create a multipart email containing plain text, html,
// and optionally attachments (create attachments with NewPartAttachment).
// Example structure with 2 pdf attachments:
// * multipart/mixed
// * * multipart/alternative
// * * * text/plain
// * * * text/html
// * * application/pdf (attachment)
// * * application/pdf (attachment)
func NewMessage(headers Header, textPlain string, html string, attachments ...*Message) *Message {
headers.Set("Content-Type", "multipart/mixed; boundary=\""+randomBoundary()+"\"")
alternativePart := NewPartMultipart("alternative", NewPartText(textPlain), NewPartHTML(html))
parts := make([]*Message, 0, 1+len(attachments))
parts = append(parts, alternativePart)
parts = append(parts, attachments...)
return &Message{Header: headers, Parts: parts}
}
// NewMessageWithInlines will create a multipart email containing plain text, html,
// and optionally attachments (create attachments with NewPartAttachment),
// where the html part contains inline parts, such as inline images
// (create inline parts with NewPartInline).
// Example structure with 2 inline jpeg's and 2 pdf attachments:
// * multipart/mixed
// * * multipart/alternative
// * * * text/plain
// * * * multipart/related
// * * * * text/html
// * * * * image/jpeg (inline with Content-ID)
// * * * * image/jpeg (inline with Content-ID)
// * * application/pdf (attachment)
// * * application/pdf (attachment)
func NewMessageWithInlines(headers Header, textPlain string, html string, inlines []*Message, attachments ...*Message) *Message {
headers.Set("Content-Type", "multipart/mixed; boundary=\""+randomBoundary()+"\"")
inlineParts := []*Message{NewPartHTML(html)}
inlineParts = append(inlineParts, inlines...)
relatedPart := NewPartMultipart("related", inlineParts...)
alternativePart := NewPartMultipart("alternative", NewPartText(textPlain), relatedPart)
parts := make([]*Message, 0, 1+len(attachments))
parts = append(parts, alternativePart)
parts = append(parts, attachments...)
return &Message{Header: headers, Parts: parts}
}
// NewPartMultipart will create a multipart part, optionally filled with sub-parts.
// Common values for parameter multipartSubType are: mixed, alternative, related, and report.
// Example: if "mixed" is passed in as multipartSubType, then a "multipart/mixed" part is created.
func NewPartMultipart(multipartSubType string, parts ...*Message) *Message {
return &Message{
Header: Header{"Content-Type": []string{"multipart/" + multipartSubType + "; boundary=\"" + randomBoundary() + "\""}},
Parts: parts}
}
// NewPartText creates a "text/plain" part, with the text string as its content
// (do not encode, this will happen automatically when needed).
func NewPartText(textPlain string) *Message {
return &Message{
Header: Header{"Content-Type": []string{"text/plain; charset=\"UTF-8\""}},
Body: []byte(textPlain)}
}
// NewPartHTML creates a "text/html" part, with the html string as its content
// (do not encode, this will happen automatically when needed).
func NewPartHTML(html string) *Message {
return &Message{
Header: Header{"Content-Type": []string{"text/html; charset=\"UTF-8\""}},
Body: []byte(html)}
}
// NewPartAttachment creates an attachment part,
// using the filename's mime type, and with the reader's content
// (do not encode, this will happen automatically when needed).
func NewPartAttachment(r io.Reader, filename string) (*Message, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return NewPartAttachmentFromBytes(b, filename), nil
}
// NewPartAttachmentFromBytes creates an attachment part,
// using the filename's mime type, and with the bytes as its content
// (do not encode, this will happen automatically when needed).
func NewPartAttachmentFromBytes(raw []byte, filename string) *Message {
return newPartFromBytes(raw, mime.TypeByExtension(filepath.Ext(filename)), "attachment; filename=\""+filename+"\"", "")
}
// NewPartInline creates an inline part,
// using the filename's mime type, specified Content-ID
// (do not wrap with angle brackets), and with the reader's content
// (do not encode, this will happen automatically when needed).
func NewPartInline(r io.Reader, filename string, contentID string) (*Message, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return NewPartInlineFromBytes(b, filename, contentID), nil
}
// NewPartInlineFromBytes creates an inline part,
// using the filename's mime type, specified Content-ID
// (do not wrap with angle brackets), and with the bytes as its content
// (do not encode, this will happen automatically when needed).
func NewPartInlineFromBytes(raw []byte, filename string, contentID string) *Message {
return newPartFromBytes(raw, mime.TypeByExtension(filepath.Ext(filename)), "inline; filename=\""+filename+"\"", contentID)
}
// newPartFromBytes creates a generic binary part,
// using specified contentType, Content-Disposition, Content-ID
// (do not wrap with angle brackets), and with the bytes as its content
// (do not encode, this will happen automatically when needed).
func newPartFromBytes(raw []byte, contentType string, contentDisposition string, contentID string) *Message {
headers := Header{}
if len(contentType) > 0 {
headers.Set("Content-Type", contentType)
} else {
headers.Set("Content-Type", "application/octet-stream")
}
if len(contentDisposition) > 0 {
headers.Set("Content-Disposition", contentDisposition)
}
if len(contentID) > 0 {
headers.Set("Content-ID", "<"+contentID+">")
}
return &Message{Header: headers, Body: raw}
}