Jump to content

downloading attachments from mailclient


surion

Recommended Posts

hi,

 

i try to download attachments from a mailclient, but it doesn't seem to work

somehow, the code adds 2 spaces in the beginning of the attachment

 

for example the beginning of a jpg image, opened in notepad++ looks like this:

 

  ÿØÿàNULDLEJFIFNULSOHSOHSOHNUL`NUL`NULNUL

and so on, what it means doesn't realy matter for me, what matters is that while downloading the image those 2 spaces you see come in front of it on the first row, so the image wount load anymore, unless i open it with notepad++ and remove the spaces.

 

this is what my code looks like:

 

$filename = stripslashes ($filename);
$filename = str_replace ("'", "", $filename);

$struct = imap_fetchstructure ($stream, $msgid);

$coding = $struct->parts[$part_no]->encoding;
$message = imap_fetchbody ($stream, $msgid, ($part_no + 1))//$part_no is a getvar, doesn't matter for us here;
$message = decode($message,$coding);

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-Type: octet-stream"); 
header("Content-Disposition: attachment; filename=\"".$filename."\""); 
header("Content-Transfer-Encoding: binary");

echo trim($message);//as you can see i tried adding trim, but it doesn't solve the problem, spaces are still there

/*and this is what the decode function looks like:*/
function decode($data,$code)
{  
if(!$code)
       {
       return imap_utf7_decode($data);
       }
if($code==0)
       {
       return imap_utf7_decode($data);
       //return mb_convert_encoding ( $data, "ISO_8859-1", "UTF7-IMAP" );
       //return iconv ( "UTF7-IMAP", "ISO_8859-1", $data );
       //return mb_convert_encoding ( $data, "UTF7-IMAP", "ISO_8859-15" );
       }
if($code==1)
       {
       return imap_utf8($data);
       }
if($code==2)
       {
       return ($data);
       }
if($code==3)
       {
       return imap_base64($data);
       }
if($code==4)
       {
       return imap_qprint(str_replace($myqprinta,$myqprintb,($data)));
       }
if($code==5)
       {
       return ($data);
       }
} 


 

i guess there is no question about the existance of my variables, since if i remove the spaces manualy the attachment is correct,...

my guess is that it goes wrong with the imap_fetchbody function, or with the decode function, in this case i m talking about $coding is 1, so it is the utf7 thing it should return, as you can see i already tested a bit with mb_convert & iconv, but it doesn't seem to change the problem (well it actualy enlarges the problem, this "2-spaces-before-actual-data" is closest i got to realy downloading an attachment so far :P

 

*edit* forgot to add this: 'sry for my bad english & thx in advantage for any suggestions that might help me solve my problem'

Link to comment
https://forums.phpfreaks.com/topic/41417-downloading-attachments-from-mailclient/
Share on other sites

keep me alive again :P

 

tough i got a little futher now, after some testing and stuf i got to know that it is NOT

imap_fetchbody or the decode function that causes the spaces to appear, it happens somewhere before that.

so i checked all the includes (those for my mailserverconnection, databaseconnection and the one with my functions)

but none of those has empty spaces @ the beginning or @ the end and they don't echo spaces aswell, any suggestions?

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.