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