how to download mails attachment to a specific folder using IMAP and php

后端 未结 3 528
感动是毒
感动是毒 2020-12-23 18:18

i am developing a site in which users can mail tickets and attach any type of files to a specific mail id. I need to add the mail subject, content and attachment to the data

3条回答
  •  执念已碎
    2020-12-23 18:47

    In case if you want to download attachments as zip

    $zip = new ZipArchive();
    $tmp_file = tempnam('.', '');
    $zip->open($tmp_file, ZipArchive::CREATE);
    
    $mailbox = $connection->getMailbox('INBOX');
    foreach ($mailbox->getMessage() as $message) {
        $attachments = $message->getAttachments();
        foreach ($attachments as $attachment) {
            $zip->addFromString($attachment->getFilename(), $attachment->getDecodedContent());
        }
    }
    
    $zip->close();
    
    # send the file to the browser as a download
    header('Content-disposition: attachment; filename=download.zip');
    header('Content-type: application/zip');
    readfile($tmp_file);
    

    This code uses library hosted on GitHub. Hope this is helpful.

提交回复
热议问题