diff -r 72b1715e18c1 -r 732e7d4515c0 Lib/email/base64mime.py --- a/Lib/email/base64mime.py Tue Apr 15 23:44:10 2014 -0400 +++ b/Lib/email/base64mime.py Thu Apr 17 10:37:06 2014 -0400 @@ -35,7 +35,7 @@ 'header_encode', ] - +from base64 import urlsafe_b64decode from binascii import b2a_base64, a2b_base64 from email.utils import fix_eols @@ -172,7 +172,9 @@ if not s: return s - dec = a2b_base64(s) + # Doing urlsafe b64decode is a little non-obvious, but a few email products + # seem to generate it. See #12489 + dec = urlsafe_b64decode(s) if convert_eols: return dec.replace(CRLF, convert_eols) return dec diff -r 72b1715e18c1 -r 732e7d4515c0 Lib/email/header.py --- a/Lib/email/header.py Tue Apr 15 23:44:10 2014 -0400 +++ b/Lib/email/header.py Thu Apr 17 10:37:06 2014 -0400 @@ -101,7 +101,7 @@ encoded += '==='[:4 - paderr] try: dec = email.base64mime.decode(encoded) - except binascii.Error: + except TypeError: # Turn this into a higher level exception. BAW: Right # now we throw the lower level exception away but # when/if we get exception chaining, we'll preserve it. diff -r 72b1715e18c1 -r 732e7d4515c0 Lib/email/test/test_email.py --- a/Lib/email/test/test_email.py Tue Apr 15 23:44:10 2014 -0400 +++ b/Lib/email/test/test_email.py Thu Apr 17 10:37:06 2014 -0400 @@ -2773,6 +2773,9 @@ eq(base64MIME.decode('aGVsbG8='), 'hello') eq(base64MIME.decode('aGVsbG8=', 'X'), 'hello') eq(base64MIME.decode('aGVsbG8NCndvcmxk\n', 'X'), 'helloXworld') + eq(base64MIME.decode( + 'QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw=='), + 'Anmeldung Netzanschluss S\xfcdring3p.jpg') def test_encode(self): eq = self.assertEqual