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" |
Edwin Love | 33953e5 | 2019-04-02 15:29:22 +0100 | [diff] [blame^] | 12 | "regexp" |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 13 | "strings" |
| 14 | "time" |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 15 | ) |
| 16 | |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 17 | const contentTypeMultipartMixed = "multipart/mixed" |
| 18 | const contentTypeMultipartAlternative = "multipart/alternative" |
| 19 | const contentTypeMultipartRelated = "multipart/related" |
| 20 | const contentTypeTextHtml = "text/html" |
| 21 | const contentTypeTextPlain = "text/plain" |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 22 | |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 23 | // Parse an email message read from io.Reader into parsemail.Email struct |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 24 | func Parse(r io.Reader) (email Email, err error) { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 25 | msg, err := mail.ReadMessage(r) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 26 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 27 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 30 | email, err = createEmailFromHeader(msg.Header) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 31 | if err != nil { |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 32 | return |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 35 | contentType, params, err := parseContentType(msg.Header.Get("Content-Type")) |
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 | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 52 | err = fmt.Errorf("Unknown top level mime type: %s", contentType) |
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 | } |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 243 | } else if isAttachment(part) { |
| 244 | at, err := decodeAttachment(part) |
| 245 | if err != nil { |
| 246 | return textBody, htmlBody, attachments, embeddedFiles, err |
| 247 | } |
| 248 | |
| 249 | attachments = append(attachments, at) |
| 250 | } else { |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 251 | 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] | 252 | } |
| 253 | } |
| 254 | |
| 255 | return textBody, htmlBody, attachments, embeddedFiles, err |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 258 | func decodeMimeSentence(s string) string { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 259 | result := []string{} |
| 260 | ss := strings.Split(s, " ") |
| 261 | |
| 262 | for _, word := range ss { |
| 263 | dec := new(mime.WordDecoder) |
| 264 | w, err := dec.Decode(word) |
| 265 | if err != nil { |
| 266 | if len(result) == 0 { |
| 267 | w = word |
| 268 | } else { |
| 269 | w = " " + word |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | result = append(result, w) |
| 274 | } |
| 275 | |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 276 | return strings.Join(result, "") |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 279 | func decodeHeaderMime(header mail.Header) (mail.Header, error) { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 280 | parsedHeader := map[string][]string{} |
| 281 | |
| 282 | for headerName, headerData := range header { |
| 283 | |
| 284 | parsedHeaderData := []string{} |
| 285 | for _, headerValue := range headerData { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 286 | parsedHeaderData = append(parsedHeaderData, decodeMimeSentence(headerValue)) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | parsedHeader[headerName] = parsedHeaderData |
| 290 | } |
| 291 | |
| 292 | return mail.Header(parsedHeader), nil |
| 293 | } |
| 294 | |
| 295 | func decodePartData(part *multipart.Part) (io.Reader, error) { |
| 296 | encoding := part.Header.Get("Content-Transfer-Encoding") |
| 297 | |
Kevin Chen | 7835eb0 | 2018-05-02 20:58:58 -0400 | [diff] [blame] | 298 | if strings.EqualFold(encoding, "base64") { |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 299 | dr := base64.NewDecoder(base64.StdEncoding, part) |
| 300 | dd, err := ioutil.ReadAll(dr) |
| 301 | if err != nil { |
| 302 | return nil, err |
| 303 | } |
| 304 | |
| 305 | return bytes.NewReader(dd), nil |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 306 | } |
Dusan Kasan | 45ca264 | 2017-04-18 10:39:35 +0200 | [diff] [blame] | 307 | |
| 308 | return nil, fmt.Errorf("Unknown encoding: %s", encoding) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 311 | func isEmbeddedFile(part *multipart.Part) bool { |
| 312 | return part.Header.Get("Content-Transfer-Encoding") != "" |
| 313 | } |
| 314 | |
| 315 | func decodeEmbeddedFile(part *multipart.Part) (ef EmbeddedFile, err error) { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 316 | cid := decodeMimeSentence(part.Header.Get("Content-Id")) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 317 | decoded, err := decodePartData(part) |
| 318 | if err != nil { |
| 319 | return |
| 320 | } |
| 321 | |
| 322 | ef.CID = strings.Trim(cid, "<>") |
| 323 | ef.Data = decoded |
| 324 | ef.ContentType = part.Header.Get("Content-Type") |
| 325 | |
| 326 | return |
| 327 | } |
| 328 | |
| 329 | func isAttachment(part *multipart.Part) bool { |
| 330 | return part.FileName() != "" |
| 331 | } |
| 332 | |
| 333 | func decodeAttachment(part *multipart.Part) (at Attachment, err error) { |
Dusan Kasan | f4376a6 | 2017-05-23 21:03:55 +0200 | [diff] [blame] | 334 | filename := decodeMimeSentence(part.FileName()) |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 335 | decoded, err := decodePartData(part) |
| 336 | if err != nil { |
| 337 | return |
| 338 | } |
| 339 | |
| 340 | at.Filename = filename |
| 341 | at.Data = decoded |
| 342 | at.ContentType = strings.Split(part.Header.Get("Content-Type"), ";")[0] |
| 343 | |
| 344 | return |
| 345 | } |
| 346 | |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 347 | type headerParser struct { |
| 348 | header *mail.Header |
Dusan Kasan | b974c63 | 2017-04-18 12:58:42 +0200 | [diff] [blame] | 349 | err error |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | func (hp headerParser) parseAddress(s string) (ma *mail.Address) { |
| 353 | if hp.err != nil { |
| 354 | return nil |
| 355 | } |
| 356 | |
| 357 | if strings.Trim(s, " \n") != "" { |
| 358 | ma, hp.err = mail.ParseAddress(s) |
| 359 | |
| 360 | return ma |
| 361 | } |
| 362 | |
| 363 | return nil |
| 364 | } |
| 365 | |
| 366 | func (hp headerParser) parseAddressList(s string) (ma []*mail.Address) { |
| 367 | if hp.err != nil { |
| 368 | return |
| 369 | } |
| 370 | |
| 371 | if strings.Trim(s, " \n") != "" { |
| 372 | ma, hp.err = mail.ParseAddressList(s) |
| 373 | return |
| 374 | } |
| 375 | |
| 376 | return |
| 377 | } |
| 378 | |
Edwin Love | 33953e5 | 2019-04-02 15:29:22 +0100 | [diff] [blame^] | 379 | var timezoneRegex = regexp.MustCompile(` \([A-Za-z0-9]+\)$`) |
| 380 | |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 381 | func (hp headerParser) parseTime(s string) (t time.Time) { |
Dusan Kasan | b974c63 | 2017-04-18 12:58:42 +0200 | [diff] [blame] | 382 | if hp.err != nil || s == "" { |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 383 | return |
| 384 | } |
| 385 | |
| 386 | t, hp.err = time.Parse(time.RFC1123Z, s) |
| 387 | if hp.err == nil { |
| 388 | return t |
| 389 | } |
| 390 | |
Edwin Love | 33953e5 | 2019-04-02 15:29:22 +0100 | [diff] [blame^] | 391 | s = timezoneRegex.ReplaceAllString(s, "") |
| 392 | |
Dusan Kasan | e668cf2 | 2017-04-18 12:56:51 +0200 | [diff] [blame] | 393 | t, hp.err = time.Parse("Mon, 2 Jan 2006 15:04:05 -0700", s) |
| 394 | |
| 395 | return |
| 396 | } |
| 397 | |
| 398 | func (hp headerParser) parseMessageId(s string) string { |
| 399 | if hp.err != nil { |
| 400 | return "" |
| 401 | } |
| 402 | |
| 403 | return strings.Trim(s, "<> ") |
| 404 | } |
| 405 | |
| 406 | func (hp headerParser) parseMessageIdList(s string) (result []string) { |
| 407 | if hp.err != nil { |
| 408 | return |
| 409 | } |
| 410 | |
| 411 | for _, p := range strings.Split(s, " ") { |
| 412 | if strings.Trim(p, " \n") != "" { |
| 413 | result = append(result, hp.parseMessageId(p)) |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return |
| 418 | } |
| 419 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 420 | // Attachment with filename, content type and data (as a io.Reader) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 421 | type Attachment struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 422 | Filename string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 423 | ContentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 424 | Data io.Reader |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 427 | // EmbeddedFile with content id, content type and data (as a io.Reader) |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 428 | type EmbeddedFile struct { |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 429 | CID string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 430 | ContentType string |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 431 | Data io.Reader |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Dusan Kasan | 1a96648 | 2017-04-18 10:45:25 +0200 | [diff] [blame] | 434 | // 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] | 435 | type Email struct { |
| 436 | Header mail.Header |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 437 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 438 | Subject string |
| 439 | Sender *mail.Address |
| 440 | From []*mail.Address |
| 441 | ReplyTo []*mail.Address |
| 442 | To []*mail.Address |
| 443 | Cc []*mail.Address |
| 444 | Bcc []*mail.Address |
| 445 | Date time.Time |
| 446 | MessageID string |
| 447 | InReplyTo []string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 448 | References []string |
| 449 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 450 | ResentFrom []*mail.Address |
| 451 | ResentSender *mail.Address |
| 452 | ResentTo []*mail.Address |
| 453 | ResentDate time.Time |
| 454 | ResentCc []*mail.Address |
| 455 | ResentBcc []*mail.Address |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 456 | ResentMessageID string |
| 457 | |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 458 | HTMLBody string |
| 459 | TextBody string |
Dusan Kasan | b49ceb6 | 2017-04-13 00:00:36 +0200 | [diff] [blame] | 460 | |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 461 | Attachments []Attachment |
Dusan Kasan | 17e497e | 2017-04-10 22:44:22 +0200 | [diff] [blame] | 462 | EmbeddedFiles []EmbeddedFile |
Dusan Kasan | 4595dfe | 2017-04-13 00:38:24 +0200 | [diff] [blame] | 463 | } |