aboutsummaryrefslogtreecommitdiff
path: root/tests/send-to-imap.py
blob: ccbebdaeb5d330464b86f7869f90805b38b5cc8b (plain) (blame)
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
from imaplib import IMAP4_SSL
from os import listdir
from os.path import isfile, join
import sys

def rebuild_body_res(b):
    bb = b''
    for e in b:
        if type(e) is tuple:
            bb += b'\r\n'.join([p for p in e])
        else:
            bb += e

    f = bb[bb.find(b'('):]
    return f

path = sys.argv[1]
onlyfiles = [join(path, f) for f in listdir(path) if isfile(join(path, f)) and len(f) > 4 and f[-4:] == ".eml"]

# docker run --rm -it -p 993:993 -p 143:143 dovecot/dovecot
test_mb = "kzUXL7HyS5OjLcU8"
with IMAP4_SSL(host="localhost") as M:
    M.login("test", "pass")
    M.delete(test_mb)
    M.create(test_mb)
    M.select(test_mb)
    for (idx, f) in enumerate(onlyfiles):
        f_noext = f[:-4]
        with open(f, 'r+b') as mail:
            M.append(test_mb, [], None, mail.read())
            seq = (f"{idx+1}:{idx+1}").encode()
            (r, b) = M.fetch(seq, "(BODY)")
            assert r == 'OK'
            

            with open(f_noext + ".body", 'w+b') as w:
                w.write(rebuild_body_res(b))

            (r, b) = M.fetch(seq, "(BODYSTRUCTURE)")
            assert r == 'OK'
            with open(f_noext + ".bodystructure", 'w+b') as w:
                w.write(rebuild_body_res(b))

    M.close()
    M.logout()

# old :
    #(res, v) = M.select(test_mb)
    #assert res == 'OK'
    #exists = v[0]
    #print(M.fetch(b"1:"+exists, ))
    #print(M.fetch(b"1:"+exists, "(BODYSTRUCTURE)"))