drisate Posted February 11, 2009 Share Posted February 11, 2009 Hey guys ... how do you transform an IMAP attachement into a viewable image? I would like to create 200px wide previews This is the code i have so fare to check if i have attachements. <?php $mbox = @imap_open("{mail.***.com:143/imap/notls}" . urldecode($_GET[folder]), $_SESSION['session_username'], $_SESSION['session_password']); // delibertely choose a message with an attachment $info = imap_fetchstructure($mbox, $_GET[num]); // find out how may parts the object has $numparts = count($info->parts); $i=1; // find if multipart message if ($numparts >1) { foreach ($info->parts as $part) { if ($part->disposition == "INLINE") printf("Inline message has %s lines<BR>", $part->lines); elseif ($part->disposition == "attachment") { $i++; print "<a href='?mod=mail&CMD=Attachview&num=$_GET[num]&part=$i'><img src='http://www.jce-ecp.com/html/images/trombone2.png' height='12'> ".$part->dparameters[0]->value." ".convsize($part->bytes)."</A><BR>"; } } } ?> Quote Link to comment Share on other sites More sharing options...
drisate Posted February 11, 2009 Author Share Posted February 11, 2009 By the way ... the code above outputs a weird file name ... Ex: =?ISO-8859-1?Q?France_Le_syst=E8me_qui_travaille_pour_vous=2E=2Eppt?= (6.0MB) instead of only : France Le système qui travaille pour vous.ppt (6.0MB) Would be awsome to get that solved as well :-) Quote Link to comment Share on other sites More sharing options...
drisate Posted February 11, 2009 Author Share Posted February 11, 2009 This is a print_r on $part stdClass Object ( [type] => 3 [encoding] => 3 [ifsubtype] => 1 [subtype] => VND.MS-POWERPOINT [ifdescription] => 0 [ifid] => 0 [bytes] => 6252438 [ifdisposition] => 1 [disposition] => attachment [ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] => filename [value] => =?ISO-8859-1?Q?France_Le_syst=E8me_qui_travaille_pour_vous=2E=2Eppt?= ) ) [ifparameters] => 1 [parameters] => Array ( [0] => stdClass Object ( [attribute] => name [value] => =?ISO-8859-1?Q?France_Le_syst=E8me_qui_travaille_pour_vous=2E=2Eppt?= ) ) ) As you can see thats how i receive the filename ... Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Author Share Posted February 12, 2009 Anybody? Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Author Share Posted February 12, 2009 bump Quote Link to comment Share on other sites More sharing options...
printf Posted February 12, 2009 Share Posted February 12, 2009 I think you need to do some more reading because even if $numparts = count($info->parts); equals 0 there is still a message of type TEXT/PLAIN or RFC822. To decode a $obj->ifdparameters (IE: $obj->dparameters or $obj->parameters) VALUE you would use imap_mime_header_decode (). I have an example that will show you how to do what you want, once I remember where I put it I will post the attachment! Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Author Share Posted February 12, 2009 omg thx printf that would be awsome! I will be checking that board and helping while i whait. :-) Quote Link to comment Share on other sites More sharing options...
printf Posted February 12, 2009 Share Posted February 12, 2009 Anytime you read a parameter 'NAME, 'FILENAME' or want to decode a mail HEADER or MAIL BODY just pass that parameter to this function... // requires imap, mb_string extensions example... <?php $str = '=?ISO-8859-1?Q?France_Le_syst=E8me_qui_travaille_pour_vous=2E=2Eppt?= (6.0MB)'; // decode any string (mail raw head, body, parameter), from one encoding to another, default (utf8) function if_decode ( $string, $old = 'utf-8', $new = 'utf-8', $default = 'iso-8859-1' ) { $type = array_map ( 'strtolower', mb_list_encodings () ); $old = strtolower ( $old ); $new = strtolower ( $new ); $default = strtolower ( $default ); $keep = ''; $temp = imap_mime_header_decode ( $string ); $i = sizeof ( $temp ); for ( $j = 0; $j < $i; $j++ ) { $temp[$j]->charset = strtolower ( $temp[$j]->charset ); if ( $temp[$j]->charset == 'default' && $old == $new || $temp[$j]->charset == $new ) { $keep .= $temp[$j]->text; } else { $keep .= mb_convert_encoding ( $temp[$j]->text, $new, ( in_array ( $temp[$j]->charset, $type ) ? $temp[$j]->charset : $default ) ); } } return $keep; } echo if_decode ( $str ); ?> That's how to decode any encoded string (it will handle any encoding), I still will post a full example of reading through a mail box and decoding each message, placing the parts on the file system and returning an array so you can fetch each decoded part (text, html bodies) (attachments, any type that you allow). I found the example, I am just adding some comments so you can understand everything it is doing. Quote Link to comment Share on other sites More sharing options...
drisate Posted February 12, 2009 Author Share Posted February 12, 2009 OMG thank you thank you thank you printf ! You just made my day ! woohoo ! Quote Link to comment Share on other sites More sharing options...
printf Posted February 12, 2009 Share Posted February 12, 2009 This example is very simple, I made it to sync my GMAIL account with a local database, that I snyc to my phone, but it should give you an idea on how to completely decode a message or a whole mail box of messages. Hopefully its a good starting point for you. To run the example just set the values in the script and create the needed directories that the script can write to! It uses UID(s), so it will never over_write any data (messages and or attachments) [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.