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