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