SSilence / php-imap-client

a easy solution for simple IMAP email access in php
MIT License
268 stars 138 forks source link

Corrupted attachment files #257

Closed bjaverhagen closed 3 years ago

bjaverhagen commented 3 years ago

Hi,

I save all attachments of an email. But when I download the attachments from the folder, all files are corrupted. I cant open any file. What am I doing wrong?

$imap->selectFolder("INBOX"); $emails = $imap->getMessages();

foreach ( $emails as $email ) { $id = $email->getId($email->header->uid); $imap->getMessage($id); $imap->saveAttachments(['dir'=>'/dir/attachment']); }

bjaverhagen commented 3 years ago

Changed saveAttachments function in ImapClient.php to:

public function saveAttachments($options = null)
{
    if(!isset($options['dir'])){
        $dir = __DIR__.DIRECTORY_SEPARATOR;
    }else{
        $dir = $options['dir'];
    };
    if(!isset($options['incomingMessage'])){
        $incomingMessage = $this->incomingMessage;
    }else{
        $incomingMessage = $options['incomingMessage'];
    };
    foreach ($incomingMessage->attachments as $key => $attachment) {
        $newFileName = $attachment->name;
        if (base64_decode($attachment->body, true) == true){
            file_put_contents($dir.DIRECTORY_SEPARATOR.$newFileName, base64_decode($attachment->body));
        }else{
            file_put_contents($dir.DIRECTORY_SEPARATOR.$newFileName, quoted_printable_decode($attachment->body));
        }
        //file_put_contents($dir.DIRECTORY_SEPARATOR.$newFileName, $attachment->body);
    };
}