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
:::::::::::::::::::::::::::::::::