Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SirAlyon opened this issue May 9, 2024 · 0 comments
Open
Labels
needs investigation This will be tested / debugged or checked out.

Comments

@SirAlyon
Copy link

SirAlyon commented May 9, 2024

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. :)

@SirAlyon SirAlyon added the needs investigation This will be tested / debugged or checked out. label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs investigation This will be tested / debugged or checked out.
Projects
None yet
Development

No branches or pull requests

1 participant