def login_button_clicked(self): host = self.Host_line.text() port = self.Port_line.text() login = self.Login_line.text() password = self.Password_line.text() try: imap = Imap(host, port, login, password) imap.login() self.set_disabled(True) main_window = MainWindow(imap, self) self.Main_window = main_window except Exception as e: self.message_box = QtWidgets.QMessageBox() self.message_box.setText(str(e)) self.message_box.show()
class Test(unittest.TestCase): host = 'imap.gmail.com' port = '993' login = '******' password = '******' imap = Imap(host, port, login, password) imap.login() def test_show_list_of_folders(self): result = self.imap.show_list_of_folders().decode() self.assertTrue('INBOX' in result) self.assertTrue('Important' in result) self.assertTrue('Sent' in result) self.assertFalse('BadFolderName' in result) def test_select_folder(self): result = self.imap.select_folder('INBOX').decode() self.assertTrue('OK' in result) self.assertTrue('INBOX selected' in result) def test_get_count_of_letters(self): self.imap.select_folder('INBOX') self.assertEqual(int(self.imap.get_count_of_letters()), 3) def test_status_of_folder(self): self.imap.select_folder('INBOX').decode() result = self.imap.status_of_folder('INBOX', 'MESSAGES').decode() self.assertTrue('STATUS "INBOX" (MESSAGES 3)' in result) def test_close_folder(self): self.imap.select_folder('INBOX').decode() self.imap.close_folder() result = self.imap.get_answer().decode() self.assertTrue('Returned to authenticated state' in result) def test_search_msg(self): self.imap.select_folder('INBOX').decode() result = self.imap.search_msg('all').decode() self.assertTrue('SEARCH completed' in result) def test_choose_msg(self): self.imap.select_folder('INBOX').decode() result = self.imap.choose_msg(1, 'body[]').decode() self.assertTrue('Delivered-To: [email protected]' in result) self.assertTrue('Sun, 10 Mar 2019 07:09:50 -0700 (PDT)' in result) self.assertTrue('Content-Type: multipart/alternative' in result)
def _check_new(self): if not self.info.is_connected: self.imap = Imap(self.config) try: self.imap.login() except Exception: logging.exception("Login error") self.info = Info() return self.info.is_connected = True try: envelopes = self.imap.get_envelopes("Unseen", 10) except Exception: logging.exception("Can't check inbox") self.info = Info() return self.info.emails = envelopes
class Application: def __init__(self, update_ui_method, queue, config): self.update_ui_method = update_ui_method self.queue = queue self.config = config self.active = True self.info = Info() self.last_info = Info() def check_queue(self): while self.active: method, arg = self.queue.get() method(arg) self.queue.task_done() def schedule_check_new(self): while True: self.queue.put((self.check_new, None)) self.queue.join() time.sleep(self.config.get_check_interval()) def close(self, arg): self.active = False if self.info.is_connected: self.imap.logout() self.info.is_connected = False def check_new(self, arg): if self.queue.qsize() == 0: self._check_new() self.update_ui() else: logging.info("Queue is not empty, skip checking inbox") def _check_new(self): if not self.info.is_connected: self.imap = Imap(self.config) try: self.imap.login() except Exception: logging.exception("Login error") self.info = Info() return self.info.is_connected = True try: envelopes = self.imap.get_envelopes("Unseen", 10) except Exception: logging.exception("Can't check inbox") self.info = Info() return self.info.emails = envelopes def archive(self, email): self.last_info = None if not self.info.is_connected: logging.info("Cancel archiving email {}".format(email)) return try: self.imap.mark_as_seen(email.number, True) except Exception: logging.exception("Can't archive email") self.info = Info() self.update_ui(force=True) def delete(self, email): self.last_info = None if not self.info.is_connected: logging.info("Cancel deleting email {}".format(email)) return try: self.imap.delete(email.number) except Exception: logging.exception("Can't delete email") self.info = Info() self.update_ui(force=True) def update_ui(self, force=False): if force or (self.info != self.last_info and self.queue.qsize() == 0): data = pickle.dumps(self.info) self.last_info = pickle.loads(data) self.update_ui_method(data)
from Imap import Imap import sys print(sys.platform) imap = Imap('imap.gmail.com', 993, '*****@*****.**', 'Fcbljhf23Akbynjdyf2323') print(imap.login()) # print(imap.get_count_of_letters())