Python email encoding and decryption issues

Basically, I want to read all new letters from the Inbox and put them in the database. The reason I use python is because it has imaplib, but I don't know anything about it.

I currently have something like this:

def primitive_get_text_blocks(email_message_instance):
    maintype = email_message_instance.get_content_maintype()
    if maintype == 'multipart':
        return_parts = ""
        for part in email_message_instance.get_payload():
            if part.get_content_maintype() == 'text':
                return_parts+= " "+ part.get_payload()
        return return_parts
    elif maintype == 'text':
        return email_message_instance.get_payload()
    return ""

fromField=con.escape(email_message["From"])
contentField=con.escape(primitive_get_text_blocks(email_message))

A primitive get_text_blocksis a copy inserted from somewhere. As a result, I get the records in the database as follows:

<META http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8">

From what I understand, this is due to the fact that it is encoded in utf-7. So I changed to get_payload(decode=True), but that gives me byte arrays. If I add another decode('utf-8'), it sometimes crashes with errors, for example

'codec error cannot be decoded ...'.

, , unicode .

convert(charset from, charset to)? ( ?). IMAP Fetch Encoding decode_header, .

-

, - , , , ? , , -, , - beeing, utf-8 utf-7. ?

google , , , , , ( )

+4
1

, . , , unicode - , "str" .

, , 'decode = True' 'getPayload' str (..., 'utf-8').

+1

Source: https://habr.com/ru/post/1542172/


All Articles