Jump to content

Email attachments with IMAP


Bricktop

Recommended Posts

Hi all,

 

I'm writing a very basic webmail client using the IMAP protocol and am struggling with downloading attachments.  My code at present displays the attachments fine, and I found a snippet of code here on phpfreaks.com which should allow for the download of the attachments but I can't get it to work - I think the download funtion requires the message part number but I'm not sure how to get it from the message.

 

Anyway, here's my code:

 

Display attachments:

$struct = imap_fetchstructure($mbox,$overview[0]->msgno);
$contentParts = count($struct->parts);

if($contentParts > 1) 
	{
	foreach($struct->parts as $part) {
   		
	// this checks if its an attachment
	$part->disposition = strtolower($part->disposition);
   		if ($part->disposition == "attachment" || $part->disposition == "inline") 
	{
       
       	// get the filename
       	$filename = $part->dparameters[0]->value;
	$content .= '<div class="attachments">';
   		$content .= '<span class="attachmentsheader">Attachments:</span>';
   		$content .= '<p class="attachmentsdetail"><a href="?fct=download&filename='.$filename.'&msgno='.$overview[0]->msgno.'">'.$filename.'</a></p>';
	$content .= '</div>';
   		}
	}
	}

 

 

Download code:

function download()
{
	global $hostname,$username,$password;

$filename = $_GET['filename'];
$msgno = $_GET['msgno'];

	/* get information specific to this email */
	$mbox = imap_open($hostname,$username,$password);

$fileContent = imap_fetchbody($mbox,$msgno,$file+2);

$file = $_REQUEST['file'];

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
@readfile($file);
}

 

Any help greatly appreciated!  Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/172667-email-attachments-with-imap/
Share on other sites

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.