email - PHP Imap Download Attachment Script Not working -
my script works fine unless there image in body, doesn't work. can't seem figure out. below code:
if(!isset($http_user_agent)) $http_user_agent = $_server['http_user_agent']; $isie = $isie6 = 0; $mbox = imap_open("{localhost:143/novalidate-cert}inbox", $email, $login_pass); $partno = "2"; $filename = base64_decode($_get[filename]);
function structure
function get_structure($structure) { $primarymimetype = array("text", "multipart", "message", "application", "audio", "image", "video", "other"); if ($structure->subtype) { return $primarymimetype[(int)$structure->type] . "/" . $structure->subtype; } return "text/plain"; }
function mime_content_type (has been edited reduce size)
function mime_content_type($filename) { $mime_types = array( 'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', // images 'png' => 'image/png', 'jpg' => 'image/jpeg', // archives 'zip' => 'application/zip', // audio/video 'mp3' => 'audio/mpeg', // adobe 'pdf' => 'application/pdf', // ms office 'doc' => 'application/msword', ); $ext = pathinfo($filename, pathinfo_extension); if (array_key_exists($ext, $mime_types)) { return $mime_types[$ext]; } else { } }//end mime_type
get body attachment
function get_body_attach($mbox, $uid) { $struct = imap_fetchstructure($mbox, $uid); $parts = $struct->parts; $i = 0; if (!$parts) { /* simple message, 1 piece */ $attachment = array(); /* no attachments */ $content = imap_body($mbox, $uid); } else { /* complicated message, multiple parts */ $endwhile = false; $stack = array(); /* stack while parsing message */ $content = ""; /* content of message */ $attachment = array(); /* attachments */ while (!$endwhile) { if (!$parts[$i]) { if (count($stack) > 0) { $parts = $stack[count($stack)-1]["p"]; $i = $stack[count($stack)-1]["i"] + 1; array_pop($stack); } else { $endwhile = true; } } if (!$endwhile) { /* create message part first (example '1.2.3') */ $partstring = ""; foreach ($stack $s) { $partstring .= ($s["i"]+1) . "."; } $partstring .= ($i+1); if (strtoupper($parts[$i]->disposition) == "attachment" || strtoupper($parts[$i]->disposition) == "inline") { /* attachment or inline images */ $filedata = imap_fetchbody($mbox, $uid, $partstring); if ( $filedata != '' ) { // handles base64 encoding or plain text $decoded_data = base64_decode($filedata); if ( $decoded_data == false ) { $attachment[] = array("filename" => $parts[$i]->parameters[0]->value, "filedata" => $filedata); } else { $attachment[] = array("filename" => $parts[$i]->parameters[0]->value, "filedata" => $decoded_data); } } } elseif (strtoupper($parts[$i]->subtype) == "plain" && strtoupper($parts[$i+1]->subtype) != "html") { /* plain text message */ $content .= imap_fetchbody($mbox, $uid, $partstring); } elseif ( strtoupper($parts[$i]->subtype) == "html" ) { /* html message takes priority */ $content .= imap_fetchbody($mbox, $uid, $partstring); } } if ($parts[$i]->parts) { if ( $parts[$i]->subtype != 'related' ) { // glitch: embedded email message have 1 additional stack in structure subtype 'related', stack not present when using imap_fetchbody() fetch parts. $stack[] = array("p" => $parts, "i" => $i); } $parts = $parts[$i]->parts; $i = 0; } else { $i++; } } /* while */ } /* complicated message */ $ret = array(); $ret['body'] = quoted_printable_decode($content); $ret['attachment'] = $attachment; return $ret; //return quoted_printable_decode($content); }//end $mime_type = mime_content_type($filename); $body_attachments = get_body_attach($mbox, $uid); $attachments = $body_attachments['attachment']; if($attachments != ""){ //print_r($attachments); $path = $root_folder.'/attachments/'; foreach( $attachments $key){ if($filename == $key[filename]){ $filetype = mime_content_type($key[filename]); $fname = $key[filename]; $fname = preg_replace('{[\\/:\*\?"<>\|;]}', '_', str_replace(' ', ' ', $fname)); //$fname = $email.'_'.$uid.'_'.$fname; //get_file_header($fname); $fp = fopen("$path/$fname","w"); fwrite($fp, $key[filedata]); fclose($fp); $file = "$path/$fname"; // adjust accordingly 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); }} $filedata = ""; $fname = ""; $fp = ""; }
a filler word apparently meaningless word, phrase, or sound marks pause or hesitation in speech. known pause filler or hesitation form. of common filler words in english um, uh, er, ah, like, okay, right, , know.
Comments
Post a Comment