MailCore / mailcore2

MailCore 2 provide a simple and asynchronous API to work with e-mail protocols IMAP, POP and SMTP. The API has been redesigned from ground up.
Other
2.59k stars 623 forks source link

[Bug] - Can't parse somewhat irregular EML file #1936

Open vincedev opened 2 years ago

vincedev commented 2 years ago

Summary

Trying to parse the attached EML file fails using MCOMessageParser. Still mail client applications (at least Apple Mail) can open, parse and render the message (looks like parsing is somewhat lenient). Parse in this case returns the whole message data.

Platform(s)

<macOS>

Piece of code

Trying to get attachments using this piece of code :

extension MCOMessageParser {
    func allAttachments() -> [MCOAttachment] {
        guard let main = mainPart() else {
            return []
        }
        let attachments = parseAttachments(in: main)
        // MailCore may return the whole itself in case it can't parse the message
        // We are ensuring this does not happen by returning an empty array
        if let first = attachments.first, first == main {
            return []
        }
        return parseAttachments(in: main)
    }

    private func parseAttachments(in part: MCOAbstractPart) -> [MCOAttachment] {
        if let message = part as? MCOMessagePart, let main = message.mainPart {
            return parseAttachments(in: main)
        } else if let multipart = part as? MCOMultipart {
            var attachements = [MCOAttachment]()
            for part in multipart.parts {
                if let abstractPart = part as? MCOAbstractPart {
                    attachements.append(contentsOf: parseAttachments(in: abstractPart))
                }
            }
            return attachements
        } else if let attachment = part as? MCOAttachment {
            return [attachment]
        } else {
            return []
        }
    }
}

Actual outcome MailCore did return the whole data as an attachment.

Expected outcome Expected MailCore to somehow parse data correctly.

vincedev commented 2 years ago

test.txt