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

3

10

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 database. I am doing this using cron. Except the attachments every thing works perfect. I have seen some post which create download links. Since i am using cron i can't do it manually.

        $hostname = '{xxxx.net:143/novalidate-cert}INBOX';
        $username = '[email protected]';
        $password = 'zzzz';
        /* try to connect */
        $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error());
        $emails = imap_search($inbox,'ALL');

                if($emails) {
          $output = '';
          rsort($emails);
          foreach($emails as $email_number) {
            $structure = imap_fetchstructure($inbox, $email_number); 
            $name = $structure->parts[1]->dparameters[0]->value; // name of the file
            $type = $structure->parts[1]->type; //type of the file 
}}

I am able to get type and name of the files but don't know how to proceed further

Any one please help me. thank you...

Rosemonde answered 2/4, 2012 at 9:59 Comment(0)
B
8

To save attachments as files, you need to parse the structure of the message and take out all parts that are attachments on it's own (content disposition). You should wrap that into classes of their own so you have an easy access and you can handle errors more easily over time, email parsing can be fragile:

$savedir = __DIR__ . '/imap-dump/';

$inbox = new IMAPMailbox($hostname, $username, $password);
$emails = $inbox->search('ALL');
if ($emails) {
    rsort($emails);
    foreach ($emails as $email) {
        foreach ($email->getAttachments() as $attachment) {
            $savepath = $savedir . $attachment->getFilename();
            file_put_contents($savepath, $attachment);
        }
    }
}

The code of these classes is more or less wrapping the imap_... functions, but for the attachment classes, it's doing the parsing of the structures as well. You find the code on github. Hope this is helpful.

Balaklava answered 11/4, 2012 at 22:52 Comment(1)
Hi @LimiJerin , Would you be so kind to post the working codes here ? it seem not to work from my end, not attachment is being downloadedDecree
H
5

Although using PHP + Cron and a standard mail server might work, the amount of work needed to handle all the edge cases, error reporting, etc might not be worth the time. Although I haven't used it, Postmark Inbound seems like an incredible (paid) service that will eliminate most of the headache of processing email via the PHP imap api.

If you want to try to handle everything via PHP, you might want to check this resource out.

Helotry answered 2/4, 2012 at 12:49 Comment(2)
Never underestimate the burden to integrate and error report remove-services. Especially the edge cases.Balaklava
True - both sides definitely have to be weighed. If email processing isn't at the core of your project, it might be best to write your own, but if you are looking to simplify inbound email processing outsourcing that logic might be a good choice.Helotry
R
0

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.

Reamer answered 8/12, 2014 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.