Hey guys

I am building web-based e-mail client and I need to be able to read file attachments. I have the following code:

$mbox = "{localhost:110/pop3}INBOX";
$remuserid = "$usname";
$remuserpassword = "$pw";
if(!$stream = imap_open($mbox,$remuserid,$remuserpassword)){
  $streamerror = "Connection to server failed";
}

if(!$headers = @imap_headers($stream)){
  $header = "No!";
}else{
  $numEmails = sizeof($headers);
}

if(isset($numEmails) &&($numEmails > 0)){
  for($i = 1; $i < $numEmails+1; $i++){
    $mailHeader = @imap_headerinfo($stream, $i);
    $from = $mailHeader->fromaddress;
    $subject = strip_tags($mailHeader->subject);        
$size = strip_tags($mailHeader->size); $msgNo = imap_msgno($stream, $i); $date = $mailHeader->date; list($dayName,$day,$month,$year,$time) = split(" ",$date); $time = substr($time,0,5); $date = strtotime($day." ".$month." ".$year." ".$time); $msgBody = imap_body($stream,$msgNo); //fetch the message parts $msgparts = imap_fetchstructure($stream,$msgNo); } }

I got as far as that and I have no idea where to go from here. What I do know is that I have to get the structure of the e-mail, then work out the structure type and then encoding so I can then decode the data into a file of some sort. How on earth does one do that? There are no tutorials on the net regarding this.

Bear in mind that the code above works for pulling the e-mail headers and body, I have no problem with that, I just need to read file attachments.

Your help would be most appreciated.

Thanks,
J

    6 days later

    There are, in fact, many tutorials on the net. What you're interested in is MIME multipart emails.

    Here's a good starting point - it's talking about sending multipart MIME (which is much more easily achieved by using PHPMailer or something similar), but it goes pretty in-depth into the syntax, so it applies equally to parsing emails in PHP.

    Here's a PEAR class for sending MIME email, with also includes methods for decoding.

      Write a Reply...