Search code examples
phphyperlinkdownloadimapattachment

How to make an attachment download link for a webmail using IMAP and PHP?


I'm working on a mobile webmail project, and I'm currently looking for a way to create a link (or a list of links) to allow webmail users to download attachments on mails. It would look like : <a href="???">title of the file.ext</a>

I can already get the attachments and display it as an awful wall of special chars, but I can't find the way to create a link to download the attachment after displaying the email content.

Does anyone have a guess ?


Solution

  • Pass the force-download header to the client.

    header("Content-type: application/force-download");
    header("Content-Disposition: attachment; filename=\"file.zip\"");
    header("Content-Length: ".filesize("file.zip"));
    

    The content-length is not entirely necessary.

    You will have to create a php script which will be linked liked this:

    <a href="http://server/script.php?emailId=12345&fileName=file.zip">
    

    And the PHP script will just send the headers above and print full contents of the file.