Jump to content

imap attachment file download


ted_chou12

Recommended Posts

Hello, I'm attempting to download the attachment from an message:

$structure = imap_fetchstructure($mbox, $id); 
$message = imap_fetchbody($mbox,$id, "1");    
$name = $structure->parts[1]->dparameters[0]->value; 
$type = $structure->parts[1]->type; 
############## type 
if ($type == 0) 
{ 
    $type = "text/"; 
} 
elseif ($type == 1) 
{ 
    $type = "multipart/"; 
} 
elseif ($type == 2) 
{ 
    $type = "message/"; 
} 
elseif ($type == 3) 
{ 
    $type = "application/"; 
} 
elseif ($type == 4) 
{ 
    $type = "audio/"; 
} 
elseif ($type == 5) 
{ 
    $type = "image/"; 
} 
elseif ($type == 6) 
{ 
    $type = "video"; 
} 
elseif($type == 7) 
{ 
    $type = "other/"; 
} 
$type .= $structure->parts[1]->subtype; 
######## Type end 

header("Content-type: ".$type); 
header("Content-Disposition: attachment; filename=".$name); 

The above gives the correct name, but the file itself seems to be empty, I dont know if the problem is the wrong parts[] or any other problem,

thanks for the help,

Ted

Link to comment
https://forums.phpfreaks.com/topic/137480-imap-attachment-file-download/
Share on other sites

  • 4 weeks later...

I have the same problem I can get the attachment name to appear but I can't for the life of me download the file if I do download anything its far to small.

 

Anyone managed to download attachments using IMAP?

 

here's my code:

 

//$mbox == imap connection
//$mid == message number
$info = imap_fetchstructure($mbox,$mid);

// find out how may parts the object has
$numparts = count($info->parts);

// find if if multipart message
if ($numparts > 1) {

   //echo "More then one part<BR>";
   
   foreach ($info->parts as $part) {

	  if ($part->disposition == "inline") {
		 // inline message. Show number of lines
	  
		// printf("<p>Inline message has %s lines<BR>", $part->lines."</p>");
	  
	  } elseif ($part->disposition == "attachment") {
		 // an attachment
	  
		 //echo "<p>Attachments</p>";
		 // print out the file name
		 //echo "<p>Filename: ", $part->dparameters[0]->value."</p>";			 


			//$file = $part->dparameters[0]->value;
			$file = $part->dparameters[0]->value;


				header('Content-Description: File Transfer');
				header('Content-Type: application/octet-stream');
				header('Content-Disposition: attachment; filename='.basename($file));
				header('Content-Transfer-Encoding: binary');
				header('Expires: 0');
				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
				header('Pragma: public');
				header('Content-Length: ' . filesize($file));
				ob_clean();
				flush();
				readfile($file);
				unlink ($file);	

  • 2 weeks later...

Finally got attachments working with IMAP

 

Here is my code in case it is useful to anyone else:

 

Function to download attachment

 

<?php
//--------------------- download attachment
function get_attached_file($mbox,$structure,$k,$mid)
{
$encoding = $structure->parts[$k]->encoding;
//extract file name from headers
$fileName = strtolower($structure->parts[$k]->dparameters[0]->value);
//extract attachment from email body
$fileSource = base64_decode(imap_fetchbody($mbox, $mid, $k+1));

//get extension
$ext = substr($fileName, strrpos($fileName, '.') + 1);
//die($ext);

//get mime file type
switch ($ext) {
case "asf":
	$type = "video/x-ms-asf";
	break;
case "avi":
	$type = "video/avi";
	break;
case "flv":
	$type = "video/x-flv";
	break;
case "fla":
	$type = "application/octet-stream";
	break;
case "swf":
	$type = "application/x-shockwave-flash";
	break;		
case "doc":
	$type = "application/msword";
	break;
case "docx":
	$type = "application/msword";
	break;
case "zip":
	$type = "application/zip";
	break;
case "xls":
	$type = "application/vnd.ms-excel";
	break;
case "gif":
	$type = "image/gif";
	break;
case "jpg" || "jpeg":
	$type = "image/jpg";
	break;
case "png":
	$type = "image/png";
	break;		
case "wav":
	$type = "audio/wav";
	break;
case "mp3":
	$type = "audio/mpeg3";
	break;
case "mpg" || "mpeg":
	$type = "video/mpeg";
	break;
case "rtf":
	$type = "application/rtf";
	break;
case "htm" || "html":
	$type = "text/html";
	break;
case "xml":
	$type = "text/xml";
	break;	
case "xsl":
	$type = "text/xsl";
	break;
case "css":
	$type = "text/css";
	break;
case "php":
	$type = "text/php";
	break;
case "txt":
	$type = "text/txt";
	break;
case "asp":
	$type = "text/asp";
	break;
case "pdf":
	$type = "application/pdf";
	break;
case "psd":
	$type = "application/octet-stream";
	break;
default:
	$type = "application/octet-stream";
}

//download file
header('Content-Description: File Transfer');
header('Content-Type: ' .$type);
header('Content-Disposition: attachment; filename='.$fileName);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($fileSource));
ob_clean();
flush();
echo $fileSource;
}
?>

 

Calling function:

 

<?php
        //$mbox = imap stream
//$mid = message id
//$parno = message part number
$partno = $_GET['download'];
$structure = imap_fetchstructure($mbox, $mid);
get_attached_file($mbox,$structure,$partno,$mid);
?>

  • 5 years later...

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.