def get_today_mails(self): """ Getting inbox messages from the server """ with Imbox( self.server, username=self.userName, password=self.password, ssl=self.ssl, ssl_context=self.ssl_context, starttls=self.starttls, ) as imbox: # Gets all messages now = datetime.datetime.now() new_messagers = imbox.messages( date__on=datetime.date(now.year, now.month, now.day)) unread_msgs = imbox.messages(unread=True) unread_uid = [] for uid, msg in unread_msgs: unread_uid.append(uid.decode()) for uid, message in reversed(new_messagers): mail = self.mail_parsing_from_server(uid, message, unread_uid, "Inbox") # saving the mail in the local FS (file system) if mail: with open(".db/mails/" + str(uid.decode()) + ".json", "w") as file: json.dump(mail, file) return True
def get_unread_from_server(self): """ Getting uuid of unread emails """ mails = [] with Imbox( self.server, username=self.userName, password=self.password, ssl=self.ssl, ssl_context=self.ssl_context, starttls=self.starttls, ) as imbox: all_inbox_messages = imbox.messages(unread=True) unread_msgs = imbox.messages(unread=True) unread_uid = [] for uid, msg in unread_msgs: unread_uid.append(uid.decode()) for uid, message in reversed(all_inbox_messages): mail = self.mail_parsing_from_server(uid, message, unread_uid, "Unread") mails.append(mail) return mails
def get_unread(): mails = [] username = backend_api.get_user_info('mail') passw = backend_api.get_user_info('password') imapserver = backend_api.get_user_info('imapserver') with Imbox( imapserver, username=username, password=passw, ssl=True, ssl_context=None, starttls=False, ) as imbox: logging.info('Account informations correct. Connected.') # Gets all messages after the day x all_inbox_messages = imbox.messages(unread=True) unread_msgs = imbox.messages(unread=True) unread_uid = [] for uid, msg in unread_msgs: unread_uid.append(uid.decode()) logging.debug('Gathered all inbox messages') for uid, message in reversed(all_inbox_messages): mail = mail_parsing(uid, message, unread_uid, 'Unread') mails.append(mail) return mails
def read_email(start_date, end_date,email,pwd): with Imbox('imap.gmail.com', username=email, password=pwd, ssl=True, ssl_context=None, starttls=False) as imbox: parsed_start_date = parse_date(start_date) parsed_end_date = parse_date(end_date) GRAND_TOTAL = 0 subtotals = [] swiggy_emails = imbox.messages(sent_from='*****@*****.**', subject='trip with Uber',date__gt=parsed_start_date, date__lt=parsed_end_date) for uid, message in swiggy_emails: mailbody = str(message.body) soup = BeautifulSoup(mailbody, 'html.parser') parsed_txt = soup.find('span', attrs={'class':'Uber18_text_p2'}).text # Parsed text looks like this - ' ₹99' amount = Decimal(parsed_txt[2:]) subtotals.append(amount) GRAND_TOTAL+= amount print(subtotals) print(GRAND_TOTAL)
def get_mail_send_trello(x): with Imbox('imap.gmail.com', username='******', password='******', ssl=True, ssl_context=None, starttls=False) as imbox: desired_mail = imbox.messages(unread=True,label='',subject=x,raw='Raw Gmail search syntax') for uid, message in desired_mail: desc = message.subject html= (str(message.body)) betterHTML= html2text.html2text(html) start = "html': ['" end = ']}' texto = betterHTML[betterHTML.find(start) + len(start):betterHTML.rfind(end)] imbox.mark_seen(uid) if x == 'Subject that goes to this board/list': list_id = '' elif x == 'Subject that goes to this board/list': list_id = '' r = requests.post("https://api.trello.com/1/cards?key=" + \ API_KEY + "&token=" + OAUTH_TOKEN + \ "&name=" + desc + "&idList=" + \ list_id + "&desc=" + \ texto)
def fetch_emails(query: str = None): """ query: sent_from: Messages sent FROM sent_to: Messages sent TO subject: Messages whose subjects contain a string folder: Messages from a specific folder """ with Imbox( IMAP_HOST, username=IMAP_USER, password=IMAP_PASS, ssl=True, ssl_context=None, starttls=False, ) as box: messages = box.messages() for uid, message in messages[-1:-11:-1]: if not message.body.get("html"): plain = message.body.get("plain")[0] message.body["html"] = f"<pre>{plain}</pre>" else: message.body["html"] = message.body["html"][0] message.date = parse(message.date) yield message
def get_deleted(): mails = [] username = backend_api.get_user_info("mail") passw = backend_api.get_user_info("password") imapserver = backend_api.get_user_info("imapserver") with Imbox( imapserver, username=username, password=passw, ssl=True, ssl_context=None, starttls=False, ) as imbox: logging.info("Account information correct. Connected.") # Gets all messages after the day x all_inbox_messages = imbox.messages(folder="Deleted") unread_msgs = imbox.messages(unread=True) unread_uid = [] for uid, msg in unread_msgs: unread_uid.append(uid.decode()) logging.debug("Gathered all inbox messages") for uid, message in reversed(all_inbox_messages): mail = mail_parsing(uid, message, unread_uid) mails.append(mail) return mails
def mail2pmid(): imbox = Imbox(em_server, username=em_usr, password=em_pw) print '\n\nFetching emails ...', email_gen = (imbox.messages(sent_from=sender)) while True: try: msgs_l.append(email_gen.next()) except StopIteration: break except Exception as e: print(e) sys.exit(1) print("Done.\nSuccessfully retrieved {} messages from NCBI.".format( len(msgs_l))) msgs_str = str(msgs_l) pat = re.compile(r'pubmed\/(?P<pmid>\d+)') pmids = re.findall(pat, msgs_str) pmids = list(set(pmids)) print('Found {} unique PMIDs.\n'.format(len(pmids))) return pmids
def get_mails(year, month, day): # Download all the mails from the DB # backend_api.get_mails(year,month,day) # mails = db_api.get("emails","*","WHERE datetimes = "+datetime.date(year, month, day)) mails = [] username = backend_api.get_user_info("mail") passw = backend_api.get_user_info("password") imapserver = backend_api.get_user_info("imapserver") with Imbox( imapserver, username=username, password=passw, ssl=True, ssl_context=None, starttls=False, ) as imbox: logging.info("Account informations correct. Connected.") # Gets all messages after the day x all_inbox_messages = imbox.messages( date__on=datetime.date(year, month, day)) unread_msgs = imbox.messages(unread=True) unread_uid = [] for uid, msg in unread_msgs: unread_uid.append(uid.decode()) logging.debug("Gathered all inbox messages") for uid, message in reversed(all_inbox_messages): mail = mail_parsing(uid, message, unread_uid) mails.append(mail) return mails
def getmaildata(): print('=====Get Mail Data %s====='%datetime.now()) imbox = Imbox(config('IMAP_ADDRESS'), username=config('USER_NAME'), password=config('PASSWORD'), ssl=True, ssl_context=None) messages_from = imbox.messages(date__gt=when.yesterday(),sent_from=config('SENT_FROM')) #messages_from = imbox.messages(sent_from='*****@*****.**') ldata = [] c,n = 0,0 #解析数据 for uid,message in messages_from: if message.subject == '信用管家消费提醒': html = str(message.body['html']) a = html[2:-2] soup = BeautifulSoup(a,'html.parser') b = soup.find_all('font') for s in b: if s.string is not None: ldata.append(s.string) print('=====Praser Data Success Size:%s ====='%str(len(ldata)/6)) # for m in ldata: # print(m) # print(m,file = open('a123.txt','a')) #写入数据库 while n < len(ldata)/6: try: pid =ldata[1+c]+ldata[2+c].replace(':','') de = DailyExpense.create(cardno=ldata[0+c],exDate=ldata[1+c],exTime=ldata[2+c],currency=ldata[3+c],exRecord=ldata[4+c],fee=float(ldata[5+c].replace(',','')),pid=pid) de.save() except Exception as e: print(e) n,c = n+1,c+6
def get_junk(self): try: with Imbox(self.imap_server, username = self.email_address, password = self.password, ssl=True, ssl_context=None, starttls=False) as imbox: # Get all folders status, folders_with_additional_info = imbox.folders() # Load unread junk messages unread_junk = imbox.messages(unread = True, folder = self.junk_folder) # Get sha256 of each raw message and save raw msg for uid, msg in unread_junk: raw_msg = msg.raw_email.encode("utf-8") sha256 = hashlib.sha256(raw_msg).hexdigest() eml_file = self.save_folder + '/' + sha256 + ".eml" if not os.path.exists(eml_file): with open(eml_file, "wb") as eml: eml.write(raw_msg) # Print message info print('\n' + msg.date) print(msg.sent_from) print(msg.subject) print(sha256) # Mark the message as read imbox.mark_seen(uid) except gaierror as e: print('Error connecting: %s\nCheck your creds or imap server.' % e)
def download_all_mailbox(self): with Imbox( self.server, username=self.userName, password=self.password, ssl=self.ssl, ssl_context=self.ssl_context, starttls=self.starttls, ) as imbox: # Gets all messages after the day x all_inbox_messages = imbox.messages() i = 0 for uid, message in reversed(all_inbox_messages): # Check if the mail exists in the local database percentage = i / all_inbox_messages.__len__() i = i + 1 query = DBApi("mails").get("uuid", "WHERE uuid =" + uid.decode()) if not query: try: self.mail_parsing_from_server(uid, message, "1", "Inbox") except Exception as e: pass
def check_mailbox(self): try: with Imbox("imap.seznam.cz", username=mail_user, password=mail_psw, ssl=True, ssl_context=None, starttls=False) as imbox: messages = imbox.messages(folder='INBOX', unread=True) for uid, message in messages: topic = message.subject payload = json.loads(message.body["plain"][0]) try: self.client.publish(topic, json.dumps(payload)) except: logger.exception( " {} - Exception in message loop.".format( message.subject)) continue imbox.mark_seen(uid) # command execuded else: logger.info("No new message") except: logger.exception( " {} - Exception IMAP connection failed.".format(mail_user))
def download(): google_ids = ImportedEmail.objects.values_list("google_id", flat=True) count = 0 # TODO only get past 24 hours? with Imbox("imap.gmail.com", user, password) as imbox: for google_id, message in imbox.messages(): google_id = google_id.decode() if google_id in google_ids: continue if message.body['plain']: ImportedEmail.objects.create( subject=message.subject, content=message.body['plain'][0], google_id=google_id, attachment_number=0, source_email=user, from_email=message.sent_to[0]['email'], ) for num, content in enumerate(message.body['html']): ImportedEmail.objects.create( subject=message.subject, content=content, google_id=google_id, attachment_number=num + 1, source_email=user, from_email=message.sent_from[0]['email'], ) count += 1
async def post_handler(request, username): global clients password = request.json["password"] client = Imbox("imap.gmail.com", username=username, password=password) client.selected = None clients[username] = client, time.time() return json(None, status=200)
def download_email_attachments(subject, sent_from, download_path): sender = get_default_sender() host = "imap.gmail.com" username = sender["address"] password = sender["password"] download_folder = download_path if not os.path.isdir(download_folder): os.makedirs(download_folder, exist_ok=True) mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False) messages = mail.messages(subject=subject, sent_from=sent_from) # filtro por este asunto for (uid, message) in messages: mail.mark_seen(uid) # opcional, marco el mensaje como leido for idx, attachment in enumerate(message.attachments): try: att_fn = attachment.get('filename') download_path = f"{download_folder}/{att_fn}" print(download_path) with open(download_path, "wb") as fp: fp.write(attachment.get('content').read()) except: print(traceback.print_exc()) mail.logout()
def fetch(self): # with suppress(BaseException): with Imbox('imap.yandex.ru', self.login, self.password, port=993) as ya: # app folder already contains letters sent to the contact_address for uid, msg in ya.messages(unread=True, folder='app'): yield msg ya.mark_seen(uid)
def _login(self, *args, **kwargs): if not isinstance(self._server, Imbox): self._server = Imbox(self._host, username=self._user, password=self._pwd, port=self._port, *args, **kwargs) return self._server
def retrieve_kindle_highlight_files(username, password, dl_directory): file_locations = [] inbox = Imbox('imap.gmail.com', username=username, password=password, ssl=True, ssl_context=None, starttls=False) kindle_messages = inbox.messages(subject='Kindle Export for') for uid, message in kindle_messages: inbox.mark_seen(uid) for index, attachment in enumerate(message.attachments): try: filename = attachment.get('filename') if not os.path.exists(dl_directory): os.makedirs(dl_directory) dl_path = f"{dl_directory}/{filename}" file_locations.append(os.path.abspath(dl_path)) with open(dl_path, 'wb') as fp: fp.write(attachment.get('content').read()) except: pass traceback.print_exc() inbox.logout() return file_locations
def read_email(start_date, end_date, email, pwd): with Imbox('imap.gmail.com', username=email, password=pwd, ssl=True, ssl_context=None, starttls=False) as imbox: parsed_start_date = parse_date(start_date) parsed_end_date = parse_date(end_date) GRAND_TOTAL = 0 subtotals = [] swiggy_emails = imbox.messages(sent_from='*****@*****.**', subject='Your Amazon.in order of', date__gt=parsed_start_date, date__lt=parsed_end_date) for uid, message in swiggy_emails: mailbody = str(message.body) soup = BeautifulSoup(mailbody, 'html.parser') parsed_txt = soup.find_all('td', attrs={'class': 'total'})[1].text parsed_txt = parsed_txt.replace(',', '') amount = Decimal(parsed_txt[4:]) subtotals.append(amount) GRAND_TOTAL += amount print(subtotals) print(GRAND_TOTAL)
def read_email(start_date, end_date,email,pwd): with Imbox('imap.gmail.com', username=email, password=pwd, ssl=True, ssl_context=None, starttls=False) as imbox: parsed_start_date = parse_date(start_date) parsed_end_date = parse_date(end_date) GRAND_TOTAL = 0 subtotals = [] swiggy_emails = imbox.messages(sent_from='*****@*****.**', subject='receipt',date__gt=parsed_start_date, date__lt=parsed_end_date) for uid, message in swiggy_emails: mailbody = str(message.body) soup = BeautifulSoup(mailbody, 'html.parser') parsed_txt = soup.find('tr', attrs={'class':'grand-total'}).text amount = int(parsed_txt.replace('\\r','').replace('\\t','').replace('\\n','').split(':')[1]) subtotals.append(amount) GRAND_TOTAL+= amount print(subtotals) print(GRAND_TOTAL)
def download_coba(): download_folder = "." if not os.path.isdir(download_folder): os.makedirs(download_folder, exist_ok=True) mail = Imbox(host, username=sender_gmail, password=password, ssl=True, ssl_context=None, starttls=False) messages = mail.messages(sent_from='*****@*****.**', date__on = datetime.now()) # filters below for (uid, message) in messages: mail.mark_seen(uid) # optional, mark message as read for idx, attachment in enumerate(message.attachments): try: att_fn = attachment.get('filename') download_path = f"{download_folder}/{att_fn}" print(download_path) with open(download_path, "wb") as fp: fp.write(attachment.get('content').read()) print("downloaded coba file!\n") except: #pass # print(traceback.print_exc()) print("error downloading") mail.logout()
def downloadattachments(hst, usrname, passkey, fromuser): if os.path.exists("attachments"): pass else: os.mkdir("attachments") download_folder = os.getcwd() + "/attachments" if not os.path.isdir(download_folder): os.makedirs(download_folder, exist_ok=True) mail = Imbox(hst, username=usrname, password=passkey, ssl=True, ssl_context=None, starttls=False) # currently it will receive message from user called [email protected] if you want to change to specific user pass its name messages = mail.messages(sent_from=fromuser) # defaults to inbox for (uid, message) in messages: # mail.mark_seen(uid) # optional, mark message as read for idx, attachment in enumerate(message.attachments): try: att_fn = attachment.get('filename') download_path = f"{download_folder}/{att_fn}" print(download_path) with open(download_path, "wb") as fp: fp.write(attachment.get('content').read()) except: print(traceback.print_exc()) mail.logout()
def Ebox(): eBox = Imbox('imap-mail.outlook.com', username=emailAccount.hotname, password=emailAccount.hotpass, ssl=True, ssl_context=None) return eBox
def get_server(self): """Returns an active IMAP server""" return Imbox( self.imap_url, username=self.imap_user, password=self.imap_pass, ssl=True, )
def __init__(self): self.imbox = Imbox(settings.IMAP_HOST, username=settings.IMAP_USERNAME, password=settings.IMAP_PASSWORD) self.evernote = EvernoteClient(token=settings.EVERNOTE_TOKEN, sandbox=False) self.noteStore = self.evernote.get_note_store()
def move_to_folder(cls, uid, folder, email=None, password=None): email, password = cls.define_access_variables(email, password) with Imbox(cls.imap_server, username=email, password=password, ssl=True, ssl_context=None, starttls=False) as imbox: imbox.move(uid=uid, destination_folder=folder)
def mark_as_read(cls, uid, email=None, password=None): email, password = cls.define_access_variables(email, password) with Imbox(cls.imap_server, username=email, password=password, ssl=True, ssl_context=None, starttls=False) as imbox: imbox.mark_seen(uid)
def sync(imap_endpoint, couchdb_endpoint, username, password, from_date, starttls=False): # https://pythonhosted.org/CouchDB/getting-started.html # assumes couchdb is running prior to running this... couch = couchdb.Server(couchdb_endpoint) dbname = 'messages_%s' % sha.sha(username).hexdigest() db = None try: db = couch[dbname] except: db = couch.create(dbname) # db didn't exist so create # https://github.com/martinrusev/imbox # note: using fresher fork: https://github.com/balsagoth/imbox imap_server = imap_endpoint imap_port = None imbox = None ssl = 'SSL' if starttls: ssl = 'STARTTLS' try: imap_server, imap_port = imap_endpoint.split(":") imbox = Imbox(imap_server, port=imap_port, username=username, password=password, ssl=ssl) except: imbox = Imbox(imap_server, username=username, password=password, ssl=ssl) # todo: sync point (set from_date to) day prior to most recent day stored in db all_messages = imbox.messages(date__gt=from_date) print "syncing messages to couch db [%s]" % db for uid, message in all_messages: print uid, message.subject, sync_message_with_couchdb(uid, message, db)
def mark_as_seen(self, uid): with Imbox( self.server, username=self.userName, password=self.password, ssl=self.ssl, ssl_context=self.ssl_context, starttls=self.starttls, ) as imbox: imbox.mark_seen(uid)