def test_printableSingletons(self): """ The IMAP4 modified UTF-7 implementation encodes all printable characters which are in ASCII using the corresponding ASCII byte. """ # All printables represent themselves for o in range(0x20, 0x26) + range(0x27, 0x7f): self.failUnlessEqual(chr(o), encode(chr(o))) self.failUnlessEqual(chr(o), decode(chr(o))) self.failUnlessEqual(encode('&'), '&-') self.failUnlessEqual(decode('&-'), '&')
def test_printable_singletons(self): """ The IMAP4 modified UTF-7 implementation encodes all printable characters which are in ASCII using the corresponding ASCII byte. """ # All printables represent themselves for o in list(range(0x20, 0x26)) + list(range(0x27, 0x7f)): self.assertEqual(int2byte(o), encode(unichr(o))) self.assertEqual(unichr(o), decode(int2byte(o))) self.assertEqual(encode('&'), b'&-') self.assertEqual(encode('&'), b'&-') self.assertEqual(decode(b'&-'), '&')
def list_mailboxes(self, conn): rv, mailboxes = conn.list() print('LISTA DE MAIL BOXES:') for i in mailboxes: decoded = imap_utf7.decode(i) print('') print(decoded.split("\"/\"")[1])
def __init__(self, name, flags): self.name = name self.name_dec = imap_utf7.decode(name.encode()) self.flags = flags self.max_uid = 0 self.last_loaded_uid = None self.loaded = False self.messages = []
def list_folders(imap): lst = imap.list() for folder in lst[1]: # b'(\\Marked \\HasNoChildren) "|" "&BB0EEAQRBB4EIA-"' decoded_folder = imap_utf7.decode(folder) # (\Marked \HasNoChildren) "|" "НАБОР" folder_name = decoded_folder.split(' "|" ') print(folder_name, folder)
def parse_list_response(line, useUTF7=False): if useUTF7: decodedstring = imap_utf7.decode(line) else: decodedstring = line.decode('latin-1') match = list_response_pattern.match(decodedstring) flags, delimiter, mailbox_name = match.groups() mailbox_name = mailbox_name.strip('"') return (flags, delimiter, mailbox_name)
def list_mailboxes(self, conn): rv, mailboxes = conn.list() print('LISTA DE MAIL BOXES:') for i in mailboxes: decoded = imap_utf7.decode(i) print('') if isinstance(decoded, str): sp = decoded.split("\"/\"")[-1] print(sp)
def select_email_folder(mail, storage): """Get email folders and select the one you want""" result, folders = mail.list() enum_folders = {} if result == 'OK': for num, f in enumerate(folders, 1): l = f.decode().split(' "/" ') enum_folders[num] = l[1] print(str(num) + '. ' + imap_utf7.decode(f)) storage['folder'] = enum_folders.get( int(input('Select number of folder:')), 'INBOX')
def connect_to_server(self): self.statusbar.showMessage("Parsing account info...") with open('account.json') as f: account = json.load(f) try: passwd = subprocess.check_output(account["passwdcmd"], shell=True) passwd = passwd.strip().decode() except Exception as e: self.statusbar.showMessage("Decoding Password failed.") import traceback traceback.print_exc() return # Connect to the server self.statusbar.showMessage("Connecting to server...") try: self.imap_connection = imaplib.IMAP4_SSL(account["server"], account["port"]) except Exception as e: self.statusbar.showMessage("Connection to server failed.") import traceback traceback.print_exc() return # Login to the server self.statusbar.showMessage("Authenticating...") try: typ, data = self.imap_connection.login(account["user"], passwd) if typ != 'OK': self.statusbar.showMessage("Authentication failed.") return except Exception as e: self.statusbar.showMessage("Authentication failed.") import traceback traceback.print_exc() return self.statusbar.showMessage("Authentication succeeded.") mailbox_list = self.get_mailbox_list() # Make list of mailboxes and register to QListWidget for item in mailbox_list: decoded_name = imap_utf7.decode(item["name"].encode()) item = QListWidgetItem(decoded_name) self.mailboxes.addItem(item) return
def mailboxes_list(self, imap): mailboxes = dict() translator = googletrans.Translator() for box in imap.list()[1]: box = box.split(b' "/" ')[1] mailbox = imap_utf7.decode(box)[1:-1] box = box.decode('utf-8') if mailbox == '[Gmail]': continue elif mailbox == 'INBOX': mailboxes['inbox'] = ('Входящие', box) else: mailbox = mailbox.replace('[Gmail]/', '') ind = translator.translate(mailbox, 'en').text.lower().replace(' ', '').replace('/', '-') mailboxes[ind] = (mailbox, box) return mailboxes
def test_decode(self): for (input, output) in self.tests: decoded = decode(output) self.assertEquals(input, decoded) self.assert_(isinstance(decoded, unicode))
def main(): global folder_name global folder_name_call unread_messages = 0 unread_messages_call = 0 message_number = [] message_number_call = [] date_list = [] shipment_list = [] address_list = [] datas_list = [] price_list = [] date_list_call = [] datas_list_call = [] # CONNECT ----------------------------------------------- password, server, login = get_pass() print("Connecting to {}...".format(server)) imap = imaplib.IMAP4_SSL(server) print("Connected! Logging in as {}...".format(login)) imap.login(login, password) print("Logged in! Listing folders...") # -------------------------------------------------------- folders = [] for item in imap.list()[1]: folder_raw = imap_utf7.decode(item) try: folder = folder_raw.split('"')[3] except IndexError: folder = folder_raw.split('"')[2] if folder == 'INBOX|юмакс заказы': folder_number = imap.list()[1].index(item) folder_name = imap.list()[1][folder_number].decode('utf-8').split( '"')[-2] if folder == 'юммаксзвонки': # print('Haaaaalllooooo!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') # print(item, type(item)) # print(str(item).split('"')) folder_number_call = imap.list()[1].index(item) folder_name_call = imap.list()[1][folder_number_call].decode( 'utf-8').split('"')[-2] folders.append(folder) # print(folder) # ENTER THE FOLDER ------------------------------------------------ date_list_call, datas_list_call, unread_messages_call, message_number_call = get_calls( imap, folder_name_call) status, select_data = imap.select(folder_name) letters_list = imap.search(None, 'ALL') # ---------------------------------------------------------------- # CYCLE FOR LETTERS ---------------------------------------------- # for i in ['26']: # for i in [str(x) for x in range(1, 5)]: for i in letters_list[1][0].split(): cur_letter = imap.fetch(i, '(RFC822)') # print('current letter: {}'.format(cur_letter)) status, data = imap.fetch( i, '(RFC822)') # .encoding('utf-8') '(RFC822)' message = email.message_from_bytes(data[0][1], _class=email.message.EmailMessage) # DATE --------------------------------------------------------- date = list(email.utils.parsedate_tz(message['Date']))[:3] date_out = '' for _ in date: date_out += str(_) + '.' date_final = date_out[0:-1] # -------------------------------------------------------------- # soup = BeautifulSoup(message.get_payload(decode=True), 'html5lib') soup = BeautifulSoup(message.get_payload(decode=True), 'lxml') try: # PRICE ----------------------------------------------------- try: price = 0 if soup.strong: price = soup.strong.string.split('=')[0] price = [x for x in price if x.isdigit() or x == '.'] price_str = ''.join(price) price = float(price_str) # ------------------------------------------------------------ # DATAS -------------------------- datas = soup.findAll('p')[0].findAll('b') # print(datas) datas_out = list(map(lambda x: x.string, datas)) # print(datas_out) datas_final = '' for _ in datas_out: if _: datas_final += _ + ' ' # '\n' + ' ' datas_final = datas_final[0:] # -------------------------------------------------------------- # ADDRESS ------------------------------------------------------ address = str(soup.findAll('p')[2].contents[0]) # print(address) if ',' in list(address): address = str(address).replace(',', '') # -------------------------------------------------------------- shipment = '' if len(soup.findAll('p')) > 4: shipment = str(soup.findAll('p')[4].contents[0]) shipment = shipment.replace('\\', '') # print(shipment) # DATAFRAME --------------------------------------------------------- date_list.append(date_final) shipment_list.append(shipment) price_list.append(price) address_list.append(address) datas_list.append(datas_final) # ----------------------------------------------------------------------- # print(date_final, shipment, price, address, datas_final) except AttributeError as e: print('Ошибка:\n', traceback.format_exc()) print('AttributeError') unread_messages += 1 message_number.append(i) except IndexError as err: print('Ошибка:\n', traceback.format_exc()) print('IndexError') unread_messages += 1 message_number.append(i) print('unread messages : {}'.format(unread_messages)) print('unread messages call: {}'.format(unread_messages_call)) print('unread messages numbers: {}'.format(message_number)) print('unread messages numbers call: {}'.format(message_number_call)) # print(datas_list) # print(datas_list_call) writer = ExcelWriter('Заказы и звонки.xlsx') df = DataFrame({ 'Date': date_list, 'Shipment': shipment_list, 'Price': price_list, 'Address': address_list, 'Personal data': datas_list }) df_call = DataFrame({ 'Date': date_list_call, 'Personal data': datas_list_call }) df.to_excel(writer, 'Заказы', index=False) df_call.to_excel(writer, 'Звонки', index=False) writer.save() imap.logout()
def test_decode(self): for (input, output) in self.tests: decoded = decode(output) self.assertIsInstance(decoded, text_type) self.assertEqual(input, decoded)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys from imapclient import imap_utf7 decoded = imap_utf7.decode(sys.argv[1]) print(decoded)
def utf7_to_unicode(text): """Convert string in utf-7 to unicode""" return imap_utf7.decode(text)
# IMAP folder path encoding (IMAP UTF-7) for Python from imapclient import imap_utf7 decoded = imap_utf7.decode('&BdAF6QXkBdQ-')
#coding:utf-8 from imapclient import imap_utf7 s = 'AGI-d+AGMAdA-f+AHsAQABiAGw-ue+AEA-d+AG8-n+AEA-edu+AEAAfQ-' s = s.split('+') s = ['&' + i for i in s] decoded = '' for i in (imap_utf7.decode(i) for i in s): decoded += ''.join(i) print decoded
def save(self, toJson=False, useFileName=False, raw=False): '''Saves the message body and attachments found in the message. Setting toJson to true will output the message body as JSON-formatted text. The body and attachments are stored in a folder. Setting useFileName to true will mean that the filename is used as the name of the folder; otherwise, the message's date and subject are used as the folder name.''' if useFileName: # strip out the extension dirName = filename.split('/').pop().split('.')[0] else: # Create a directory based on the date and subject of the message d = self.parsedDate if d is not None: dirName = '{0:02d}-{1:02d}-{2:02d}_{3:02d}{4:02d}'.format(*d) else: dirName = "UnknownDate" if self.subject is None: subject = "[No subject]" else: subject = "".join(i for i in self.subject if i not in r'\/:*?"<>|') dirName = dirName + " " + subject def addNumToDir(dirName): # Attempt to create the directory with a '(n)' appended for i in range(2, 100): try: newDirName = dirName + " (" + str(i) + ")" os.makedirs(newDirName) return newDirName except Exception: pass return None try: os.makedirs(dirName) except Exception: newDirName = addNumToDir(dirName) if newDirName is not None: dirName = newDirName else: raise Exception( "Failed to create directory '%s'. Does it already exist?" % dirName ) oldDir = os.getcwd() try: os.chdir(dirName) # Save the message body fext = 'json' if toJson else 'text' f = open("message." + fext, "w") # From, to , cc, subject, date def xstr(s): return '' if s is None else str(s) attachmentNames = [] # Save the attachments for attachment in self.attachments: attachmentNames.append(attachment.save()) if toJson: import json from imapclient.imapclient import decode_utf7 emailObj = {'from': xstr(self.sender), 'to': xstr(self.to), 'cc': xstr(self.cc), 'subject': xstr(self.subject), 'date': xstr(self.date), 'attachments': attachmentNames, 'body': decode_utf7(self.body)} f.write(json.dumps(emailObj, ensure_ascii=True)) else: f.write("From: " + xstr(self.sender) + "\n") f.write("To: " + xstr(self.to) + "\n") f.write("CC: " + xstr(self.cc) + "\n") f.write("Subject: " + xstr(self.subject) + "\n") f.write("Date: " + xstr(self.date) + "\n") f.write("-----------------\n\n") f.write(imap_utf7.decode(self.body)) f.close() except Exception: self.saveRaw() raise finally: # Return to previous directory os.chdir(oldDir)
for users in cur.execute('select mail_user_seq, mail_uid from mail_user'): print(users[1]) except: print("NOT CONNECT") # print(str(users)) # # print(e.pgerror) # print("I am unable to connect to the database") conn.close() db = sqlite3.connect("d:/_mcache.db") folder = db.cursor() disk_total = 0 for row in folder.execute('select * from mail_folder order by folder_name, disk_usage desc'): # print(row[1]) box_name = imap_utf7.decode(str(row[1])) disk_usage = round(row[8]/1024/1024,3) if box_name not in ("Spam", "Trash", "Drafts","Quotaviolate"): disk_total = round( disk_total + disk_usage, 3) else : box_name = "------" + box_name + "-------" # print(box_name + "\t" + str(disk_usage)+"MB") # schema = db.execute('.scheme') # print(schema) db.close() # print(imap_utf7.decode("Inbox.&rLDHrMw4yHA-")) print("DISK_USAGE : " + str(disk_total) + "MB")