Page 1 of 1

IMAP attachments

Posted: Fri Mar 18, 2005 8:39 am
by hyper_st8
Hello!
I was wondering if anyone knew how you can get a attachment from an e-mail through imap, in php?

I found a function that gets all parts of an e-mail (except for the attachment's data), including the file name of the attachment and puts it into an array:

Code: Select all

<?
function retrieve_message($mbox, $messageid)
{
   $message = array();
  
   $header = imap_header($mbox, $messageid);
   $structure = imap_fetchstructure($mbox, $messageid);

   $message['subject'] = $header->subject;
   $message['fromaddress'] =  $header->fromaddress;
   $message['toaddress'] =  $header->toaddress;
   $message['ccaddress'] =  $header->ccaddress;
   $message['date'] =  $header->date;

  if (check_type($structure))
  {
   $message['body'] = imap_fetchbody($mbox,$messageid,"1"); ## GET THE BODY OF MULTI-PART MESSAGE
   if(!$message['body']) {$message['body'] = '[NO TEXT ENTERED INTO THE MESSAGE]\n\n';}
  }
  else
  {
   $message['body'] = imap_body($mbox, $messageid);
   if(!$message['body']) {$message['body'] = '[NO TEXT ENTERED INTO THE MESSAGE]\n\n';}
  }
  
  return $message;
}
?>

But I have looked high and low (from here, php.net to searching through google) to find something that actually receives the e-mail's attachment data.

Any help would be greatly appreciated!
Thanks!!!


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Mar 21, 2005 4:06 am
by hyper_st8
Guess no one knows....

Posted: Mon Mar 21, 2005 5:55 am
by Chris Corbyn
From PHP.net....

Essenitally, you read the body structure, check the part that has the attachment and then fetch that part.

You'll need to header() the content-type and file details back out from a PHP script to get the file to download :wink:

Code: Select all

<?php 

function getAttachments($mbox,$msgNum){ 
  $structure = imap_fetchstructure($mbox,$msgNum); 
  $contentParts = count($structure->parts); 
  if($contentParts > 1){ 
   for($i=1; $i<=$contentParts; $i++){ 
         if($structure->parts[$i]->type > 0){ 
       $attachments .= $structure->parts[$i]->description; 
     } 
   } 
  } 
  return $attachments; 
} 
  } 
  return $attachments; 
} 

?>

Posted: Wed Mar 23, 2005 1:51 am
by hyper_st8
Cheers d11wtq, but I have found that the function you have supplied previously and it didn't work. (Sorry I should of mentioned it before). Also the function seems to repeat the last bit, causing an error in the function...

Code: Select all

}
  return $attachments;
}
Cheers d11wtq

Posted: Wed Mar 23, 2005 4:46 am
by Chris Corbyn
Delete that bit... looks like a cut & paste problem sorry :oops:

Posted: Wed Mar 23, 2005 6:50 am
by hyper_st8
Nothing you did, it is actually on the php.net website: http://uk2.php.net/pt_BR/imap-bodystruct

Posted: Wed Mar 23, 2005 7:26 am
by Chris Corbyn
By the way.... when you call it just replace $msgnum with $messageid in the arguments.

Alternatively if you look inside your function to get the message body and this one to get the attachments you notice any similarities? Break this second function down and modify the function you first posted to return an array containing the message body and it's attachments :wink:

Posted: Tue Mar 29, 2005 4:28 am
by hyper_st8
Think I know what you mean, but I think I have gone wrong somewhere else, so when I get the time I'll have a look at it. When it's completed I'll post it here (if you're interested).

Cheers for the help d11wtq!