Jump to content

[SOLVED] IMAP Fetching Attachment


ballouta

Recommended Posts

Hello

 

i have been working on this piece of script for serveral days. I googled for solutions many times but nothing. I feel i wanna shoort myself. Kindly please help me:

 

first, all messages coming to this email contains always only 1 attachment (zip archive)

I need a piece of code to copy this zip archive file to an existing directory.

 

i am NOT expert to discover or rewrite these lines:

 

i am successfully able to open the inbox using Imap functions, echo the headers, and the attach file name:

 

The BIG problem is that the phpzip functions is not working because the file is corrupted during copy process.

i am sure that the original file is correct, and if I use the webmail it looks perfect.

As if my function is copyng the file name only and not the real file, this is why it is only always 1KB.

 

if ($check->Nmsgs=='0')	{
echo "There are no messages that need to be downloaded"; 	
}	else	{

$emailIndex = 1;

/* Lets find the email body */
$struct = imap_fetchstructure($mailbox, $emailIndex);
$parts = $struct->parts;
$i = 0;

if (!$parts) { /* Simple message, only 1 piece */
  $attachment = array(); /* No attachments */
  $Email_Comments = imap_body($mailbox, $emailIndex);
} else { /* Complicated message, multiple parts */

  $endwhile = false;

  $stack = array(); /* Stack while parsing message */
  $Email_Comments = "";    /* Content of message */
  $attachment = array(); /* Attachments */

  while (!$endwhile) {
    if (!$parts[$i]) {
      if (count($stack) > 0) {
        $parts = $stack[count($stack)-1]["p"];
        $i     = $stack[count($stack)-1]["i"] + 1;
        array_pop($stack);
      } else {
        $endwhile = true;
      }
    }

    if (!$endwhile) {
      /* Create message part first (example '1.2.3') */
      $partstring = "";
      foreach ($stack as $s) {
        $partstring .= ($s["i"]+1) . ".";
      }
      $partstring .= ($i+1);
   
      if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */
        $attachment[] = array("filename" => $parts[$i]->parameters[0]->value,
                              "filedata" => @imap_fetchbody($mailbox, $emailIndex, $partstring));
      } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */
        $Email_Comments .= imap_fetchbody($mailbox, $emailIndex, $partstring);
		echo "<b>Email Body:</b> $Email_Comments <br/><br/>";
		$daouk=$Email_Comments; // my code
		$emailIndex +=1;
      }
    }

    if ($parts[$i]->parts) {
      $stack[] = array("p" => $parts, "i" => $i);
      $parts = $parts[$i]->parts;
      $i = 0;
    } else {
      $i++;
    }
  } /* while */
} /* complicated message */

	foreach ($attachment as $attach)	{
		WriteToFile($SaveFileDirectory . $attach['filename'], imap_base64($attach['filedata']));
		echo "Attachment found: " . $attach['filename'] . "<br>";
		$myf=$attach['filename'];
	}
} // end else

 

Please serious help and solution.

I appreciate your kindness.

Link to comment
https://forums.phpfreaks.com/topic/110994-solved-imap-fetching-attachment/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.