| 1 | package mailyak |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "net/http" |
| 8 | "net/textproto" |
| 9 | ) |
| 10 | |
| 11 | // DetectContentType needs at most 512 bytes |
| 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 | // Attach adds the contents of r to the email as an attachment with name as the |
| 30 | // filename. |
| 31 | // |
| 32 | // r is not read until Send is called and the MIME type will be detected |
| 33 | // using https://golang.org/pkg/net/http/#DetectContentType |
| 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 | // AttachWithMimeType adds the contents of r to the email as an attachment with |
| 43 | // name as the filename and mimeType as the specified MIME type of the content. |
| 44 | // It is up to the user to ensure the mimeType is correct. |
| 45 | // |
| 46 | // r is not read until Send is called. |
| 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 | // AttachInline adds the contents of r to the email as an inline attachment. |
| 57 | // Inline attachments are typically used within the email body, such as a logo |
| 58 | // or header image. It is up to the user to ensure name is unique. |
| 59 | // |
| 60 | // Files can be referenced by their name within the email using the cid URL |
| 61 | // protocol: |
| 62 | // |
| 63 | // <img src="cid:myFileName"/> |
| 64 | // |
| 65 | // r is not read until Send is called and the MIME type will be detected |
| 66 | // using https://golang.org/pkg/net/http/#DetectContentType |
| 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 | // AttachInlineWithMimeType adds the contents of r to the email as an inline attachment |
| 76 | // with mimeType as the specified MIME type of the content. Inline attachments are |
| 77 | // typically used within the email body, such as a logo or header image. It is up to the |
| 78 | // user to ensure name is unique and the specified mimeType is correct. |
| 79 | // |
| 80 | // Files can be referenced by their name within the email using the cid URL |
| 81 | // protocol: |
| 82 | // |
| 83 | // <img src="cid:myFileName"/> |
| 84 | // |
| 85 | // r is not read until Send is called. |
| 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 | // ClearAttachments removes all current attachments. |
| 96 | func (m *MailYak) ClearAttachments() { |
| 97 | m.attachments = []attachment{} |
| 98 | } |
| 99 | |
| 100 | // writeAttachments loops over the attachments, guesses their content-type and |
| 101 | // writes the data as a line-broken base64 string (using the splitter mutator). |
| 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 | // More to write? |
| 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 | } |