-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_.py
79 lines (65 loc) · 2.45 KB
/
fetch_.py
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
# -*- coding: utf-8 -*-
"""
"""
import imaplib
import pprint
from pymaillib.imap.client import ImapClient
from pymaillib.imap.query.builders.search import SearchQueryBuilder
from pymaillib.imap.entity.folder import ImapFolder
from pymaillib.imap.query.builders.store import StoreQueryBuilder
from pymaillib.imap.query.builders.fetch import FetchQueryBuilder
from pymaillib.mailbox import UserMailbox
from pymaillib.settings import Config
imaplib.Debug = 0
ImapClient()
fetch_query = FetchQueryBuilder.fast('1')\
.fetch_envelope()\
.fetch_body_structure()\
.fetch_rfc822_size()\
.fetch_header_item('Subject')\
.fetch_header_item('Message-ID')
# 1 (ENVELOPE INTERNALDATE RFC822.SIZE BODYSTRUCTURE UID FLAGS BODY[HEADER.FIELDS (Subject Message-ID)])
print(fetch_query)
config = Config().from_config_file('./pymail.ini')
mailbox = UserMailbox('sxadmin', '1', config)
with mailbox.imap() as client:
with client as client2:
# {'other_users': [{'separator': b'Other Users/', 'name': b'/'}],
# 'public_folders': [{'separator': b'Public Folders/', 'name': b'/'}],
# 'private': [{'separator': b'', 'name': b'/'}]}
print(client2.namespace())
for folder in client.folders():
# Name:INBOX Path: / Attributes:{b'\\X-Unseen-Msgs': b'0',
# b'\\X-Total-Msgs': b'19161', b'\\X-ModDate': b'20170407112206',
# ....
print(folder)
print(folder.selectable)
# [None]
print(client.recent())
folder = ImapFolder(b'Inbox', b'/', {})
store_query = StoreQueryBuilder(1).remove(r'\SEEN')
client.select_folder(folder)
res = client.store(store_query)
# [{'SEQ': 1, 'FLAGS': [b'\\Recent']}]
print(list(res))
search_query = SearchQueryBuilder()\
.unseen()
res = client.search(search_query)
# [1]
print(list(res))
client.select_folder(folder)
msg = list(client.fetch(fetch_query))[-1]
# {'BODY': {},
# 'BODYSTRUCTURE': [{'disposition': None, 'boundary': ...
# 'ENVELOPE': {'cc': [], 'message_id': b'<1991954822.314JavaMail....
# 'FLAGS': [b'\\Seen', b'\\Recent'],
# 'HEADER': {'Message-ID': '<1991954822.3148>', 'Subject': 'ewdew'},
# 'INTERNALDATE': datetime.datetime(2017, 2, 6, ....,
# 'RFC822.SIZE': 1020,
# 'SEQ': 1,
# 'UID': 1}
pprint.pprint(dict(msg.dump()))
# 1020
pprint.pprint(msg.envelope)
# '<1991954822.31486384534092.JavaMail.root@test.centosx64.com>'
pprint.pprint(msg.header_item('Message-ID'))