PHP Classes

Base64 attachment gets corrupted

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  Base64 attachment gets corrupted  >  (Un) Subscribe thread alerts  
Subject:Base64 attachment gets corrupted
Summary:Base64 attachment gets corrupted becuase the data isnt decoded?
Messages:10
Author:Pontus Lundin
Date:2011-10-07 02:30:21
Update:2011-10-08 03:50:07
 

  1. Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Pontus Lundin Pontus Lundin - 2011-10-07 02:30:21
Hi,

Great script,thank you!

However,when sending base64 attachments and trying to save them as files using fopen with ftp and make sure $part['FileDisposition'] == 'attachment' :

fwrite($fp,$part['Body']);

The new file only gets 70-80% of the decoded base64 data,as such pdf,pptx etc gets corrupted and images cut.

Looking at the mime parser source code,is it correct to really do strlen as you did below ?


elseif(IsSet($this->headers['Base64']))
{
$part['Data'] = base64_decode($this->body_buffer_position ? substr($this->body_buffer,$this->body_buffer_position) : $this->body_buffer);
$this->body_offset += strlen($this->body_buffer) - $this->body_buffer_position;
$this->body_buffer_position = 0;
$this->body_buffer = '';
}

Any advice ?

Thanks!

  2. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Pontus Lundin Pontus Lundin - 2011-10-07 02:40:19 - In reply to message 1 from Pontus Lundin
And i should also add this is when the attachment is larger than a few kb,having a very small images or pdf always works perfectly.

I have looked at the memory setting in php.ini and it is 128mb,as well as long excection time etc.Something is wrong when the files is getting larger and trying to decode the base64 and fwrite it to the server.

  3. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2011-10-07 03:19:46 - In reply to message 1 from Pontus Lundin
Can you please provide a sample e-mail file complete with headers and upload it somewhere so I can try to reproduce the problem?

  4. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Pontus Lundin Pontus Lundin - 2011-10-07 10:03:31 - In reply to message 3 from Manuel Lemos
Hi Manuel!

Thank you for quick replay.

Yes, i have uploaded the documents.

Here is the raw email parts:
mediafire.com/?t7ka38gr97j4iu1

the parse script (where fwrite happens):
mediafire.com/?npv9d8yrciv2fo7

The resulting (cropped) image (it has been cut):
mediafire.com/?8qnt5cagbxuvh8p

Again,thank you!

  5. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2011-10-07 20:59:56 - In reply to message 4 from Pontus Lundin
I am able to decode the message correctly an extract the image file with 769088 bytes.

Your problem may be in the actual FTP transfer. Are you sure you have enough FTP quota to transfer files this large?

Could it be a bug in the FTP extension of the PHP version you are using?

Have you tried using the SaveBody option of the Decode function to see if the whole image is saved to a file?

  6. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Pontus Lundin Pontus Lundin - 2011-10-07 21:48:20 - In reply to message 5 from Manuel Lemos
Thanks for your great help here Manual.

Now i know the script works at least.I will report back if i find any problem with my PHP installation and extension.

You mentioned the SaveBody option. So am right that i can just specify:

$parameters=array(
'Data'=>$email,
'SaveBody'=>'path to a folder'

);

and the path can be relative to the script (that is running under htdocs)
will save the attachment as files to the server? Hm, i think i have tried it once,and it failed.But i will give it yet another try (if the code is correct) ?

Thank you!

  7. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2011-10-07 22:01:03 - In reply to message 6 from Pontus Lundin
The path is relative to the current work directory, which initially is your start PHP script directory.

  8. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Pontus Lundin Pontus Lundin - 2011-10-07 22:34:17 - In reply to message 7 from Manuel Lemos
Success!!

The savebody does indeed work! How can i rename them correctly ? (they get saved as the part integer on the server). I guess i have to do it with the ordinary php methods like rename and move_files etc, no out of the box methods in your script right (other than i get the filename string)?

Really if this works in production i will look into donations for this site/scripts!

Thank you, better than paid support,amazing!

  9. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Pontus Lundin Pontus Lundin - 2011-10-08 00:14:52 - In reply to message 7 from Manuel Lemos
Manuel, just a last question on the body parts.How can i rename them from the 1,2,3..n value on the sever to the FileName (or is the BodyFile?)

I tried:

foreach($decoded[0]['Parts'] as $part){
$counter++;
//check for attachments
if($part['FileDisposition'] == 'attachment'){

//format file name anything that isn't a letter, number or derscore)
$filename = preg_replace('/[^0-9,a-z,\.,_]*/i','',str_replace(' ','_', $part['FileName']));

rename('/var/path/'.$counter, "/var/path/$filename");
}

But the counter in the loop is not synced with the saved body parts,how come ?

  10. Re: Base64 attachment gets corrupted   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2011-10-08 03:50:08 - In reply to message 9 from Pontus Lundin
Set the class variable use_part_file_names to 1.