barbushin / php-imap

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
MIT License
1.67k stars 459 forks source link

[BUG] Truncated Attachment Names Resulting in Lost File Extensions #724

Open SirAlyon opened 6 months ago

SirAlyon commented 6 months ago

Environment PHP IMAP version: 5.0.1 PHP Version: 8.1.2-1ubuntu2.17 (cli) Type of execution: CLI

Describe the bug When processing emails with attachments through the PHP IMAP library, attachment names that are long and contain spaces are truncated. This results in filenames losing their extensions, which complicates file handling and can lead to potential data loss. This issue necessitates manual intervention to determine and append the correct file extensions.

To Reproduce Steps to reproduce the behavior:

Configure an IMAP connection using the Mailbox class. Fetch emails with attachments that have long names including spaces. Observe that the fetched attachment names are truncated and missing extensions.

The used code:

$mailsIds = $mailbox->searchMailbox('ALL');
foreach ($mailsIds as $mailId) {
    $mail = $mailbox->getMail($mailId);
    foreach ($mail->getAttachments() as $attachment) {
        // The following line is used to manually determine the extension due to the issue
        $extension = $this->determineExtension($attachment, pathinfo($attachment->name, PATHINFO_EXTENSION), $mail);
        echo $attachment->name . " extension: " . $extension;  // Output shows truncated names
    }
}

private function determineExtension($attachment, $fileInfo, $mail)
    {
        if (array_key_exists('extension', $fileInfo)) {
            return $fileInfo['extension'];
        } else {
            $attachmentExt = $this->extractExtensionFromMime($attachment, $mail);
            $attachment->name .= ".$attachmentExt";
            return $attachmentExt;
        }
    }

Expected behavior I expect that attachment names should be fully preserved, including spaces and their full length, without truncation. This would allow for accurate file handling and prevent the risk of data loss due to incorrect or missing file extensions.

Additional context This issue occurs only with certain emails that have attachments with particularly long names. The problem seems to be related to how the IMAP server or the PHP IMAP library handles or parses attachment headers.

Thank you for your attention to this matter. I appreciate any assistance you can provide in resolving this issue, and I am available for further details or to assist with testing solutions. :)

goodlikee commented 2 weeks ago

Hi, I'm facing a similar problem. after saving the $mailStructure, I saw that the values in the filename and name attributes were different, in name they were correct, and in filename they were truncated. When sending these attachments from my mail, I received the same values in the filename and name attributes.

Does it seem to me that the problem is on the sender's side?

{
    "type": 1,
    "encoding": 0,
    "ifsubtype": 1,
    "subtype": "MIXED",
    "ifdescription": 0,
    "ifid": 0,
    "ifdisposition": 0,
    "ifdparameters": 0,
    "ifparameters": 1,
    "parameters": [
        {
            "attribute": "boundary",
            "value": "------------93zUhZ1GGmkBP6uTmX3AMEeh"
        }
    ],
    "parts": [
        {
            "type": 0,
            "encoding": 0,
            "ifsubtype": 1,
            "subtype": "PLAIN",
            "ifdescription": 0,
            "ifid": 0,
            "ifdisposition": 0,
            "ifdparameters": 0,
            "ifparameters": 1,
            "parameters": [
                {
                    "attribute": "charset",
                    "value": "UTF-8"
                },
                {
                    "attribute": "format",
                    "value": "flowed"
                }
            ]
        },
        {
            "type": 3,
            "encoding": 3,
            "ifsubtype": 1,
            "subtype": "PDF",
            "ifdescription": 0,
            "ifid": 0,
            "bytes": 313852,
            "ifdisposition": 1,
            "disposition": "attachment",
            "ifdparameters": 1,
            "dparameters": [
                {
                    "attribute": "filename",
                    "value": "UTF-8''%32%34%31%30%32%31%2D%32%30%38%36%38%20%D0%A1%D0%BE"
                }
            ],
            "ifparameters": 1,
            "parameters": [
                {
                    "attribute": "name",
                    "value": "=?UTF-8?B?MjQxMDIxLTIwODY4INCh0L7Qs9C70LDRgdC+0LLQsNC90LjQtSDQv9GA?= =?UTF-8?B?0L7QtdC60YLQsCDRgNC10YjQtdC90LjRj1\/QnNCY0J3QntCR0KDQndCQ?= =?UTF-8?B?0KPQmtCYX9CY0JPQlF\/QsS7Rgl8o0YTQsNC50Lsg0L7RgtC+0LHRgNCw?= =?UTF-8?B?0LbQtdC90LjRjykucGRm?="
                }
            ]
        },
        {
            "type": 3,
            "encoding": 3,
            "ifsubtype": 1,
            "subtype": "PDF",
            "ifdescription": 0,
            "ifid": 0,
            "bytes": 314218,
            "ifdisposition": 1,
            "disposition": "attachment",
            "ifdparameters": 1,
            "dparameters": [
                {
                    "attribute": "filename",
                    "value": "UTF-8''%32%34%31%30%32%31%2D%32%30%38%37%30%20%D0%A1%D0%BE"
                }
            ],
            "ifparameters": 1,
            "parameters": [
                {
                    "attribute": "name",
                    "value": "=?UTF-8?B?MjQxMDIxLTIwODcwINCh0L7Qs9C70LDRgdC+0LLQsNC90LjQtSDQv9GA?= =?UTF-8?B?0L7QtdC60YLQsCDRgNC10YjQtdC90LjRj1\/QnNCY0J3QntCR0KDQndCQ?= =?UTF-8?B?0KPQmtCYX9Cd0JPQkNCh0KNf0LEu0YJfKNGE0LDQudC7INC+0YLQvtCx?= =?UTF-8?B?0YDQsNC20LXQvdC40Y8pLnBkZg==?="
                }
            ]
        },
        {
            "type": 3,
            "encoding": 3,
            "ifsubtype": 1,
            "subtype": "PDF",
            "ifdescription": 0,
            "ifid": 0,
            "bytes": 314402,
            "ifdisposition": 1,
            "disposition": "attachment",
            "ifdparameters": 1,
            "dparameters": [
                {
                    "attribute": "filename",
                    "value": "UTF-8''%32%34%31%30%32%31%2D%32%30%38%39%33%20%D0%A1%D0%BE"
                }
            ],
            "ifparameters": 1,
            "parameters": [
                {
                    "attribute": "name",
                    "value": "=?UTF-8?B?MjQxMDIxLTIwODkzINCh0L7Qs9C70LDRgdC+0LLQsNC90LjQtSDQv9GA?= =?UTF-8?B?0L7QtdC60YLQsCDRgNC10YjQtdC90LjRj1\/QnNCY0J3QntCR0KDQndCQ?= =?UTF-8?B?0KPQmtCYX9CY0JPQlF\/Rgl8o0YTQsNC50Lsg0L7RgtC+0LHRgNCw0LY=?= =?UTF-8?B?0LXQvdC40Y8pLnBkZg==?="
                }
            ]
        },
        {
            "type": 3,
            "encoding": 3,
            "ifsubtype": 1,
            "subtype": "PDF",
            "ifdescription": 0,
            "ifid": 0,
            "bytes": 314662,
            "ifdisposition": 1,
            "disposition": "attachment",
            "ifdparameters": 1,
            "dparameters": [
                {
                    "attribute": "filename",
                    "value": "UTF-8''%32%34%31%30%32%31%2D%32%30%38%36%39%20%D0%A1%D0%BE"
                }
            ],
            "ifparameters": 1,
            "parameters": [
                {
                    "attribute": "name",
                    "value": "=?UTF-8?B?MjQxMDIxLTIwODY5INCh0L7Qs9C70LDRgdC+0LLQsNC90LjQtSDQv9GA?= =?UTF-8?B?0L7QtdC60YLQsCDRgNC10YjQtdC90LjRj1\/QnNCY0J3QntCR0KDQndCQ?= =?UTF-8?B?0KPQmtCYX9Cd0JPQkNCh0KNf0LEu0YJf0LrRgNC+0LLQu9GPXyjRhNCw?= =?UTF-8?B?0LnQuyDQvtGC0L7QsdGA0LDQttC10L3QuNGPKS5wZGY=?="
                }
            ]
        }
    ]
}