Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 23rd, 2003, 05:19 AM
Registered User
 
Join Date: Oct 2003
Location: mumbai, maharashtra, India.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default scan attachments from inbox

Can anybody help me in scanning attachments from an inbox and then further scanning a particular attachment file??

Please tell me how do I proceed? I am new tp php but I have been doing web programming since last 4 years..


shali

sv_shali
Reply With Quote
  #2 (permalink)  
Old October 23rd, 2003, 12:59 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

sv_shali,
     The following code appears a bit complicated at first but with some study it's actually pretty simple. This all assumes that you want to use the IMAP module to access your attachments. And also assumes that you already know how to build a connection using that module. Here's a link for more information on imap.

http://www.php.net/imap

Here's another post on p2p which I explain how to open a mail connection using the imap module.
http://p2p.wrox.com/topic.asp?TOPIC_ID=5084

Code:
<?php

open imap connection here
access messages

    /*
     * array message_components(array mail resources) - takes the message_id and returns an array of 
     * message components, attachments, etc.  and returns these in the $message variable. 
     * Takes the $mail array as argument. 
     * 
     * $mail["connection"] - (resource) contains the IMAP resource identifier
     * $mail["message_id"] - (integer)  contains the message identifier offset from '1'
     * For the sake of convienience this may also be accessed by specifying the message in the $_GET["message_id"] variable
    */


    function message_components($mail) {

        /* 
         * -- Define $message variable characteristics
         *
         * $message["attachment"]["type"][$i] - numbered array containing the various possible attachment types
         * $message["attachment"]["encoding"][$i] - numbered array containing the various possible encoding types
         *
         * -- The following numbered array count will correspond with the number of message parts
         *
         * $message["pid"][$i] - contains the message part number (offset from '1')
         * $message["type"][$i] - (part interger value, and subtype e.g. plain, html, etc.)
         *        defined in the following format (type/subtype) 
         * 
         * $message["subtype"][$i] - contains only the subtype   
         * $message["encoding"][$i] - integer value
         * $message["size"][$i] - integer value containing the message part size in bytes
         * $message["disposition"][$i] - inline/attachment - differientiates between inline messages (message body) and attachments
         * $message["name"][$i] - if the disposition is an attachment this will contain the file name of the attachment
         *
        */

        $message = array();

            $message["attachment"]["type"][0] = "text";
            $message["attachment"]["type"][1] = "multipart";
            $message["attachment"]["type"][2] = "message";
            $message["attachment"]["type"][3] = "application";
            $message["attachment"]["type"][4] = "audio";
            $message["attachment"]["type"][5] = "image";
            $message["attachment"]["type"][6] = "video";
            $message["attachment"]["type"][7] = "other";

            $message["attachment"]["encoding"][0] = "7bit";
            $message["attachment"]["encoding"][1] = "8bit";
            $message["attachment"]["encoding"][2] = "binary";
            $message["attachment"]["encoding"][3] = "base64";
            $message["attachment"]["encoding"][4] = "quoted-printable";
            $message["attachment"]["encoding"][5] = "other";


        if (isset($_GET["message_id"]))
        {
            $mail["message_id"] = $_GET["message_id"];    
        }

        # http://www.php.net/imap_fetchstructure
        #
        # imap_fetchstructure(resource imap connection, int offset message id)

        $structure = imap_fetchstructure($mail["connection"], $mail["message_id"]);    

        $parts = $structure->parts;

        for($i = 0; $i < count($parts); $i++)
        {

            $message["pid"][$i] = ($i+1);

            $part = $parts[$i];

            #default to text

                if (empty($part->type)) 

                    $part->type = 0;


            $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
            $message["subtype"][$i] = strtolower($part->subtype);

            #default to 7bit

                if (empty($part->encoding))

                    $part->encoding = 0;

            $message["encoding"][$i] = $message["attachment"]["encoding"][$part->encoding];
            $message["size"][$i] = strtolower($part->bytes);
            $message["disposition"][$i] = strtolower($part->disposition);

            if (strtolower($part->disposition) == "attachment")
            {

            $params = $part->dparameters;

                foreach ($params as $p)
                {                
                    if($p->attribute == "FILENAME")
                    {                    
                        $message["name"][$i] = $p->value;
                            break;
                    }
                }

            }

        }

        return $message;

    }

    /*
     * detech_attachments loops through the $message array() as returned by 
     * the message_components() function to find the parts which are data attachments
     * and returns these as part of the attachments array()
     * Takes the message array as argument
     *
    */

    function detect_attachments($message)
    {
        for($i=0; $i<sizeof($message); $i++)
        {
            if($message["disposition"][$i] == "attachment") 
                $attachments[$i] = $message["disposition"][$i];            
        }

        return $attachments;
    }

$message_structure = message_components($mail);
$attachments = detect_attachments($message_structure);

if (!empty($attachments))

    $attachment_count = count($attachments);

else 

    $attachment_count = "This message does not contain attachments";

echo $attachment_count;

?>
So here we go. The first function that I provided will break down each part of a message based on data returned from the imap_fetchstructure function. And it will then place each part of the message into a multi-dimensional array. I happened to get this code from some web-based tutorial out there. I still don't fully and completely understand how everything works myself, but I do know that it works under most conditions. I have run into a bug where it won't detect the attchments that I send from my PHP-designed mail program using a MIME mail class that I downloaded but it does detect attachements from Outlook 2000. But I think that problem has something to do with the MIME class.

The second function accesses that built array of message part information and detects which are attachements.

I haven't done all the work for you! Mostly because I haven't yet *written* the final step that accesses the attachment.

I don't have all the answers on that either, you'll have to think through that part for yourself. I can provide some clues though, use imap_fetchbody and provide the part id which will return the binary data. Which I am sure will have to be decoded from whatever encoding was used in the message. If you look at the first function that I provided you will see that information is gathered. The file type and original file name is also gathered there in the same multi-dimensional array.

imap_fetchbody(imap resource identifier, int message id, int part id);
http://www.php.net/imap_fetchbody

The part id above is set as indice 'pid' in the message_components() function.

Then to download the attachment you'll need to create a script in which you pass the part id, and message_id to it where it may be accessed by imap_fetchbody. Create a simple hyperlink to that script in your mail application, include something like you see in the following code and the browser will be presented with a 'save as' dialouge.

Code:
<?php
# download.php
# This will present the browser with a save as dialouge

open imap connection here

# Access the attachment via the message_id and part_id

$data = imap_fetchbody(imap resource identifier, int message id, int part id);

header("Content-type: application/x-unknown");
header("Content-Disposition: attachment; filename = proposed file name here.extension");

print out binary data retrieved from imap_fetchbody here

echo $data;

close imap connection
?>
If you want to gather additional information on how the imap_fetchstructure function works and provides information you can use the var_dump() function to see what everything inside is doing.

echo "<pre>";
echo var_dump(imap_fetchstructure($mail["connection"], $mail["message_id"]));
echo "</pre>";

This will give you a readable print out of what is going on inside of one of these complicated IMAP functions.

That's a lot to take in, so give that a good study and get back with us if you have any questions. You can also reference the web for additional tutorials to cross-reference.

http://www.google.com/search?q=PHP+IMAP+tutorial

: )
Rich


:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old October 23rd, 2003, 11:51 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi shali,
I had a little more time to go back and straighten out my reply and edit out some of the nonsense and elaborate a little more on the key components. I was in a bit of a hurry :D.

There's always the alternative of downloading a complete PHP mail program! But if you're like me and just want to know how it all works there's plenty of tutorials out there to help you figure it out!

Do let me know if you're having any trouble understanding what is going on!

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #4 (permalink)  
Old October 25th, 2003, 12:40 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hello again,
Having yet more time on my hands to look at this code, which I now have a much better understanding of, I found some bugs in my original function there. The bug was in how the information for the MIME type of each message part was gathered. Now updated once again.

I also went to all the bother of searching for the original tutorial that I referenced to create my program. Which as it turns out was on the devshed website.

The second tutorial on devshed is the one that specifically addresses accessing email attachments.

http://www.devshed.com/Server_Side/P...il1/page1.html
http://www.devshed.com/Server_Side/P...il2/page1.html
http://www.devshed.com/Server_Side/P...il3/page1.html

Yet another general IMAP tutorial:
http://www.devarticles.com/art/1/104

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Picking mail from Inbox sanjepau Biztalk 0 July 28th, 2007 08:42 AM
Filtering My Inbox steve-o General .NET 0 September 22nd, 2004 08:07 PM
MAPI inbox checker rodmcleay C# 1 September 11th, 2003 03:07 AM



All times are GMT -4. The time now is 10:01 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.