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