Welcome Guest, Not a member yet? Register   Sign In
Email download library
#1

[eluser]ray73864[/eluser]
So i had an instance the other day where it was too complicated for a guy i am doing a website for to save the attachment in the email he receives, go to the upload form on the website and upload the attachment to the server.

So i came up with the following library, where you email a special email account, the script will connect to that email account and retrieve all attachments, then depending on what 'subtype' the attachment is, would depend on what it does.

Code:
<?php

$mbox = imap_open("{mail.example.com:143/imap/notls}", "user", "pass");

$lines = imap_num_msg($mbox);

for ($i = 0; $i < $lines; $i++) {
    
    $j = $i + 1;
    
    $header_overview = imap_fetch_overview($mbox, $j);
    
    if ($header_overview[0]->seen == 0) {
        $message = imap_fetchstructure($mbox, $header_overview[0]->msgno);    
    
        if ($message->parts) {
            foreach ($message->parts as $key => $part) {
                switch (strtoupper($part->subtype)) {
                    case 'PDF':
                        $body_struct = imap_bodystruct($mbox, $header_overview[0]->msgno, $key+1);

                        print_r($body_struct);
                        
                        $fp = fopen($body_struct->parameters[0]->value, "w");
                        fwrite($fp, imap_base64(imap_fetchbody($mbox, $header_overview[0]->msgno, $key+1)));
                        fclose($fp);
                    break;
                                        case 'PLAIN':
                                                echo nl2br(imap_base64(imap_fetchbody($mbox, $header_overview[0]->msgno, $key+1)));
                    default: break;
                }
            }
        }
    }
}

imap_close($mbox);
?&gt;

Basically you add types into the switch and do with them what you need to. If you wish to know what is in each object after going through an imap function, basically just 'print_r(object)' and look at the object array structure.

For this to work, the server you use it on must have the IMAP php module turned on.


Messages In This Thread
Email download library - by El Forum - 05-02-2009, 03:36 PM
Email download library - by El Forum - 05-02-2009, 03:45 PM
Email download library - by El Forum - 05-02-2009, 03:49 PM
Email download library - by El Forum - 05-04-2009, 01:45 PM
Email download library - by El Forum - 05-04-2009, 01:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB