Home » Uncategorized » Rescue wrongly encoded e-mail attachment

Rescue wrongly encoded e-mail attachment

Due to the long heritage of its underlying transport protocol SMTP, e-mail messages are still base64 encoded. This ensures that binary attachments (such as PDFs or zip files) are converted into the 7-bit ASCII character set [a-zA-Z0-9+/] that the SMTP protocol knows how to handle. The character `=` is used to terminate the so encoded string. The raw content of the attachment may look like this:

Content-Disposition: inline; filename="spe1618_v4_doors_20161107.pdf"
Content-Type: application/pdf; x-unix-mode=0644; 
Content-Transfer-Encoding: base64


Usually your e-mail client handles the decoding for you and all you see is a little icon, e.g. of a PDF document, which you can double-click to open the attachment. Sometimes it happens, however, that your e-mail client fails to properly decode this string and then you cannot open the attachment. In this case, simply save the long string “JVBE…FT0Y=” to a text file, say file.txt and then decode this using this shell command:

cat text.txt | base64 --decode > text.pdf