IMAP attachments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

IMAP attachments

Post 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]
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

Post by hyper_st8 »

Guess no one knows....
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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; 
} 

?>
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Delete that bit... looks like a cut & paste problem sorry :oops:
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

Post by hyper_st8 »

Nothing you did, it is actually on the php.net website: http://uk2.php.net/pt_BR/imap-bodystruct
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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:
User avatar
hyper_st8
Forum Newbie
Posts: 20
Joined: Mon Aug 18, 2003 5:27 am
Contact:

Post 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!
Post Reply