S
S
Sasha Filinsky2021-07-28 01:43:19
Python
Sasha Filinsky, 2021-07-28 01:43:19

Letter decoding how to do?

I want to read the last letter that I received for reading using imaplib, But the subject and text of the letter are encrypted, how to decrypt? The text that gives me and the code below


('none none', 'The mail came from here')
Wed, 28 Jul 2021 00:36:09 +0300
=?UTF-8?B?0J3QvtCy0LDRjyDQt9Cw0Y/QstC60LAg4oSWMzY3ODk0MTE=?=
< CAAzzXJgMW=tfFRHAKs+5eBujcphz5bnmail.b5g .com >


The code

import imaplib
import email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('***', '***')
 
mail.list()
mail.select("inbox")

result, data = mail.search(None, "ALL")
 
ids = data[0]
id_list = ids.split()
latest_email_id = id_list[-1]
 
result, data = mail.fetch(latest_email_id, "(RFC822)")
raw_email = data[0][1]
raw_email_string = raw_email.decode()

email_message = email.message_from_string(raw_email_string)

print(email.utils.parseaddr(email_message['From']))
print(email_message['Date'])
print(email_message['Subject'])
print(email_message['Message-Id'])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey〒., 2021-07-28
@UkaUkaa

Subject and text of the letter in base64

=?UTF-8?B? 0J3QvtCy0LDRjyDQt9Cw0Y/QstC60LAg4oSWMzY3ODk0MTE= ?=

It just contains information about encoding:
UTF-8 - character encoding
B - Base64 encoding in which it is transmitted
from email.header import decode_header

subject = email_message['Subject']
bytes, encoding = decode_header(subject)[0]
print(bytes.decode(encoding))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question