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: |
Kevin Chen | 9b9506a | 2018-05-03 22:17:38 -0400 | [diff] [blame] | 192 | tb, hb, ef, err := parseMultipartRelated(part, params["boundary"]) |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 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 |
Edwin Love | 71e25d5 | 2019-04-02 14:07:44 +0100 | [diff] [blame^] | 305 | } else if strings.EqualFold(encoding, "7bit") { |
| 306 | dd, err := ioutil.ReadAll(part) |
| 307 | if err != nil { |
| 308 | return nil, err |
| 309 | } |
| 310 | |
| 311 | return bytes.NewReader(dd), nil |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 312 | } |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 313 | |
| 314 | return nil, fmt.Errorf("Unknown encoding: %s", encoding) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 317 | func isEmbeddedFile(part *multipart.Part) bool { |
| 318 | return part.Header.Get("Content-Transfer-Encoding") != "" |
| 319 | } |
| 320 | |
| 321 | func decodeEmbeddedFile(part *multipart.Part) (ef EmbeddedFile, err error) { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 322 | cid := decodeMimeSentence(part.Header.Get("Content-Id")) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 323 | decoded, err := decodePartData(part) |
| 324 | if err != nil { |
| 325 | return |
| 326 | } |
| 327 | |
| 328 | ef.CID = strings.Trim(cid, "<>") |
| 329 | ef.Data = decoded |
| 330 | ef.ContentType = part.Header.Get("Content-Type") |
| 331 | |
| 332 | return |
| 333 | } |
| 334 | |
| 335 | func isAttachment(part *multipart.Part) bool { |
| 336 | return part.FileName() != "" |
| 337 | } |
| 338 | |
| 339 | func decodeAttachment(part *multipart.Part) (at Attachment, err error) { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 340 | filename := decodeMimeSentence(part.FileName()) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 341 | decoded, err := decodePartData(part) |
| 342 | if err != nil { |
| 343 | return |
| 344 | } |
| 345 | |
| 346 | at.Filename = filename |
| 347 | at.Data = decoded |
| 348 | at.ContentType = strings.Split(part.Header.Get("Content-Type"), ";")[0] |
| 349 | |
| 350 | return |
| 351 | } |
| 352 | |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 353 | type headerParser struct { |
| 354 | header *mail.Header |
Dusan Kasan | b974c63 | 2017-04-18 12:58:42 +0200 | [diff] [blame] | 355 | err error |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | func (hp headerParser) parseAddress(s string) (ma *mail.Address) { |
| 359 | if hp.err != nil { |
| 360 | return nil |
| 361 | } |
| 362 | |
| 363 | if strings.Trim(s, " \n") != "" { |
| 364 | ma, hp.err = mail.ParseAddress(s) |
| 365 | |
| 366 | return ma |
| 367 | } |
| 368 | |
| 369 | return nil |
| 370 | } |
| 371 | |
| 372 | func (hp headerParser) parseAddressList(s string) (ma []*mail.Address) { |
| 373 | if hp.err != nil { |
| 374 | return |
| 375 | } |
| 376 | |
| 377 | if strings.Trim(s, " \n") != "" { |
| 378 | ma, hp.err = mail.ParseAddressList(s) |
| 379 | return |
| 380 | } |
| 381 | |
| 382 | return |
| 383 | } |
| 384 | |
| 385 | func (hp headerParser) parseTime(s string) (t time.Time) { |
Dusan Kasan | b974c63 | 2017-04-18 12:58:42 +0200 | [diff] [blame] | 386 | if hp.err != nil || s == "" { |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 387 | return |
| 388 | } |
| 389 | |
| 390 | t, hp.err = time.Parse(time.RFC1123Z, s) |
| 391 | if hp.err == nil { |
| 392 | return t |
| 393 | } |
| 394 | |
| 395 | t, hp.err = time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", s) |
| 396 | |
| 397 | return |
| 398 | } |
| 399 | |
| 400 | func (hp headerParser) parseMessageId(s string) string { |
| 401 | if hp.err != nil { |
| 402 | return "" |
| 403 | } |
| 404 | |
| 405 | return strings.Trim(s, "<> ") |
| 406 | } |
| 407 | |
| 408 | func (hp headerParser) parseMessageIdList(s string) (result []string) { |
| 409 | if hp.err != nil { |
| 410 | return |
| 411 | } |
| 412 | |
| 413 | for _, p := range strings.Split(s, " ") { |
| 414 | if strings.Trim(p, " \n") != "" { |
| 415 | result = append(result, hp.parseMessageId(p)) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return |
| 420 | } |
| 421 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 422 | // Attachment with filename, content type and data (as a io.Reader) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 423 | type Attachment struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 424 | Filename 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 | // EmbeddedFile with content id, content type and data (as a io.Reader) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 430 | type EmbeddedFile struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 431 | CID string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 432 | ContentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 433 | Data io.Reader |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 436 | // 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] | 437 | type Email struct { |
| 438 | Header mail.Header |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 439 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 440 | Subject string |
| 441 | Sender *mail.Address |
| 442 | From []*mail.Address |
| 443 | ReplyTo []*mail.Address |
| 444 | To []*mail.Address |
| 445 | Cc []*mail.Address |
| 446 | Bcc []*mail.Address |
| 447 | Date time.Time |
| 448 | MessageID string |
| 449 | InReplyTo []string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 450 | References []string |
| 451 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 452 | ResentFrom []*mail.Address |
| 453 | ResentSender *mail.Address |
| 454 | ResentTo []*mail.Address |
| 455 | ResentDate time.Time |
| 456 | ResentCc []*mail.Address |
| 457 | ResentBcc []*mail.Address |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 458 | ResentMessageID string |
| 459 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 460 | HTMLBody string |
| 461 | TextBody string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 462 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 463 | Attachments []Attachment |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 464 | EmbeddedFiles []EmbeddedFile |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 465 | } |