sutt0n 0 Newbie Poster

Ok, so basically I want to be able to do this. I have it all down from obtaining the content from the e-mail and saving the attachment, but when I save the file's content into a file (permissions aren't a problem, as you'll see in the code) it doesn't show it at all. It just says the image's path when I try to pull it up in my browser, so I'm guessing there's got to be a mime-type or decoding error happening.

What I'm doing is reading a catch-all mailbox account, and pulling any attachments (only images and videos, though, but that's not the problem) that are within the e-mail(s). Later on, I'll delete the e-mail message, but that's not important right now. I'm just having some attachment parsing problems due to either encoding problems or mime-types. Hopefully it's an easy fix.

Here's the code I'm using:

if ($imap = imap_open("{".$server."/imap/notls}INBOX",$user, $pass))  {
		/*echo "Total Messages: ". sizeof(imap_headers($imap));
		$headers = imap_headers($imap);
		echo "<pre>\n";
		print_r($headers);
		echo "</pre>\n";
		echo "<hr />\n";
		echo "<pre>\n";*/
		$y = 0;
		$attachments = array();
		foreach (imap_headers($imap) as $head) {
			print_r(imap_fetchstructure($imap, $y));
			$y++;
		}
		foreach (imap_headers($imap) as $head) {
			//electrictoolbox.com reference
			$structure = imap_fetchstructure($imap, $y);
			if(isset($structure->parts) && count($structure->parts)) {
				for($i = 0; $i < count($structure->parts); $i++) {
					$attachments[$i] = array(
						'is_attachment' => false,
						'filename' => '',
						'name' => '',
						'attachment' => ''
					);
		
					if($structure->parts[$i]->ifdparameters) {
						foreach($structure->parts[$i]->dparameters as $object) {
							if(strtolower($object->attribute) == 'filename') {
								$attachments[$i]['is_attachment'] = true;
								$attachments[$i]['filename'] = $object->value;
							}
						}
					}
		
					if($structure->parts[$i]->ifparameters) {
						foreach($structure->parts[$i]->parameters as $object) {
							if(strtolower($object->attribute) == 'name') {
								$attachments[$i]['is_attachment'] = true;
								$attachments[$i]['name'] = $object->value;
							}
						}
					}
		
					if($attachments[$i]['is_attachment']) {
						$attachments[$i]['attachment'] = imap_fetchbody($imap, $y, $i+1);
							if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
								$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
							} elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
								$attachments[$i]['attachment'] = imap_qprint($attachments[$i]['attachment']);
							}
					}
				}
			}
			$y++;
		}
		foreach ($attachments as $file) {
			$filename = $file['name'];
			$extension = end(explode(".", $file['name']));

			if (in_array($extension, $extensions)) {
				if ($extension == "mpeg" || $extension == "mpg" || $extension == "avi" || $extension == "wmv") {
					//it's a video
				} else {
					//it's an image
					$content = $file['attachment'];
					$explode = explode(".".$extension, $file['name']);
					
					

				}
					
			}
		}
	} else {
		//handle error
		exit;
	}