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.
#2

[eluser]The Wizard[/eluser]
interesting piece of code, looks very clean and usefull.

thanks Smile
#3

[eluser]ray73864[/eluser]
thanks, i spent all of yesterday going through it, for the 'seen' if statement to work, this piece of code has to be used on an imap server, since only with the imap protocol does the server and the client know about the status of the messages, couldn't get the seen code to work with pop3.

i had a problem with the live server and connecting to it though, which i didn't have on my development server.

If you get an rsh error when trying to close the imap connection, put '/norsh' into the connection string and it will go away, even if you don't get that error it can be handy, as it removes the 10-15sec lag when opening a connection.

eg:

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

and if you aren't sure of the port that your server uses for imap, you can omit the port number completely by doing the following

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

If you want to force it into a specific folder on your email account

Code:
$mbox = imap_open("{mail.example.com:143/imap/notls}INBOX", "user", "pass");
or
Code:
$mbox = imap_open("{mail.example.com:143/imap/notls}DRAFTS", "user", "pass");
or
Code:
$mbox = imap_open("{mail.example.com:143/imap/notls}SENT", "user", "pass");
#4

[eluser]sophistry[/eluser]
thanks for this.

you might want to check imap_pop library in the wiki which, despite the name, is mostly POP3 oriented (though it does work with imap).

imap_pop_class
#5

[eluser]Dam1an[/eluser]
Awesome, I can see this coming in use on my current project (with a few tweaks) Smile




Theme © iAndrew 2016 - Forum software by © MyBB