def getLastEmails(conf, inputs, outputs): conn = IMAP(conf["mm_mail"]["server"]) conn.login(conf["mm_mail"]["user"], conf["mm_mail"]["password"]) status, data = conn.select("INBOX") results = [] for num in range(1, int(data[0]) + 1): typ, data = conn.fetch(num, '(RFC822)') raw_email = data[0][1] raw_email_string = raw_email.decode('utf-8') email_message = email.message_from_string(raw_email_string) email_from = email_message['from'] for part in email_message.walk(): if part.get_content_maintype() == 'multipart': continue if part.get('Content-Disposition') is None: continue fileName = part.get_filename() if bool(fileName): filePath = os.path.join(conf["main"]["tmpPath"], fileName) if not os.path.isfile(filePath): fp = open(filePath, 'wb') fp.write(part.get_payload(decode=True)) fp.close() results += [{"mail": email_from, "path": filePath}] conn.store(num, '+FLAGS', '\\Deleted') conn.expunge() conn.close() conn.logout() outputs["Result"]["value"] = json.dumps(results) return zoo.SERVICE_SUCCEEDED
def change_swa_settings(conn: imaplib.IMAP4, swa_options: dict): """Changes swa preference email or create a new one if message with preference does not exists. :param swa_options: :param conn: imaplib.IMAP4 :return: """ status, data = conn.select('#Scalix/Oddpost') if status != 'OK': raise Exception('Could not select folder #Scalix/Oddpost. ' 'Imap response: {}'.format(data)) orphan_uids = set() if not int(data[0]): # there are no messages in mailbox fetching will fail so we will # skip it email = create_preference_message() else: email, orphan_uids = find_swa_preference_email(conn) if not email: email = create_preference_message() # lets apply changes to the preferences for key, value in swa_options.items(): find_option_and_change(email.preferences, key, value) # if we have UID for previous email jus tadd it to delete if email.uid: orphan_uids.add(email.uid) if orphan_uids: uids_del = b','.join([str(uid).encode() for uid in orphan_uids]) status, data = conn.uid('STORE', uids_del.decode(), '+FLAGS', '\\Deleted') if status != 'OK': warnings.warn( 'Could not delete duplicate preference emails {}' '. Error {}.'.format(orphan_uids, data), RuntimeWarning ) status, data = conn.append('#Scalix/Oddpost', '\\Seen', imaplib.Time2Internaldate(time.time()), email.as_bytes()) if status != 'OK': if email.uid: conn.uid('STORE', email.uid, '-FLAGS', '\\Deleted') raise Exception('Could not save email. Error message {}'.format(data)) conn.expunge()
def run(self, imap_obj: imaplib.IMAP4): typ, data = imap_obj.uid('STORE', self.__uids, '+FLAGS', '\\Deleted') self.check_response(typ, data) typ, data = imap_obj.expunge() self.check_response(typ, data) return True