How to decode an email attachment received as a Base64 text
Asked Answered
P

2

9

I have an email backup file which is purely text. How can I retrieve the document (PDF, images, word files) attached to it as a normal file?

Palliative answered 13/10, 2014 at 10:36 Comment(1)
If you are on Linux, the base64 program does a splendid job of converting the Base64 string (e.g., JVBERi0xLjUNCiW1tbW) to your target format. You can find some examples here.Malisamalison
P
9
  1. Select the long string of text which appears in your email. That is probably one of the attachments, it usually starts like this:

    --bcaec554d754b0f76a04d9fda578--
    --bcaec554d754b0f77204d9fda57a
    Content-Type: application/pdf; name="test.pdf"
    Content-Disposition: attachment; filename="Otest.pdf"
    Content-Transfer-Encoding: base64
    X-Attachment-Id: 9ba6310dffca527f_0.1
    
  2. Copy this long string and paste it in the Base64 decoder found here.

  3. Download the output and rename it by adding the appropriate extension to it. For example testfile.pdf or filename.docx.

There you go. You just recreated your lost attachment using Base64 decoding.

Palliative answered 13/10, 2014 at 10:36 Comment(4)
What if I want to do the decoding on my own in a programatical way?Quatrain
@OzanKurt Can you please give a bit of more context as to which programming language you intend to use, and will you be running this locally or on a specific server. Also is fetching email attachments involved etc?Palliative
I already managed to make it work via PHP. It somehow didn't work when I did the process manually though... Kind of weird.Quatrain
@OzanKurt Perfect! Glad you got sorted. Maybe best if you could summarise your answer here below for anyone else who might be interested. Happy codingPalliative
G
0

This is how to do it in PHP decode to a file

 function base64_to_jpeg( $inputfile, $outputfile ) { 
  /* read data (binary) */ 
  $ifp = fopen( $inputfile, "rb" ); 
  $imageData = fread( $ifp, filesize( $inputfile ) ); 
  fclose( $ifp ); 
  /* encode & write data (binary) */ 
  $ifp = fopen( $outputfile, "wb" ); 
  fwrite( $ifp, base64_decode( $imageData ) ); 
  fclose( $ifp ); 
  /* return output filename */ 
  return( $outputfile ); 
}

in HTML if you want to just display in web / using HTML

<img src="data:image/png;base64,Your_Base_64_Code_Here">

You can use For use as CSS background:

url('data:image/png;base64,Your_Base_64_Code_Here')
Gaeta answered 1/12, 2018 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.