Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 1 | package parsemail |
| 2 | |
| 3 | import ( |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 4 | "bytes" |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 5 | "encoding/base64" |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 6 | "fmt" |
| 7 | "io" |
| 8 | "io/ioutil" |
| 9 | "mime" |
| 10 | "mime/multipart" |
| 11 | "net/mail" |
| 12 | "strings" |
| 13 | "time" |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 14 | ) |
| 15 | |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 16 | const contentTypeMultipartMixed = "multipart/mixed" |
| 17 | const contentTypeMultipartAlternative = "multipart/alternative" |
| 18 | const contentTypeMultipartRelated = "multipart/related" |
| 19 | const contentTypeTextHtml = "text/html" |
| 20 | const contentTypeTextPlain = "text/plain" |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 21 | |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 22 | // Parse an email message read from io.Reader into parsemail.Email struct |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 23 | func Parse(r io.Reader) (email Email, err error) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 24 | msg, err := mail.ReadMessage(r) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 25 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 26 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 29 | email, err = createEmailFromHeader(msg.Header) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 30 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 31 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 32 | } |
| 33 | |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 34 | email.ContentType = msg.Header.Get("Content-Type") |
| 35 | contentType, params, err := parseContentType(email.ContentType) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 36 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 37 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 40 | switch contentType { |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 41 | case contentTypeMultipartMixed: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 42 | email.TextBody, email.HTMLBody, email.Attachments, email.EmbeddedFiles, err = parseMultipartMixed(msg.Body, params["boundary"]) |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 43 | case contentTypeMultipartAlternative: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 44 | email.TextBody, email.HTMLBody, email.EmbeddedFiles, err = parseMultipartAlternative(msg.Body, params["boundary"]) |
Obi Symons | 89230f4 | 2020-04-04 14:32:11 +1100 | [diff] [blame] | 45 | case contentTypeMultipartRelated: |
| 46 | email.TextBody, email.HTMLBody, email.EmbeddedFiles, err = parseMultipartRelated(msg.Body, params["boundary"]) |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 47 | case contentTypeTextPlain: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 48 | message, _ := ioutil.ReadAll(msg.Body) |
| 49 | email.TextBody = strings.TrimSuffix(string(message[:]), "\n") |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 50 | case contentTypeTextHtml: |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 51 | message, _ := ioutil.ReadAll(msg.Body) |
| 52 | email.HTMLBody = strings.TrimSuffix(string(message[:]), "\n") |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 53 | default: |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 54 | email.Content, err = decodeContent(msg.Body, msg.Header.Get("Content-Transfer-Encoding")) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 57 | return |
| 58 | } |
| 59 | |
| 60 | func createEmailFromHeader(header mail.Header) (email Email, err error) { |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 61 | hp := headerParser{header: &header} |
| 62 | |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 63 | email.Subject = decodeMimeSentence(header.Get("Subject")) |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 64 | email.From = hp.parseAddressList(header.Get("From")) |
| 65 | email.Sender = hp.parseAddress(header.Get("Sender")) |
| 66 | email.ReplyTo = hp.parseAddressList(header.Get("Reply-To")) |
| 67 | email.To = hp.parseAddressList(header.Get("To")) |
| 68 | email.Cc = hp.parseAddressList(header.Get("Cc")) |
| 69 | email.Bcc = hp.parseAddressList(header.Get("Bcc")) |
| 70 | email.Date = hp.parseTime(header.Get("Date")) |
| 71 | email.ResentFrom = hp.parseAddressList(header.Get("Resent-From")) |
| 72 | email.ResentSender = hp.parseAddress(header.Get("Resent-Sender")) |
| 73 | email.ResentTo = hp.parseAddressList(header.Get("Resent-To")) |
| 74 | email.ResentCc = hp.parseAddressList(header.Get("Resent-Cc")) |
| 75 | email.ResentBcc = hp.parseAddressList(header.Get("Resent-Bcc")) |
| 76 | email.ResentMessageID = hp.parseMessageId(header.Get("Resent-Message-ID")) |
| 77 | email.MessageID = hp.parseMessageId(header.Get("Message-ID")) |
| 78 | email.InReplyTo = hp.parseMessageIdList(header.Get("In-Reply-To")) |
| 79 | email.References = hp.parseMessageIdList(header.Get("References")) |
| 80 | email.ResentDate = hp.parseTime(header.Get("Resent-Date")) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 81 | |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 82 | if hp.err != nil { |
| 83 | err = hp.err |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 84 | return |
| 85 | } |
| 86 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 87 | //decode whole header for easier access to extra fields |
| 88 | //todo: should we decode? aren't only standard fields mime encoded? |
| 89 | email.Header, err = decodeHeaderMime(header) |
| 90 | if err != nil { |
| 91 | return |
| 92 | } |
| 93 | |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | func parseContentType(contentTypeHeader string) (contentType string, params map[string]string, err error) { |
| 98 | if contentTypeHeader == "" { |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 99 | contentType = contentTypeTextPlain |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 100 | return |
| 101 | } |
| 102 | |
| 103 | return mime.ParseMediaType(contentTypeHeader) |
| 104 | } |
| 105 | |
Kevin Chen | 5dc5bc8 | 2018-05-03 22:07:35 -0400 | [diff] [blame] | 106 | func parseMultipartRelated(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) { |
| 107 | pmr := multipart.NewReader(msg, boundary) |
| 108 | for { |
| 109 | part, err := pmr.NextPart() |
| 110 | |
| 111 | if err == io.EOF { |
| 112 | break |
| 113 | } else if err != nil { |
| 114 | return textBody, htmlBody, embeddedFiles, err |
| 115 | } |
| 116 | |
| 117 | contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type")) |
| 118 | if err != nil { |
| 119 | return textBody, htmlBody, embeddedFiles, err |
| 120 | } |
| 121 | |
| 122 | switch contentType { |
| 123 | case contentTypeTextPlain: |
| 124 | ppContent, err := ioutil.ReadAll(part) |
| 125 | if err != nil { |
| 126 | return textBody, htmlBody, embeddedFiles, err |
| 127 | } |
| 128 | |
| 129 | textBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
| 130 | case contentTypeTextHtml: |
| 131 | ppContent, err := ioutil.ReadAll(part) |
| 132 | if err != nil { |
| 133 | return textBody, htmlBody, embeddedFiles, err |
| 134 | } |
| 135 | |
| 136 | htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
| 137 | case contentTypeMultipartAlternative: |
| 138 | tb, hb, ef, err := parseMultipartAlternative(part, params["boundary"]) |
| 139 | if err != nil { |
| 140 | return textBody, htmlBody, embeddedFiles, err |
| 141 | } |
| 142 | |
| 143 | htmlBody += hb |
| 144 | textBody += tb |
| 145 | embeddedFiles = append(embeddedFiles, ef...) |
| 146 | default: |
| 147 | if isEmbeddedFile(part) { |
| 148 | ef, err := decodeEmbeddedFile(part) |
| 149 | if err != nil { |
| 150 | return textBody, htmlBody, embeddedFiles, err |
| 151 | } |
| 152 | |
| 153 | embeddedFiles = append(embeddedFiles, ef) |
| 154 | } else { |
| 155 | return textBody, htmlBody, embeddedFiles, fmt.Errorf("Can't process multipart/related inner mime type: %s", contentType) |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return textBody, htmlBody, embeddedFiles, err |
| 161 | } |
| 162 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 163 | func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) { |
| 164 | pmr := multipart.NewReader(msg, boundary) |
| 165 | for { |
| 166 | part, err := pmr.NextPart() |
| 167 | |
| 168 | if err == io.EOF { |
| 169 | break |
| 170 | } else if err != nil { |
| 171 | return textBody, htmlBody, embeddedFiles, err |
| 172 | } |
| 173 | |
| 174 | contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type")) |
Dusan Kasan | c661cc0 | 2017-04-18 10:51:51 +0200 | [diff] [blame] | 175 | if err != nil { |
| 176 | return textBody, htmlBody, embeddedFiles, err |
| 177 | } |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 178 | |
| 179 | switch contentType { |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 180 | case contentTypeTextPlain: |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 181 | ppContent, err := ioutil.ReadAll(part) |
| 182 | if err != nil { |
| 183 | return textBody, htmlBody, embeddedFiles, err |
| 184 | } |
| 185 | |
| 186 | textBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 187 | case contentTypeTextHtml: |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 188 | ppContent, err := ioutil.ReadAll(part) |
| 189 | if err != nil { |
| 190 | return textBody, htmlBody, embeddedFiles, err |
| 191 | } |
| 192 | |
| 193 | htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 194 | case contentTypeMultipartRelated: |
Kevin Chen | 9b9506a | 2018-05-03 22:17:38 -0400 | [diff] [blame] | 195 | tb, hb, ef, err := parseMultipartRelated(part, params["boundary"]) |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 196 | if err != nil { |
| 197 | return textBody, htmlBody, embeddedFiles, err |
| 198 | } |
| 199 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 200 | htmlBody += hb |
| 201 | textBody += tb |
| 202 | embeddedFiles = append(embeddedFiles, ef...) |
| 203 | default: |
| 204 | if isEmbeddedFile(part) { |
| 205 | ef, err := decodeEmbeddedFile(part) |
| 206 | if err != nil { |
| 207 | return textBody, htmlBody, embeddedFiles, err |
| 208 | } |
| 209 | |
| 210 | embeddedFiles = append(embeddedFiles, ef) |
| 211 | } else { |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 212 | return textBody, htmlBody, embeddedFiles, fmt.Errorf("Can't process multipart/alternative inner mime type: %s", contentType) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return textBody, htmlBody, embeddedFiles, err |
| 218 | } |
| 219 | |
| 220 | func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody string, attachments []Attachment, embeddedFiles []EmbeddedFile, err error) { |
| 221 | mr := multipart.NewReader(msg, boundary) |
| 222 | for { |
| 223 | part, err := mr.NextPart() |
| 224 | if err == io.EOF { |
| 225 | break |
| 226 | } else if err != nil { |
| 227 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 228 | } |
| 229 | |
| 230 | contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type")) |
| 231 | if err != nil { |
| 232 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 233 | } |
| 234 | |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 235 | if contentType == contentTypeMultipartAlternative { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 236 | textBody, htmlBody, embeddedFiles, err = parseMultipartAlternative(part, params["boundary"]) |
| 237 | if err != nil { |
| 238 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 239 | } |
Kevin Chen | 5dc5bc8 | 2018-05-03 22:07:35 -0400 | [diff] [blame] | 240 | } else if contentType == contentTypeMultipartRelated { |
| 241 | textBody, htmlBody, embeddedFiles, err = parseMultipartRelated(part, params["boundary"]) |
| 242 | if err != nil { |
| 243 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 244 | } |
Maya Rashish | a3803bd | 2019-06-08 17:53:21 +0300 | [diff] [blame] | 245 | } else if contentType == contentTypeTextPlain { |
| 246 | ppContent, err := ioutil.ReadAll(part) |
| 247 | if err != nil { |
| 248 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 249 | } |
| 250 | |
| 251 | textBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
k-yomo | 2e670d9 | 2020-04-30 22:42:48 +0900 | [diff] [blame] | 252 | } else if contentType == contentTypeTextHtml { |
| 253 | ppContent, err := ioutil.ReadAll(part) |
| 254 | if err != nil { |
| 255 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 256 | } |
| 257 | |
| 258 | htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n") |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 259 | } else if isAttachment(part) { |
| 260 | at, err := decodeAttachment(part) |
| 261 | if err != nil { |
| 262 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 263 | } |
| 264 | |
| 265 | attachments = append(attachments, at) |
| 266 | } else { |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 267 | return textBody, htmlBody, attachments, embeddedFiles, fmt.Errorf("Unknown multipart/mixed nested mime type: %s", contentType) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | return textBody, htmlBody, attachments, embeddedFiles, err |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 274 | func decodeMimeSentence(s string) string { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 275 | result := []string{} |
| 276 | ss := strings.Split(s, " ") |
| 277 | |
| 278 | for _, word := range ss { |
| 279 | dec := new(mime.WordDecoder) |
| 280 | w, err := dec.Decode(word) |
| 281 | if err != nil { |
| 282 | if len(result) == 0 { |
| 283 | w = word |
| 284 | } else { |
| 285 | w = " " + word |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | result = append(result, w) |
| 290 | } |
| 291 | |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 292 | return strings.Join(result, "") |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 295 | func decodeHeaderMime(header mail.Header) (mail.Header, error) { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 296 | parsedHeader := map[string][]string{} |
| 297 | |
| 298 | for headerName, headerData := range header { |
| 299 | |
| 300 | parsedHeaderData := []string{} |
| 301 | for _, headerValue := range headerData { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 302 | parsedHeaderData = append(parsedHeaderData, decodeMimeSentence(headerValue)) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | parsedHeader[headerName] = parsedHeaderData |
| 306 | } |
| 307 | |
| 308 | return mail.Header(parsedHeader), nil |
| 309 | } |
| 310 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 311 | func isEmbeddedFile(part *multipart.Part) bool { |
| 312 | return part.Header.Get("Content-Transfer-Encoding") != "" |
| 313 | } |
| 314 | |
| 315 | func decodeEmbeddedFile(part *multipart.Part) (ef EmbeddedFile, err error) { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 316 | cid := decodeMimeSentence(part.Header.Get("Content-Id")) |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 317 | decoded, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding")) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 318 | if err != nil { |
| 319 | return |
| 320 | } |
| 321 | |
| 322 | ef.CID = strings.Trim(cid, "<>") |
| 323 | ef.Data = decoded |
| 324 | ef.ContentType = part.Header.Get("Content-Type") |
| 325 | |
| 326 | return |
| 327 | } |
| 328 | |
| 329 | func isAttachment(part *multipart.Part) bool { |
| 330 | return part.FileName() != "" |
| 331 | } |
| 332 | |
| 333 | func decodeAttachment(part *multipart.Part) (at Attachment, err error) { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 334 | filename := decodeMimeSentence(part.FileName()) |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 335 | decoded, err := decodeContent(part, part.Header.Get("Content-Transfer-Encoding")) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 336 | if err != nil { |
| 337 | return |
| 338 | } |
| 339 | |
| 340 | at.Filename = filename |
| 341 | at.Data = decoded |
| 342 | at.ContentType = strings.Split(part.Header.Get("Content-Type"), ";")[0] |
| 343 | |
| 344 | return |
| 345 | } |
| 346 | |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 347 | func decodeContent(content io.Reader, encoding string) (io.Reader, error) { |
| 348 | switch encoding { |
| 349 | case "base64": |
| 350 | decoded := base64.NewDecoder(base64.StdEncoding, content) |
| 351 | b, err := ioutil.ReadAll(decoded) |
| 352 | if err != nil { |
| 353 | return nil, err |
| 354 | } |
| 355 | |
| 356 | return bytes.NewReader(b), nil |
Dusan Kasan | 3325e73 | 2020-04-04 11:46:04 +0200 | [diff] [blame] | 357 | case "7bit": |
| 358 | dd, err := ioutil.ReadAll(content) |
| 359 | if err != nil { |
| 360 | return nil, err |
| 361 | } |
| 362 | |
| 363 | return bytes.NewReader(dd), nil |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 364 | case "": |
| 365 | return content, nil |
| 366 | default: |
| 367 | return nil, fmt.Errorf("unknown encoding: %s", encoding) |
| 368 | } |
| 369 | } |
| 370 | |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 371 | type headerParser struct { |
| 372 | header *mail.Header |
Dusan Kasan | b974c63 | 2017-04-18 12:58:42 +0200 | [diff] [blame] | 373 | err error |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | func (hp headerParser) parseAddress(s string) (ma *mail.Address) { |
| 377 | if hp.err != nil { |
| 378 | return nil |
| 379 | } |
| 380 | |
| 381 | if strings.Trim(s, " \n") != "" { |
| 382 | ma, hp.err = mail.ParseAddress(s) |
| 383 | |
| 384 | return ma |
| 385 | } |
| 386 | |
| 387 | return nil |
| 388 | } |
| 389 | |
| 390 | func (hp headerParser) parseAddressList(s string) (ma []*mail.Address) { |
| 391 | if hp.err != nil { |
| 392 | return |
| 393 | } |
| 394 | |
| 395 | if strings.Trim(s, " \n") != "" { |
| 396 | ma, hp.err = mail.ParseAddressList(s) |
| 397 | return |
| 398 | } |
| 399 | |
| 400 | return |
| 401 | } |
| 402 | |
| 403 | func (hp headerParser) parseTime(s string) (t time.Time) { |
Dusan Kasan | b974c63 | 2017-04-18 12:58:42 +0200 | [diff] [blame] | 404 | if hp.err != nil || s == "" { |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 405 | return |
| 406 | } |
| 407 | |
Dusan Kasan | 88226cf | 2020-04-04 11:13:06 +0200 | [diff] [blame] | 408 | formats := []string{ |
| 409 | time.RFC1123Z, |
| 410 | "Mon, 2 Jan 2006 15:04:05 -0700", |
| 411 | time.RFC1123Z + " (MST)", |
| 412 | "Mon, 2 Jan 2006 15:04:05 -0700 (MST)", |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Dusan Kasan | 88226cf | 2020-04-04 11:13:06 +0200 | [diff] [blame] | 415 | for _, format := range formats { |
| 416 | t, hp.err = time.Parse(format, s) |
| 417 | if hp.err == nil { |
| 418 | return |
| 419 | } |
| 420 | } |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 421 | |
| 422 | return |
| 423 | } |
| 424 | |
| 425 | func (hp headerParser) parseMessageId(s string) string { |
| 426 | if hp.err != nil { |
| 427 | return "" |
| 428 | } |
| 429 | |
| 430 | return strings.Trim(s, "<> ") |
| 431 | } |
| 432 | |
| 433 | func (hp headerParser) parseMessageIdList(s string) (result []string) { |
| 434 | if hp.err != nil { |
| 435 | return |
| 436 | } |
| 437 | |
| 438 | for _, p := range strings.Split(s, " ") { |
| 439 | if strings.Trim(p, " \n") != "" { |
| 440 | result = append(result, hp.parseMessageId(p)) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return |
| 445 | } |
| 446 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 447 | // Attachment with filename, content type and data (as a io.Reader) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 448 | type Attachment struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 449 | Filename string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 450 | ContentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 451 | Data io.Reader |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 454 | // EmbeddedFile with content id, content type and data (as a io.Reader) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 455 | type EmbeddedFile struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 456 | CID string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 457 | ContentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 458 | Data io.Reader |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 461 | // Email with fields for all the headers defined in RFC5322 with it's attachments and |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 462 | type Email struct { |
| 463 | Header mail.Header |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 464 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 465 | Subject string |
| 466 | Sender *mail.Address |
| 467 | From []*mail.Address |
| 468 | ReplyTo []*mail.Address |
| 469 | To []*mail.Address |
| 470 | Cc []*mail.Address |
| 471 | Bcc []*mail.Address |
| 472 | Date time.Time |
| 473 | MessageID string |
| 474 | InReplyTo []string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 475 | References []string |
| 476 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 477 | ResentFrom []*mail.Address |
| 478 | ResentSender *mail.Address |
| 479 | ResentTo []*mail.Address |
| 480 | ResentDate time.Time |
| 481 | ResentCc []*mail.Address |
| 482 | ResentBcc []*mail.Address |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 483 | ResentMessageID string |
| 484 | |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 485 | ContentType string |
| 486 | Content io.Reader |
| 487 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 488 | HTMLBody string |
| 489 | TextBody string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 490 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 491 | Attachments []Attachment |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 492 | EmbeddedFiles []EmbeddedFile |
Dusan Kasan | 428369f | 2020-02-24 00:47:31 +0100 | [diff] [blame] | 493 | } |