Skip to content

ImapClient

email_profile.clients.imap.client.ImapClient

Owns the IMAP socket and the discovered mailbox map.

Source code in email_profile/clients/imap/client.py
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
class ImapClient:
    """Owns the IMAP socket and the discovered mailbox map."""

    def __init__(
        self,
        *,
        server: str,
        user: str,
        password: str,
        port: int = 993,
        ssl: bool = True,
    ) -> None:
        self.server = server
        self.user = user
        self.password = password
        self.port = port
        self.ssl = ssl
        self.client: Optional[imaplib.IMAP4_SSL] = None
        self.mailboxes: dict[str, MailBox] = {}

    @property
    def is_connected(self) -> bool:
        return self.client is not None

    def connect(self) -> ImapClient:
        try:
            client = imaplib.IMAP4_SSL(self.server)
            client.login(user=self.user, password=self.password)
        except Exception as error:
            raise ConnectionFailure() from error

        self.client = client
        self.mailboxes = {
            mb.name: mb
            for mb in (
                MailBox.from_imap_detail(client=client, detail=detail)
                for detail in Status.state(client.list())
            )
        }
        return self

    def close(self) -> None:
        if self.client is not None:
            try:
                self.client.logout()
            finally:
                self.client = None
                self.mailboxes = {}

    def noop(self) -> None:
        self.require()
        self.client.noop()

    def require(self) -> None:
        if self.client is None:
            raise NotConnected()

client = None instance-attribute

is_connected property

mailboxes = {} instance-attribute

password = password instance-attribute

port = port instance-attribute

server = server instance-attribute

ssl = ssl instance-attribute

user = user instance-attribute

__init__(*, server, user, password, port=993, ssl=True)

Source code in email_profile/clients/imap/client.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def __init__(
    self,
    *,
    server: str,
    user: str,
    password: str,
    port: int = 993,
    ssl: bool = True,
) -> None:
    self.server = server
    self.user = user
    self.password = password
    self.port = port
    self.ssl = ssl
    self.client: Optional[imaplib.IMAP4_SSL] = None
    self.mailboxes: dict[str, MailBox] = {}

close()

Source code in email_profile/clients/imap/client.py
54
55
56
57
58
59
60
def close(self) -> None:
    if self.client is not None:
        try:
            self.client.logout()
        finally:
            self.client = None
            self.mailboxes = {}

connect()

Source code in email_profile/clients/imap/client.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def connect(self) -> ImapClient:
    try:
        client = imaplib.IMAP4_SSL(self.server)
        client.login(user=self.user, password=self.password)
    except Exception as error:
        raise ConnectionFailure() from error

    self.client = client
    self.mailboxes = {
        mb.name: mb
        for mb in (
            MailBox.from_imap_detail(client=client, detail=detail)
            for detail in Status.state(client.list())
        )
    }
    return self

noop()

Source code in email_profile/clients/imap/client.py
62
63
64
def noop(self) -> None:
    self.require()
    self.client.noop()

require()

Source code in email_profile/clients/imap/client.py
66
67
68
def require(self) -> None:
    if self.client is None:
        raise NotConnected()