postalsys / imapflow

IMAP Client library for EmailEngine Email API (https://emailengine.app)
https://imapflow.com
Other
350 stars 59 forks source link

Fix MIME decoder for meta.filename #168

Closed jerezoff closed 7 months ago

jerezoff commented 7 months ago

When i trying to download files, not all mime-encoded filenames being decrypted

There is the code i am using

msg.bodyStructure.childNodes.forEach((node) => {
                                if (!(node.type.includes('xlsx') || node.type.includes('sheet') || node.type.includes('excel'))) {
                                    return(
                                        console.log('unknown file type', node.type)
                                    )
                                }
                                this.imap.download(`${msg.seq}`, node.part)
                                    .then(({meta, content}) => {
                                        const buffs: Uint8Array[] = []
                                        if (!content) {
                                            return console.log('unsupported content?')
                                        }
                                        content.on('data', (data) => {
                                            buffs.push(data)
                                        })

                                        content.on('end', () => {
                                            const result = Buffer.concat(buffs, meta.expectedSize)

                                            fs.writeFile(`./download/${meta.filename}`, result, () => {
                                                console.log(meta.filename, meta.charset) //Trying to log what happens
                                            })
                                        })
                                    })
                                    .catch((e) => {
                                        console.log('failed to download file from mail', e)
                                    })

There is log

=?UTF-8?B?0J/RgNCw0LnRgSDQktCY0J3Qog==?= 26_01_2024.xlsx undefined
=?UTF-8?B?0J/RgNCw0LnRgSDQktCY0J3Qog==?= 29_01_2024.xlsx undefined
=?windows-1251?B?MjAyNF8wMV8yOV/v8ODp8SDQz9ZfK1/t4Ovo9+jlLnhsc3g=?= undefined
=?UTF-8?B?0J/RgNCw0LnRgSDQktCY0J3Qog==?= 30_01_2024.xlsx undefined
=?UTF-8?B?0J/RgNCw0LnRgSDQktCY0J3Qog==?= 31_01_2024.xlsx undefined
=?UTF-8?B?0J/RgNCw0LnRgSDQktCY0J3Qog==?= 01_02_2024.xlsx undefined
=?UTF-8?B?0J/RgNCw0LnRgSDQktCY0J3Qog==?= 02_02_2024.xlsx undefined

But they perfectly decoded by online service: https://dogmamix.com/MimeHeadersDecoder/

image

andris9 commented 7 months ago

Mime-encoded words are not allowed for filenames. Instead, the sender must use mime continuation (see RFC2231). Can you provide the full source of such an email for testing?

jerezoff commented 7 months ago

Mime-encoded words are not allowed for filenames. Instead, the sender must use mime continuation (see RFC2231). Can you provide the full source of such an email for testing?

What is the best way to send you the source, Thunderbird export or forward the mail?

andris9 commented 7 months ago

Do not forward as this would completely mess up the structure. Export the eml or when using webmail, then "Download original" or whatever the specific option is.

jerezoff commented 7 months ago

Do not forward as this would completely mess up the structure. Export the eml or when using webmail, then "Download original" or whatever the specific option is.

Sent you an email with an EML file :)

andris9 commented 7 months ago

Thank you, I can confirm it is a bug in the download method. I'll release a fix shortly

andris9 commented 7 months ago

Fixed in v1.0.151