Hi,
I use the following code to fetch any attachments in any given "$msgid" see below "$s=imap_fetchstructure($link,$msgid); "
This works great if I define witch $msgid = 2; number of the message i want to see.
But what Iam really looking 4 is that it loops all the message and self_define the $msgid = being all of the message.
Do i use somekind of loop?
What can I do?
/Rasmus
<?php
//script will fetch an email identified by $msgid, and parse the its parts into an
//array $partsarray
//structure of array:
//$partsarray[<name of part>][<attachment/text>]
//if attachment- subarray is [filename][binary data]
//if text- subarray is [type of text(HTML/PLAIN)][text string]
//i.e.
//$partsarray[3.1][attachment][filename]=filename of attachment in part 3.1
//$partsarray[3.1][attachment][binary]=binary data of attachment in part 3.1
//$partsarray[2][text][type]=type of text in part 2
//$partsarray[2][text][string]=decoded text string in part 2
//$partsarray[not multipart][text][string]=decoded text string in message that isn't multipart
function parsepart($p,$i){
global $link,$msgid,$partsarray;
//where to write file attachments to:
$filestore = '/customers/attachments/';
//fetch part
$part=imap_fetchbody($link,$msgid,$i);
//if type is not text
if ($p->type!=0){
//DECODE PART
//decode if base64
if ($p->encoding==3)$part=base64_decode($part);
//decode if quoted printable
if ($p->encoding==4)$part=quoted_printable_decode($part);
//no need to decode binary or 8bit!
//get filename of attachment if present
$filename='';
// if there are any dparameters present in this part
if (count($p->dparameters)>0){
foreach ($p->dparameters as $dparam){
if ((strtoupper($dparam->attribute)=='NAME') ||(strtoupper($dparam->attribute)=='FILENAME')) $filename=$dparam->value;
}
}
//if no filename found
if ($filename==''){
// if there are any parameters present in this part
if (count($p->parameters)>0){
foreach ($p->parameters as $param){
if ((strtoupper($param->attribute)=='NAME') ||(strtoupper($param->attribute)=='FILENAME')) $filename=$param->value;
}
}
}
//write to disk and set partsarray variable
if ($filename!=''){
$partsarray[$i][attachment] = array('filename'=>$filename,'binary'=>$part);
$fp=fopen($filestore.$filename,"w+");
fwrite($fp,$part);
fclose($fp);
}
//end if type!=0
}
//if part is text
else if($p->type==0){
//decode text
//if QUOTED-PRINTABLE
if ($p->encoding==4) $part=quoted_printable_decode($part);
//if base 64
if ($p->encoding==3) $part=base64_decode($part);
//OPTIONAL PROCESSING e.g. nl2br for plain text
//if plain text
if (strtoupper($p->subtype)=='PLAIN')1;
//if HTML
else if (strtoupper($p->subtype)=='HTML')1;
$partsarray[$i][text] = array('type'=>$p->subtype,'string'=>$part);
}
//if subparts... recurse into function and parse them too!
if (count($p->parts)>0){
foreach ($p->parts as $pno=>$parr){
parsepart($parr,($i.'.'.($pno+1)));
}
}
}
//open resource
$link=imap_open("{localhost:143}INBOX",'USERNAME,'PASSWORD');
//fetch structure of message
$MC = imap_check($link);
// Fetch an overview for all messages in INBOX
$result = imap_fetch_overview($link,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {
echo "{$overview->msgno}\n";
// echo $result;
}
$s=imap_fetchstructure($link,$msgid);
//see if there are any parts
if (count($s->parts)>0){
foreach ($s->parts as $partno=>$partarr){
//parse parts of email
parsepart($partarr,$partno+1);
}
}
//for not multipart messages
else{
//get body of message
$text=imap_body($link,$msgid);
//decode if quoted-printable
if ($s->encoding==4) $text=quoted_printable_decode($text);
//OPTIONAL PROCESSING
if (strtoupper($s->subtype)=='PLAIN') $text=$text;
if (strtoupper($s->subtype)=='HTML') $text=$text;
$partsarray['not multipart'][text]=array('type'=>$s->subtype,'string'=>$text);
}
print_r($partsarray);
?>
<?
$folder = "attachments/";
$handle = opendir($folder);
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..") {
echo ("<img src=\"".$folder.$file."\"></br>");
}
}
?>
imap_fetchstructure loop msgid problem?
Moderator: General Moderators
Re: imap_fetchstructure loop msgid problem?
Do you not understand this code because you've taken it from somewhere else? I'm asking because it seems obvious to me you need to loop through the message numbers found in your imap_check results which returns the number of messages available. Perhaps I'm missing something?
Code: Select all
//fetch structure of message
$MC = imap_check($link);
// Fetch an overview for all messages in INBOX
$result = imap_fetch_overview($link,"1:{$MC->Nmsgs}",0); // Nmsgs is the number of messages
foreach ($result as $overview) {
echo "{$overview->msgno}\n"; // HERE ARE YOUR MESSAGE NUMBERS TO LOOP THROUGH, is this echo working properly?
// echo $result;
}
//LOOP THROUGH THEM FOR $msgid here and include the code you want to parse it
$s=imap_fetchstructure($link,$msgid);