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:

--Apple-Mail=_03A23B16-D748-4835-8340-CB928700DB3D
Content-Disposition: inline; filename="spe1618_v4_doors_20161107.pdf"
Content-Type: application/pdf; x-unix-mode=0644; 
 name="spe1618_v4_doors_20161107.pdf"
Content-Transfer-Encoding: base64

JVBERi0xLjQNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFu
Zyhlbi1HQikgL1N0cnVjdFRyZWVSb290IDMzOSAwIFIvTWFya0luZm88PC9NYXJrZWQgdHJ1ZT4+
[...]
Pj4NCnN0YXJ0eHJlZg0KMjU5NTg4NQ0KJSVFT0Y=

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

My Tweets