|
Posted by slimdizzy on 08/04/06 17:27
I have a bit of code that when using POP in the imap_open connect
string it will return the filenames of the attachments, but when using
IMAP it does not.
Been racking my brain over this for a week now and decided to ask in
here for some help.
CODE:
// open mailbox
$inbox = @imap_open("{" . $SESSION_EMAILSERVER .
"/imap:143/notls/novalidate-cert}", $SESSION_USERNAME,
$SESSION_PASSWORD) or die("Could not open Mailbox - try again later!");
// parse message body
function parse($structure)
{
global $type;
global $encoding;
// create an array to hold message sections
$ret = array();
// split structure into parts
$parts = $structure->parts;
for($x=0; $x<sizeof($parts); $x++)
{
$ret[$x]["pid"] = ($x+1);
$this = $parts[$x];
// default to text
if ($this->type == "") { $this->type = 0; }
$ret[$x]["type"] = $type[$this->type] . "/" .
strtolower($this->subtype);
// default to 7bit
if ($this->encoding == "") { $this->encoding = 0; }
$ret[$x]["encoding"] = $encoding[$this->encoding];
$ret[$x]["size"] = strtolower($this->bytes);
$ret[$x]["disposition"] =
strtolower($this->disposition);
if (strtolower($this->disposition) == "attachment")
{
$params = $this->dparameters;
foreach ($params as $p)
{
if($p->attribute == "FILENAME")
{
$ret[$x]["name"] = $p->value;
break;
}
}
}
}
return $ret;
}
// iterate through object returned by parse()
// create a new array holding information only on message attachments
function get_attachments($arr)
{
for($x=0; $x<sizeof($arr); $x++)
{
if($arr[$x]["disposition"] == "attachment")
{
$ret[] = $arr[$x];
}
}
return $ret;
}
// get message headers and structure
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);
// if multipart, parse
if(sizeof($structure->parts) > 1)
{
$sections = parse($structure);
$attachments = get_attachments($sections);
}
// if attachments exist
if (is_array($attachments))
{
// display as list
for($x=0; $x<sizeof($attachments); $x++)
{
echo "<li><a href=download.php?id=$id&pid=" .
$attachments[$x]["pid"]
.. ">" . $attachments[$x]["name"] . " (" .
ceil($attachments[$x]["size"]/1024) . " KB)</a>";
}
}
Any help is greatly appreciated. TIA
slim
Navigation:
[Reply to this message]
|