How does email coding work? (Django / Python)

I am sending an email with an EmailMessage object in a Gmail field.
The subject of the letter looks something like this: u "You have a letter from Daėrius ęėįęėįęįėęįę --- reply3_433441"

When I receive an email while looking at the message information, I see that the Subject line looks like this:

Subject: =? utf-8? b? WW91IGdvdCBhIGxldHRlciBmcm9tIERhxJdyaXVzIMSZxJfEr8SZxJfEr8SZ? = =? UTF-8? B XK / El 8SZxK / EmS0tLXJlcGx5M180MzM0NDE =? =

How to decode this theme?

I have properly decrypted the email body (tex / plain) as follows:

for part in msg.walk():
  if part.get_content_type() == 'text/plain':
    msg_encoding = part.get_content_charset()
    msg_text = part.get_payload().decode('quoted-printable')
msg_text = smart_unicode(msg_text, encoding=msg_encoding, strings_only=False, errors='strict') 
+3
source share
3 answers

. RFC 2047 . "=?" charset "?" encoding "?" encoded-text "?=". , UTF-8 base-64.

email.header.decode_header str.decode Unicode:

>>> import email.header
>>> x = email.header.decode_header('=?utf-8?b?WW91IGdvdCBhIGxldHRlciBmcm9tIERhxJdyaXVzIMSZxJfEr8SZxJfEr8SZ?=')
>>> x
[('You got a letter from Da\xc4\x97rius \xc4\x99\xc4\x97\xc4\xaf\xc4\x99\xc4\x97\xc4\xaf\xc4\x99', 'utf-8')]
>>> x[0][0].decode(x[0][1])
u'You got a letter from Da\u0117rius \u0119\u0117\u012f\u0119\u0117\u012f\u0119'
+4
+3

utf8 theme line, but you read it as ASCII, you consider it the safest as utf8, since ASCII is only effective as a subset of utf8.

0
source

All Articles