def login(self):
     """Connects to the mailbox and logs in."""
     self.IMAP = imaplib.IMAP4_SSL(self.mailconfig.IMAPHost, 993, ssl_context=ssl.create_default_context())
     try:
         self.IMAP.login(self.mailconfig.IMAPUser, self.mailconfig.IMAPPass)
     except:
         log.error("IMAP login failed. Are your login credentials correct?")
         raise
 def checkFolders(self):
     """Check if the configured folders exist"""
     log.info("Checking if configured folders exist")
     response = self.IMAP.select(self.mailconfig.folderInbox)
     if response[0] != "OK":
         log.error("Error accessing Folder '%s': %s" % (self.mailconfig.folderInbox, response[1][0].decode()))
         # TODO: Raise exception
     response = self.IMAP.select(self.mailconfig.folderSuccess)
     if response[0] != "OK":
         log.error("Error accessing Folder '%s': %s" % (self.mailconfig.folderSuccess, response[1][0].decode()))
 def login(self):
     self.SMTP = smtplib.SMTP(self.mailconfig.SMTPHost, self.mailconfig.SMTPPort)
     self.SMTP.ehlo()
     self.SMTP.starttls()
     self.SMTP.ehlo()
     try:
         self.SMTP.login(self.mailconfig.SMTPUser, self.mailconfig.SMTPPass)
     except smtplib.SMTPAuthenticationError:
         log.error("SMTP login failed. Are your login credentials correct?")
         raise
    def fetchMail(self, uid: int) -> ProcessedMail:
        """Fetch mail with uid from inbox

        Arguments:
            uid: uid of email to fetch

        Returns:
            Email with uid, or None if it couldn't be found
        """
        uidbytes: bytes = str(uid).encode()

        response = self.IMAP.uid("fetch", uidbytes, "(RFC822)")
        if response[0] != "OK":
            log.error("Failed to fetch mail: %s" % response[1][0].decode())
            # TODO: throw exception?
            return None

        return ProcessedMail(uid, response[1][0][1], self.mailconfig)
    def get_mail_list(self) -> List[int]:
        """Get list of all mail-UIDs that are in the inbox

        Returns:
            List of UIDs of mails in inbox
        """
        response = self.IMAP.select(self.mailconfig.folderInbox)
        if response[0] != "OK":
            log.error("Error accessing Folder '%s': %s" % (self.mailconfig.folderInbox, response[1][0].decode()))
        emailcount: int = int(response[1][0])
        if not emailcount > 0:
            return []
        log.info("%s email(s) in inbox" % emailcount)

        response = self.IMAP.uid("search", None, "(ALL)")
        if response[0] != "OK":
            log.error("Failed to retrieve mails from inbox: %s" % response[1][0].decode())
            return []
            # TODO: Raise exception?
        indices: List[bytes] = response[1][0].split()
        return [int(x) for x in indices]