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?
How to decode an email attachment received as a Base64 text
Asked Answered
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
Copy this long string and paste it in the Base64 decoder found here.
Download the output and rename it by adding the appropriate extension to it. For example
testfile.pdf
orfilename.docx
.
There you go. You just recreated your lost attachment using Base64 decoding.
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 coding –
Palliative
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')
© 2022 - 2024 — McMap. All rights reserved.
JVBERi0xLjUNCiW1tbW
) to your target format. You can find some examples here. – Malisamalison