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
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

IMAP - Attachments

Post by gurjit »

Hi all,

i'm currently writing some IMAP script for checking and sending mail. I'm crossing the bridge of reading attachments sent. Anyone have any idea how to cross this bridge.

The script below does not work. I want to be able to display the attachments sent. When you click on the attachment link i want to be able to save to my local machine. Not sure why this script does'nt work.

I use this script to bring out the attachment as a link on my view_message.php page

Code: Select all

<?
   $struct = imap_fetchstructure($link,$msgno);
   $contentParts = count($struct->parts);
  
   if ($contentParts >= 2) {
	   for ($i=2;$i<=$contentParts;$i++) {
   	$att[$i-2] = imap_bodystruct($link,$msgno,$i);
	
   	}
	
   	for ($k=0;$k<sizeof($att);$k++) {

   		if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value    == "US-ASCII") {
   			if ($att[$k]->parameters[1]->value != "") {
   				$selectBoxDisplay[$k] = $att[$k]->parameters[1]->value;
   			}
   		} elseif ($att[$k]->parameters[0]->value != "iso-8859-1" &&    $att[$k]->parameters[0]->value != "ISO-8859-1") {
   			$selectBoxDisplay[$k] = $att[$k]->parameters[0]->value;

		}
   	}
   }
   
   if (sizeof($selectBoxDisplay) > 0) {
   	echo "<select name="attachments" size="3" class="tblContent"    onChange="handleFile(this.value)" style="width:170;">";
   	for ($j=0;$j<sizeof($selectBoxDisplay);$j++) {
   		echo "\n<option value="0">". $selectBoxDisplay[$j]    ."</option>";
   	}
   	echo "</select>";
   }

?>
The javascript on this page is:

Code: Select all

&lt;script language="JavaScript"&gt;
   var b;
   browser = navigator.appName;
   if (browser == "Microsoft Internet Explorer") &#123;
   	b = "ie";
   &#125; else &#123;
   	b = "other";
   &#125;
   
   function handleFile(nr) &#123;
   	if (b != "ie") &#123;
   		alert("This feature is currently only available for Microsoft Internet Explorer 5.5+ users\n\nWait for an update!");
   	&#125; else &#123;
   		check = confirm("Do you want to download the file ?");
   		if (check) &#123;
   			setTimeout("this.location.reload()",8000);
	
   			location.href="download_attachment.php?download=1&amp;file="+ nr +"&amp;msgno=&lt;?= $msgno ?&gt;";
   		&#125; else &#123;
   			location.reload();
   		&#125;
   	&#125;
   &#125;
&lt;/script&gt;

My download_attachment.php has this code

Code: Select all

<?php
function downloadFile($strFileType,$strFileName,$fileContent) {
   	$ContentType = "application/octet-stream";
   
   	if ($strFileType == ".asf") 
   		$ContentType = "video/x-ms-asf";
   	if ($strFileType == ".avi")
   		$ContentType = "video/avi";
   	if ($strFileType == ".doc")
   		$ContentType = "application/msword";
   	if ($strFileType == ".zip")
   		$ContentType = "application/zip";
   	if ($strFileType == ".xls")
   		$ContentType = "application/vnd.ms-excel";
   	if ($strFileType == ".gif")
   		$ContentType = "image/gif";
   	if ($strFileType == ".jpg" || $strFileType == "jpeg")
   		$ContentType = "image/jpeg";
   	if ($strFileType == ".wav")
   		$ContentType = "audio/wav";
   	if ($strFileType == ".mp3")
   		$ContentType = "audio/mpeg3";
   	if ($strFileType == ".mpg" || $strFileType == "mpeg")
   		$ContentType = "video/mpeg";
   	if ($strFileType == ".rtf")
   		$ContentType = "application/rtf";
   	if ($strFileType == ".htm" || $strFileType == "html")
   		$ContentType = "text/html";
   	if ($strFileType == ".xml") 
   		$ContentType = "text/xml";
   	if ($strFileType == ".xsl") 
   		$ContentType = "text/xsl";
   	if ($strFileType == ".css") 
   		$ContentType = "text/css";
   	if ($strFileType == ".php") 
   		$ContentType = "text/php";
   	if ($strFileType == ".asp") 
   		$ContentType = "text/asp";
   	if ($strFileType == ".pdf")
   		$ContentType = "application/pdf";
		
	}	
	
$username = "mytest";
$password = "123";
$mbox=imap_open("{localhost:143/imap/notls}","$username","$password")
     or die("can't connect: ".imap_last_error());
	 
   if ($download == "1") {
   	$strFileName = $att[$file]->parameters[0]->value;
   	$strFileType = strrev(substr(strrev($strFileName),0,4));
   	$fileContent = imap_fetchbody($mbox,$msgno,$file+2);
   	downloadFile($strFileType,$strFileName,$fileContent);
   }
   
   
   
   	header ("Content-Type: $ContentType"); 
   	header ("Content-Disposition: attachment; filename=2_del.gif");    
   	echo $fileContent;
?>

feyd | added

Code: Select all

tags for javascript[/color]
Post Reply