| 1 |
package mailyak |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net/http" |
| 8 |
"net/textproto" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
const sniffLen = 512 |
| 13 |
|
| 14 |
type partCreator interface { |
| 15 |
CreatePart(header textproto.MIMEHeader) (io.Writer, error) |
| 16 |
} |
| 17 |
|
| 18 |
type writeWrapper interface { |
| 19 |
new(w io.Writer) io.Writer |
| 20 |
} |
| 21 |
|
| 22 |
type attachment struct { |
| 23 |
filename string |
| 24 |
content io.Reader |
| 25 |
inline bool |
| 26 |
mimeType string |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
func (m *MailYak) Attach(name string, r io.Reader) { |
| 35 |
m.attachments = append(m.attachments, attachment{ |
| 36 |
filename: name, |
| 37 |
content: r, |
| 38 |
inline: false, |
| 39 |
}) |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
func (m *MailYak) AttachWithMimeType(name string, r io.Reader, mimeType string) { |
| 48 |
m.attachments = append(m.attachments, attachment{ |
| 49 |
filename: name, |
| 50 |
content: r, |
| 51 |
inline: false, |
| 52 |
mimeType: mimeType, |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
func (m *MailYak) AttachInline(name string, r io.Reader) { |
| 68 |
m.attachments = append(m.attachments, attachment{ |
| 69 |
filename: name, |
| 70 |
content: r, |
| 71 |
inline: true, |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
func (m *MailYak) AttachInlineWithMimeType(name string, r io.Reader, mimeType string) { |
| 87 |
m.attachments = append(m.attachments, attachment{ |
| 88 |
filename: name, |
| 89 |
content: r, |
| 90 |
inline: true, |
| 91 |
mimeType: mimeType, |
| 92 |
}) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (m *MailYak) ClearAttachments() { |
| 97 |
m.attachments = []attachment{} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func (m *MailYak) writeAttachments(mixed partCreator, splitter writeWrapper) error { |
| 103 |
h := make([]byte, sniffLen) |
| 104 |
|
| 105 |
for _, item := range m.attachments { |
| 106 |
hLen, err := io.ReadFull(item.content, h) |
| 107 |
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { |
| 108 |
return err |
| 109 |
} |
| 110 |
|
| 111 |
if item.mimeType == "" { |
| 112 |
item.mimeType = http.DetectContentType(h[:hLen]) |
| 113 |
} |
| 114 |
|
| 115 |
ctype := fmt.Sprintf("%s;\n\tfilename=%q; name=%q", item.mimeType, item.filename, item.filename) |
| 116 |
|
| 117 |
part, err := mixed.CreatePart(getMIMEHeader(item, ctype)) |
| 118 |
if err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
|
| 122 |
encoder := base64.NewEncoder(base64.StdEncoding, splitter.new(part)) |
| 123 |
if _, err := encoder.Write(h[:hLen]); err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if hLen == sniffLen { |
| 129 |
if _, err := io.Copy(encoder, item.content); err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if err := encoder.Close(); err != nil { |
| 135 |
return err |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
func getMIMEHeader(a attachment, ctype string) textproto.MIMEHeader { |
| 143 |
var disp string |
| 144 |
var header textproto.MIMEHeader |
| 145 |
|
| 146 |
cid := fmt.Sprintf("<%s>", a.filename) |
| 147 |
if a.inline { |
| 148 |
disp = fmt.Sprintf("inline;\n\tfilename=%q; name=%q", a.filename, a.filename) |
| 149 |
header = textproto.MIMEHeader{ |
| 150 |
"Content-Type": {ctype}, |
| 151 |
"Content-Disposition": {disp}, |
| 152 |
"Content-Transfer-Encoding": {"base64"}, |
| 153 |
"Content-ID": {cid}, |
| 154 |
} |
| 155 |
} else { |
| 156 |
disp = fmt.Sprintf("attachment;\n\tfilename=%q; name=%q", a.filename, a.filename) |
| 157 |
header = textproto.MIMEHeader{ |
| 158 |
"Content-Type": {ctype}, |
| 159 |
"Content-Disposition": {disp}, |
| 160 |
"Content-Transfer-Encoding": {"base64"}, |
| 161 |
"Content-ID": {cid}, |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return header |
| 166 |
} |