Jump to content

Small question


drisate

Recommended Posts

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>";
     }
   }

}
?>

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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 ...

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.