File: dev_notes.txt

package info (click to toggle)
imap-tools 1.10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,624 kB
  • sloc: python: 4,709; makefile: 5
file content (139 lines) | stat: -rw-r--r-- 3,890 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Attachments get_payload
=======================

decode  is_multipart  result
------  ------------  ------------------------------
True    True          +None
True    False         +_payload decoded (bytes)
False   True          +_payload (a list)
False   False         _payload (*)

* - some other encoding is used, or the header is missing,
    or if the payload has bogus data (i.e. bogus base64 or uuencoded data),
    the payload is returned as-is.


many uids command
=================
error example for 22000 uids in yandex:
    imap_tools.errors.MailboxCopyError: Response status "OK" expected, but "NO" received.
    Data: [b'command or literal size is too large']


email lib
=========
Доступен email.message.EmailMessage вместо Message, есть смысл туда заглядывать по логике разбора

self.obj = email.message_from_bytes(raw_message_data, _class=EmailMessage) можно так, пока не вижу смысла
пробовал iter_attachments для MailMessage.attachments - не работает с инлайн вложениями

утилиты https://docs.python.org/release/3.8.1/library/email.utils.html


TLS
===
все тестовые ящики: TLS not supported by server


icons
=====
📨 📬 📪 📭 📫 ✉ 📧 🖂 🖃 🖅 📩


fetch
=====
FULL - Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY)


code
====
def eml_to_python_structs(eml_path: str):
    from imap_tools import MailMessage
    with open(eml_path, 'rb') as f:
        bytes_data = f.read()
    msg = MailMessage.from_bytes(bytes_data)
    att_key = '__@@@att@@@__'
    result = """
import datetime
from imap_tools import EmailAddress

DATA = dict(
    subject={},
    from_={},
    to={},
    cc={},
    bcc={},
    reply_to={},
    date={},
    date_str={},
    text={},
    html={},
    headers={},
    attachments={}
    from_values={},
    to_values={},
    cc_values={},
    bcc_values={},
    reply_to_values={},
)
    """.format(
        repr(msg.subject),
        repr(msg.from_),
        repr(msg.to),
        repr(msg.cc),
        repr(msg.bcc),
        repr(msg.reply_to),
        repr(msg.date),
        repr(msg.date_str),
        repr(msg.text),
        repr(msg.html),  # .strip('\'')
        msg.headers,
        att_key,
        repr(msg.from_values),
        repr(msg.to_values),
        repr(msg.cc_values),
        repr(msg.bcc_values),
        repr(msg.reply_to_values),
    )
    att_res = ''
    for att in msg.attachments:
        att_res += '''
        dict(
            filename={},
            content_id={},
            content_disposition={},
            content_type={},
            payload={},
        ),
        '''.format(
            repr(att.filename),
            repr(att.content_id),
            repr(att.content_disposition),
            repr(att.content_type),
            att.payload,
        )
    att_res = '[{}],'.format(att_res)
    result = result.replace(att_key, att_res)
    return result.strip()


def build_python_structs_from_emls():
    import os
    PATH = 'C:/kvk/develop/Python/imap_tools/tests/messages'
    path_set = []
    for root, subdirs, files in os.walk(PATH):
        for file_name in files:
            full_path = '{}/{}'.format(root, file_name).replace('\\', '/')
            if full_path.lower().endswith('.eml'):
                path_set.append(full_path)

    for full_path in path_set:
        new_full_path = (full_path[:-4] + '.py').replace(PATH, f'{PATH}_data')
        print(full_path)
        print(new_full_path)
        print()
        # if any(i in full_path for i in ['forwarded_message.eml', 'attachment_7bit.eml', 'text_with_content_id.eml']):
        #     continue
        eml_python_structs = eml_to_python_structs(full_path)
        with open(new_full_path, 'wb') as f:
            f.write(eml_python_structs.encode())