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 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 mailsOfDay(self, day): imbox = Imbox(self.host, username=self.keys.username, password=self.keys.password, ssl=True) beforeDay = day - datetime.timedelta(days=1) afterDay = day + datetime.timedelta(days=1) rawMessages = imbox.messages(date__gt=beforeDay.strftime("%d-%b-%Y"), date__lt=afterDay.strftime("%d-%b-%Y")) data = [] for uid, message in rawMessages: date = self.convertRfcDateIncTimezone_Easier(message.date) if date.date() == day: data.append((uid, date, message)) return data
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 get_unread_email(mailbox_credentials): all_unread_messages = [] for mailbox in app_config.mailbox_credentials: imbox = Imbox(mailbox['imapserver'], username=mailbox['username'], password=mailbox['password'], ssl=True) try: this_mailbox_unread_messages = imbox.messages(unread=True) except: syslog.syslog(syslog.LOG_ERROR, "Error connecting to IMAP server "+mailbox['imapserver']) try: all_unread_messages.append(this_mailbox_unread_messages) #List exists, so postnum > 1 except KeyError: all_unread_messages = [this_mailbox_unread_messages] #Must be a new list! return all_unread_messages
def handle(self, *args, **options): logger.info("===== Started check_mail =====") imbox = Imbox(SETTINGS['imap_host'], username=SETTINGS['username'], password=SETTINGS['password'], ssl=True) for project in Project.objects.all(): logger.info("=== Processing project %s ===" % project.title) self.handle_messages(imbox, project) for alias in ProjectAlias.objects.all(): logger.info("=== Processing alias '{alias}' for project '{project}' ===".format( alias=alias.email, project=alias.project.title) ) self.handle_messages(imbox, alias.project, email=alias.email, assignee=alias.assignee) imbox.logout() logger.info("===== Finished check_mail =====")
def mail_loop(user): username = user.username password = user.mail_pass logging.debug("user is " + username + " password is " + password) if "gmail.com" in username: server = "imap.gmail.com" elif "yahoo.com" in username: server = "imap.mail.yahoo.com" else: raise NotImplementedError("only yahoo.com and gmail.com imaps supported") imb = Imbox(server, username, password, ssl=True) msgs = imb.messages(unread=True, sent_from="*****@*****.**") v = VPS.query.filter(VPS.idvpss == user.idvpss).first() http_proxy = "http://" + "@".join([":".join([str(v.login), str(v.password)]), ":".join([str(v.ip), str(v.port)])]) my_env = os.environ.copy() my_env["http_proxy"] = http_proxy my_env["http_proxy"] = http_proxy for uid, msg in msgs: imb.mark_seen(uid) confirm_url = re.findall("https://post.craigslist.org/./.+/.+\r", msg.body["plain"][0])[0].replace("\r", "") logging.debug("confirm link is " + confirm_url) subprocess.call( [ "python", "cragapp/admanager.py", "--action", "confirm", "--confirm_url", confirm_url, "--username", username, ], env=my_env, ) # grab current ads state after confirmation subprocess.call(["python", "cragapp/syncronizer.py", "userscrap", "--iduser", str(user.idusers)], env=my_env)
class Reader(object): """ Reads the email and stuff. """ def __init__(self): self.inbox = Imbox("imap.gmail.com", username=userName, password=userPassword, ssl=True) self.getMessages() def getMessages(self): self.messages = self.inbox.messages(unread=True) return self.messages def listMessageIds(self): """ More or less debug. """ for message in self.messages: print(message[0])
class Everclip(object): 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 get_new_emails(self): emails = self.imbox.messages(sent_to=settings.TO_ADDRESS, unread=True) return list(emails) def process_email(self, email): # Plain is given as a list - in case of multipart perhaps? # 1. Identify URL's in email plain = ' '.join(email.body['plain']) urls = extract_urls_from_text(plain) # NOTE: what to do about multiple links? include all of them? For now, # just choose the first one. url = urls[0] # 2. Extract URL content title, text = extract_url_content(url) # 3. Add to EverNote note = Types.Note() note.title = title note.content = text_to_enml(text).encode('utf8') attributes = Types.NoteAttributes() attributes.sourceURL = url note.attributes = attributes note = self.noteStore.createNote(note) def run(self): # TODO: make this a self-running daemon that continously checks for new # emails at regular intervals. print "Checking for new emails..." emails = self.get_new_emails() print "No. of new emails: %d" % (len(emails),) for id, email in emails: self.process_email(email)
class GmailReader: def __init__(self): self.inbox = '' def create_and_login(self, username, password): self.inbox = Imbox('imap.gmail.com', username=str(username), password=str(password), ssl=True, ssl_context=None) def get_messages(self, folder): message_list = [] folder = self.inbox.messages(folder=folder) for uid, email in folder: message_list.append(Message(raw_msg=str(email.body['plain']))) return message_list
def pull(self, connection_info: JsonDict) -> Iterator[JsonDict]: """Pull emails from Gmail.""" self.imbox = Imbox( connection_info["hostname"], port=connection_info.get("port", None), username=connection_info["user"], password=connection_info["password"], ssl=True, ssl_context=None, starttls=False, ) self.search_url = connection_info["search_url"] self.search_date_format = connection_info["search_date_format"] self.mark_read = connection_info.get("mark_read", False) self.archive = connection_info.get("archive", False) self.archive_folder = connection_info["archive_folder"] kwargs = {} from_ = connection_info.get("from") if from_: kwargs["sent_from"] = from_ label = connection_info.get("label") if label: kwargs.update(folder="all", label=label) folder = connection_info.get("folder") if folder: kwargs.update(folder=folder) messages = self.imbox.messages(**kwargs) if not messages: return [] for uid, message in messages: self.current_uid = uid date = pendulum.instance(message.parsed_date).date() url = self.build_search_url(from_, date, date.add(days=1)) subject: str = message.subject yield { "uid": uid.decode(), "url": url, "subject": " ".join(subject.splitlines()), "parsed_date": message.parsed_date.isoformat(), }
def connect_to_imap(self): username = self.config.get('username', None) password = self.config_dir.children['password']._content host = self.config.get('host', None) ssl = self.config.get('ssl', '0') if not (username and password and host): self.set_status( 'ERROR: you must set your username, password and host.\ ({}, {}, {} ({}))'.format(username, '*' if password else 'None', host, ssl)) else: self.set_status('Connecting to {}'.format(host)) try: self.imbox = Imbox(host, username=username, password=password, ssl=(ssl != '0')) except Exception as ex: print(username, host, ssl) print(ex) self.set_status('Error: {}'.format(ex)) else: self.connected = True self.load_folder('INBOX')
# Use https://github.com/martinrusev/imbox as IMAP Client from imbox import Imbox import json # Load user credentials from JSON with open('config.json', 'r') as configFile: config = json.loads(configFile.read()) print(config['name']) # Open inbox imbox = Imbox('mail.sjtu.edu.cn', username=config['name'], password=config['password'], ssl=True, ssl_context=None) # Gets all messages all_messages = imbox.messages() for uid, message in all_messages: # FIXME: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc4 in position 1821: invalid continuation byte print(message)
# -*- coding: utf-8 -*- #import yagmail import keyring from imbox import Imbox #authcode = input("Authorization Code: ") #yagmail.register('*****@*****.**',authcode) mail_pass = keyring.get_password('yagmail','*****@*****.**') with Imbox('imap.qq.com','*****@*****.**',mail_pass,ssl=True) as imbox: # imap服务器地址,邮箱,密码,是否支持ssl print('正在连接QQ邮箱...') unread_mails = imbox.messages(unread=True) # 读取收件箱所有信息 for uid, messages in unread_mails: title = messages.subject sent_from = messages.sent_from print(f'收到来自{sent_from}的邮件\n邮件主题为:{title}\n') #imbox.mark_seen(uid) #print('已标记为已读')
def engine(): # TODO: format time in report for normal template if request.method == 'POST': report = [] time = datetime.now() print('START', (datetime.now() - time)) report.append('START ' + str(datetime.now() - time)) # Imbox - Python IMAP for Humans with Imbox('imap.yandex.com', username='******', password='******', ssl=True, ssl_context=None, starttls=False) as imbox: inbox_messages = imbox.messages(date__on=date.today()) print(inbox_messages) report.append(str(inbox_messages)) for uid, message in inbox_messages[::-1]: r = message.attachments[0]['content'] print(r) with open("new_price1.zip", "wb") as file: file.write(r.read()) print('DONE') break print('Downloaded', (datetime.now() - time)) report.append('Downloaded ' + str(datetime.now() - time)) print('Start unzip', (datetime.now() - time)) report.append('Start unzip ' + str(datetime.now() - time)) with zipfile.ZipFile("new_price1.zip", "r") as zip_ref: zip_ref.extractall(".") print('Unzipped', (datetime.now() - time)) report.append('Unzipped ' + str(datetime.now() - time)) print('Init RAM and update Base', (datetime.now() - time)) report.append('Init RAM and update Base ' + str(datetime.now() - time)) # Объявляю переменные и загружаю ОЗУ old_base = Product.query.all() new_base = parse_db() old_art_list = [] new_art_list = [] upd_count = 0 add_count = 0 del_count = 0 # Вывод количества элементов print(len(new_base), 'записей в прайсе') print(len(old_base), 'записей в БД') report.append(str(len(new_base)) + ' записей в прайсе') report.append(str(len(old_base)) + ' записей в БД') # Создание списков артикулов для ускорения индексации for el in old_base: old_art_list.append(el.article) for el in new_base: new_art_list.append(int(el[0])) print('Start update', (datetime.now() - time)) report.append('Start update ' + str(datetime.now() - time)) # Обновление БД (в ОЗУ) из прайса for prod in old_base: if prod.article in new_art_list: index = new_art_list.index(prod.article) prod.status = new_base[index][6] prod.price = new_base[index][7] prod.date_time = new_base[index][8] prod.delivery = new_base[index][9] db.session.add(prod) upd_count += 1 print('UPD finished', upd_count, (datetime.now() - time)) report.append('UPD finished ' + str(upd_count) + ' ' + str(datetime.now() - time)) # Если элемента в БД нет, создаю его for element in new_base: if int(element[0]) not in old_art_list: prod = Product( article=element[0], cat=element[1], sub_cat=element[2], sub_sub_cat=element[3], title=element[4], guarant=element[5], status=element[6], price=element[7], date_time=element[8], delivery=element[9], ) db.session.add(prod) add_count += 1 print('ADD finished', add_count, (datetime.now() - time)) report.append('ADD finished ' + str(add_count) + ' ' + str(datetime.now() - time)) # Если элемента в ПРЙСЕ нет, удаляю его for element in old_art_list: if element not in new_art_list: prod = Product.query.filter_by(article=element).first() db.session.delete(prod) del_count += 1 print('DEL finished', del_count, (datetime.now() - time)) report.append('DEL finished ' + str(del_count) + ' ' + str(datetime.now() - time)) # Передаю изменения из ОЗУ в БД db.session.commit() print('Обновлено', upd_count, 'товаров') print('Добавлено', add_count, 'товаров') print(' Удалено ', del_count, 'товаров') report.append('Обновлено ' + str(upd_count) + ' товаров') report.append('Добавлено ' + str(add_count) + ' товаров') report.append(' Удалено ' + str(del_count) + ' товаров') # Очистка памяти del new_base del old_base del new_art_list del old_art_list del upd_count del add_count del del_count del time report = '\n'.join(report) sendEMail(report) del report restart_server() return redirect('/'), PRODUCTS else: return render_template('engine.html')
def alta ( remitente ) : cursor, conn = fusuario(remitente) if cursor.fetchone() == None: add ( conn, cursor, ( remitente, 1, "diary", 7), "insert" ) else: add ( conn, cursor, ( remitente, 1, "diary", 7), "update" ) sendmail ("alta", remitente, 6) def baja ( remitente ) : cursor, conn = fusuario(remitente) rem = cursor.fetchone() if rem != None: delete( conn, cursor, rem[2]) sendmail ("baja", rem[2], 6) imbox = Imbox( IMAPserver, mailUser, PASSWORD, True) unread_messages = imbox.messages(unread=True) for iud, message in unread_messages: sender = message.sent_from[0]['email'] command = message.subject.lower() if sender == "*****@*****.**" or sender == "*****@*****.**": pass elif command.find('alta') > -1: alta( sender ) imbox.delete( iud) elif command.find('baja') > -1: baja( sender )
def iter_inbox(cls, email=None, password=None, offset=0, limit=0, unread=False, folder=None, sent_from=None, sent_to=None, date=None, date_from=None, date_until=None, mark_as_read=False, move_to_folder=None, **filters): def paginate(messages, offset, limit): paginate = None if offset and limit: paginate = (offset, offset + limit) elif offset: paginate = (offset, None) elif limit: paginate = [limit] if paginate: return islice(messages, *paginate) return messages email, password = cls.define_access_variables(email, password) # Prepare filters if unread: filters['unread'] = unread if folder: filters['folder'] = folder if sent_from: filters['sent_from'] = sent_from if sent_to: filters['sent_to'] = sent_to if date: filters['date__on'] = date if date_from: filters['date__gt'] = date_from if date_until: filters['date__lt'] = date_until with Imbox(cls.imap_server, username=email, password=password, ssl=True, ssl_context=None, starttls=False) as imbox: # Filtering messages = imbox.messages(**filters) # Pagination messages = paginate(messages, offset, limit) # Iterate for uid, message in messages: if mark_as_read: imbox.mark_seen(uid) if move_to_folder: imbox.move(uid=uid, destination_folder=move_to_folder) yield DottedDict({ 'id': uid, 'from': [DottedDict(i) for i in message.sent_from], 'to': [DottedDict(i) for i in message.sent_to], 'date': message.parsed_date, 'subject': message.subject, 'body': DottedDict(message.body), })
ee = email.message_from_string(email_json["raw_email"]) for i, j in ee.items(): email_json["other"][i] = ee.get_all(i) return email_json now = datetime.date.today() while True: for key in r.scan_iter(): login = json.loads(key) with Imbox(login["server"], username=login["user"], password=login["password"], ssl=True, ssl_context=None, starttls=False) as imbox: inbox_messages_received_after = imbox.messages(date__gt=now) for uii, m in inbox_messages_received_after: if not r.get(key): r.set(key, int(uii)) elif int(uii) > int(r.get(key)): r.set(key, int(uii)) producer.send("email", transform_email(m)) producer.flush() print("Checked") time.sleep(1)
class Module(BaseModule): def init(self): self.alive = True imap_thread = threading.Thread(target=self.imap_thread) imap_thread.start() self.imap_thread = imap_thread self.imap_folders = {} self.status_file = VirtualFile(self.status_dir, 'status') self.create_password_file() self.connected = False self.connect_to_imap() self.messages = {} def create_password_file(self): WriteOnlyfile(self.config_dir, 'password') def set_status(self, msg): self.status_file.content = msg def connect_to_imap(self): username = self.config.get('username', None) password = self.config_dir.children['password']._content host = self.config.get('host', None) ssl = self.config.get('ssl', '0') if not (username and password and host): self.set_status( 'ERROR: you must set your username, password and host.\ ({}, {}, {} ({}))'.format(username, '*' if password else 'None', host, ssl)) else: self.set_status('Connecting to {}'.format(host)) try: self.imbox = Imbox(host, username=username, password=password, ssl=(ssl != '0')) except Exception as ex: print(username, host, ssl) print(ex) self.set_status('Error: {}'.format(ex)) else: self.connected = True self.load_folder('INBOX') def load_folder(self, name): self.set_status('Loading folder {}'.format(name)) imap_folder_dir = ImapFolderDir(self.root, name, self) self.imap_folders[name] = imap_folder_dir by_date_dir = VirtualDir(imap_folder_dir, 'by-date') by_sender_dir = VirtualDir(imap_folder_dir, 'by-sender') for uid in self.imbox.messages(folder=name): try: msg = self.imbox.get_message(uid) except Exception as ex: print('Error fetching message: {!r}'.format(ex)) else: self.messages[uid] = msg print( msg.sent_from['email'], msg.subject, msg.date, msg.parsed_date, msg.content_type ) def destroy(self): self.alive = False def imap_thread(self): while self.alive: time.sleep(10) def update_config(self, new_config): super().update_config(new_config) if not self.connected: self.connect_to_imap()
with yagmail.SMTP(user='******',host='邮箱的 smtp 网址') as yag: body = '正文内容' img = '图片文件路径' yag.send(to=['收件人邮箱地址'],cc=['抄送人邮箱地址'],bcc=['密送人邮箱地址']subject='主题',contents=[body,img,yagmail.inline('邮件内容中内嵌图片文件路径')],attachments=['附件文件路径']) print('发送成功!!') ''' 接收邮箱所有附件 ''' from imbox import Imbox pwd = keyring.get_password('yagmail','邮箱地址') with Imbox('邮箱的 imap 网址','邮箱地址',pwd,ssl=True) as imbox: all_inbox_messages = imbox.messages() for uid, message in all_inbox_messages: print(message.subject) print(message.body['plain']) ''' message.sent_from 发件人 message.sent_to 收件人 message.subject 主题 message.date 时间 message.body[‘plain’] 文本格式内容 message.body[‘html’] html格式内容 message.attchments 附件 '''
mail_server = mail_config_select("Select your Mail: ") if mail_server: userName = input("Mail ID: ") passwd = getpass.getpass(prompt="Password: "******"선택값 오류 입니다") raise SystemExit detach_dir = '.' if mail_server[0] not in os.listdir(detach_dir): os.mkdir(mail_server[0]) cal = dict((v, k) for k, v in enumerate(calendar.month_abbr)) with Imbox(mail_server[0], username=userName, password=passwd, ssl=mail_server[2], ssl_context=None, starttls=False) as imbox: status, folder_list = imbox.folders() mail_box_dir = mail_dir_select(imbox, "select mail box: ") # if mail_box_dir: # folder_messages = imbox.messages(folder=mail_box_dir) # else: # folder_messages = imbox.messages() for message_box in mail_box_dir: print("메일박스: ", message_box) try: folder_messages = imbox.messages( folder='"{}"'.format(message_box))
import keyring from imbox import Imbox import datetime user_name = "*****@*****.**" # keyring.set_keyring("<service>", "<user>", "<pwd>") pwd = keyring.get_password("qq", user_name) # imap服务器地址,邮箱,密码,是否支持ssl with Imbox('imap.qq.com', user_name, pwd, ssl=True) as imbox: # 未读、日期在2021.7.1之后 all_inbox_messages = imbox.messages(unread=True, date__gt=datetime.date(2021, 7, 1)) for uid, message in all_inbox_messages: subject = message.subject if "email-delete-test" in subject: print(message.subject) print(message.body['plain']) imbox.delete(uid) print('delete [uid=%d,subject=%s]' % (int(uid), subject))
import csv, traceback, openpyxl,re, glob, datetime, calendar, os from imbox import Imbox # get unread email from gmail and download attachments host = 'imap.gmail.com' username = '******' password = '******' download_folder = 'C:\\Users\\sariz\\OneDrive\\Desktop\\myPythonScripts\\budgetCSV' if not os.path.isdir('C:\\Users\\sariz\\OneDrive\\Desktop\\myPythonScripts\\budgetCSV'): os.makedirs('C:\\Users\\sariz\\OneDrive\\Desktop\\myPythonScripts\\budgetCSV', exist_ok= True) mail = Imbox(host, username= username, password= password, ssl= True, ssl_context = None, starttls= False) messages = mail.messages(subject = 'Transactions', unread = True) for (uid,message) in messages: mail.mark_seen(uid) 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: passprint(traceback.print_exc()) mail.logout
class EmailSource(BaseSource): """Email source.""" imbox: Imbox current_uid: Optional[bytes] = None search_url: str search_date_format: str mark_read = False archive = False archive_folder: str def pull(self, connection_info: JsonDict) -> Iterator[JsonDict]: """Pull emails from Gmail.""" self.imbox = Imbox( connection_info["hostname"], port=connection_info.get("port", None), username=connection_info["user"], password=connection_info["password"], ssl=True, ssl_context=None, starttls=False, ) self.search_url = connection_info["search_url"] self.search_date_format = connection_info["search_date_format"] self.mark_read = connection_info.get("mark_read", False) self.archive = connection_info.get("archive", False) self.archive_folder = connection_info["archive_folder"] kwargs = {} from_ = connection_info.get("from") if from_: kwargs["sent_from"] = from_ label = connection_info.get("label") if label: kwargs.update(folder="all", label=label) folder = connection_info.get("folder") if folder: kwargs.update(folder=folder) messages = self.imbox.messages(**kwargs) if not messages: return [] for uid, message in messages: self.current_uid = uid date = pendulum.instance(message.parsed_date).date() url = self.build_search_url(from_, date, date.add(days=1)) subject: str = message.subject yield { "uid": uid.decode(), "url": url, "subject": " ".join(subject.splitlines()), "parsed_date": message.parsed_date.isoformat(), } def build_search_url(self, from_: str = None, after: pendulum.Date = None, before: pendulum.Date = None) -> str: """Build the Gmail search URL.""" search_terms = [] if from_: search_terms.append(f"from:({from_})") if after: search_terms.append(f"after:{after.format(self.search_date_format)}") if before: search_terms.append(f"before:{before.format(self.search_date_format)}") quoted_terms = quote_plus(" ".join(search_terms)) return f"{self.search_url}{quoted_terms}" def on_success(self): """Mark email as read and/or archive it, if requested.""" if not self.current_uid: return if self.mark_read: self.imbox.mark_seen(self.current_uid) if self.archive: self.imbox.move(self.current_uid, self.archive_folder)
def make_imbox(account): imbox = Imbox('imap.gmail.com', account[acc_keys.username], base64.b64decode(account[acc_keys.password])) return imbox
class MailReceiver: address: str server_address: str port: int context: SSLContext server: Imbox def __init__(self, *, address, password, server_address, ssl_context): self.address = address self.server_address = server_address # self.port = port self.server = Imbox( hostname=server_address, username=address, password=password, ssl=True, ssl_context=ssl_context, ) def _add_label(self, flag: FLAGS): pass def _get_charset(self, message, default="utf-8"): # Taken from # http://ginstrom.com/scribbles/2007/11/19/parsing-multilingual-email-with-python/ """Get the message charset""" if message.get_content_charset(): return message.get_content_charset() if message.get_charset(): return message.get_charset() return default def _get_header(header_text, default="utf-8"): # Taken from # http://ginstrom.com/scribbles/2007/11/19/parsing-multilingual-email-with-python/ """Decode the specified header""" headers = decode_header(header_text) header_sections = [ str(text, charset or default) for text, charset in headers ] return u"".join(header_sections) def _get_body(self, message): # Taken from # http://ginstrom.com/scribbles/2007/11/19/parsing-multilingual-email-with-python/ """Get the body of the email message""" if message.is_multipart(): # get the plain text version only text_parts = [ part for part in typed_subpart_iterator(message, "text", "plain") ] body = [] for part in text_parts: charset = self._get_charset(part, self._get_charset(message)) body.append( str(part.get_payload(decode=True), charset, "replace")) return u"\n".join(body).strip() else: # if it is not multipart, the payload will be a string # representing the message body body = str(message.get_payload(decode=True), self._get_charset(message), "replace") return body.strip() def _move_message_to_folder(self, mail_id: int, folder_name: str): # if folder_name not in self.server.folders: # self.server.create_folder(folder_name) self.server.move(mail_id, folder_name) def get_not_seen_messages(self, mark_as_seen=False ) -> typing.List[ProcessedMessage]: messages = self.server.messages() mails: typing.List[ProcessedMessage] = [] for uid, message_data in messages: # email_message = email.message_from_bytes(message_data[b"RFC822"]) body = message_data.body["plain"] date = message_data.parsed_date subject = message_data.subject mails.append( ProcessedMessage(uid=uid, body=body, subject=subject.replace("Re:", ""), date=date)) self._move_message_to_folder(uid, Labels.SEEN_BY_DEV.value) return mails
def verify_account_in_html_email(http_settings, imap_server, username, password, sender, clues, match_substring=False, ssl=True): from bringyourownproxies.errors import VerificationLinkNotFound, AccountProblem email_box = Imbox(imap_server, username, password, ssl) msgs = email_box.messages(sent_from=sender) verification_link = None if isinstance(clues, (list, tuple)): if not isinstance(clues[0], (list, tuple)): if len(clues) != 2: raise AccountProblem( 'clues needs to be a list/tuple each containing 2 tuple/list items' ) else: clues = [clues] else: for clue in clues: if isinstance(clue, (list, tuple)): if len(clue) != 2: raise AccountProblem( 'clues needs to be a list/tuple each containing 2 tuple/list items' ) else: raise AccountProblem( 'clues needs to be a list/tuple each containing 2 tuple/list items' ) else: raise AccountProblem( 'clues needs to be a list/tuple each containing 2 tuple/list items' ) for msg in msgs: uid, email = msg doc = etree.fromstring(email.body['html'][0], HTMLParser()) for a in doc.xpath('//a'): for clue in clues: clue_attrib, clue_value = clue if clue_attrib == 'text': value_found = a.text else: value_found = a.attrib[clue_attrib] if match_substring: if value_found: if clue_value in value_found: verification_link = a.attrib['href'] break if clue_value == value_found: verification_link = a.attrib['href'] break if not verification_link: raise VerificationLinkNotFound( 'Cannot find email verification link sent from:{sender}'. format(sender=sender)) session = http_settings.session proxy = http_settings.proxy verify = session.get(verification_link, proxies=proxy) return verify.content
from imbox import Imbox imbox = Imbox('imap.gmail.com', username='',# Gmail Username password='',# Password ssl=True) # Gets all messages all_messages = imbox.messages() # Unread messages unread_messages = imbox.messages(unread=True) for uid,message in unread_messages: print message.sent_from print message.body.plain
def send_to_trello(mail_content, subject): r = requests.post("https://api.trello.com/1/cards?key=" + \ API_KEY + "&token=" + OAUTH_TOKEN + \ "&name=" + subject + "&idList=" + \ trello_list_id + "&desc=" + \ mail_content) return r with Imbox('imap.gmail.com', username=data['mail_username'], password=data['mail_password'], ssl=True, ssl_context=None, starttls=False) as imbox: fetch_mail_type = imbox.messages(sent_from=data['mail_from_username']) # Get all folders #status, folders_with_additional_info = imbox.folders() # Gets all messages from the inbox #all_inbox_messages = imbox.messages() for uid, message in fetch_mail_type: # Every message is an object with the following keys origin = message.sent_from
def check_inbox(self, r=30): import keyring while True: with Imbox('imap.gmail.com', username=EMAIL, password=keyring.get_password('system', KEYRING_USER), ssl=True, ssl_context=None, starttls=False) as imbox: unread_msgs = imbox.messages(unread=True) for uid, message in unread_msgs: imbox.mark_seen(uid) if '[CMD]' in message.subject: self.cmd(message) # make sure the sender is in the group # make sure grp is in groups # strip Re from subject field try: if 'Re' in message.subject.split(' ')[0]: del message.subject.split(' ')[0] grp = message.subject.split(' ')[0][1:-1] except IndexError: grp = '' try: msg_type = message.subject.split(' ')[1][1:-1] except IndexError: msg_type = '' try: option = message.subject.split(' ')[2][1:-1] except IndexError: option = '' if msg_type == 'CRT' and option == self.passwd: self.crt(message, grp) ## All other cmds require valid grp if not grp in self.groups: continue if not message.sent_from[0]['email'] in self.groups[ grp].values(): continue if 'BCAST' in message.subject: self.bcst(message, grp) if msg_type == 'WSPR' and option in '[From] Secret Santa]': self.wspr_to(message, grp) if msg_type == 'WSPR' and option in '[To] Secret Santa]': self.wspr_from(message, grp) if msg_type == 'ADD' and option == self.passwd: self.add(message, grp) if msg_type == 'RM': self.rm(message, grp) if msg_type == 'PWROLL' and option == 'Please_be_careful_1234567890' + self.passwd: self.roll(grp) if msg_type == 'ROLL': self.reroll[grp].update(message.sent_from[0]['name']) # 2/3 majority if len(self.reroll[grp]) > 2 * len( self.groups[grp]) / 3: self.roll(grp) sleep(r) # sleep for 30s to not spam email
import os from imbox import Imbox from sender import Mail, Message import pendulum imap = os.environ['GMAIL_IMAP'] user = os.environ['GMAIL_USER'] pwrd = os.environ['GMAIL_PASS'] if __name__ == "__main__": with Imbox(imap, username=user, password=pwrd, ssl=True, ssl_context=None, starttls=False) as imbox: drafts = imbox.messages(folder="[Gmail]/Drafts") todays_mail = [] for uid, msg in drafts: if 'schedmail' in msg.subject.lower(): date = msg.subject.lower().split(':')[1] today = pendulum.now().date().isoformat() subject_date = pendulum.parse(date).date().isoformat() if subject_date == today: todays_mail.append(msg) mail = Mail('smtp.gmail.com', port=587, username=user, password=pwrd, use_tls=True) for i in todays_mail: msg = Message(i.subject.split(':')[-1]) msg.fromaddr = (i.sent_from[0]['name'], i.sent_from[0]['email']) msg.to = [j['email'] for j in i.sent_to]
primary_working_directory = os.path.dirname(os.path.realpath(__file__)) download_folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'retrieved_downloads', 'vinsolutions') print('Download forlder is: ', download_folder) if not os.path.isdir(download_folder): os.makedirs(download_folder, exist_ok=True) #Builds list of day params as integers to match imbox's weird snytax today = date.today().strftime('%Y,%m,%d').split(',') today = [int(x) for x in today] print(today) mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False) #This is Part 1 of 2 that downloads the BDC Data from emails in today's inbox messages = mail.messages(subject='VSLeadPull', date__on=date(today[0], today[1], today[2])) # Subject Contains String 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}/BDC_{att_fn}" print("Download: ", download_path)
def new_message(request): imbox = Imbox('imap.gmail.com', username='******', password='******', ssl=True, ssl_context=None, starttls=False) last_message = IncomingEmail.objects.first() ll = LastMessage.objects.all()[0] if last_message != None: ll.date = last_message.date ll.save() last_message_date = ll.date print(last_message_date) all_inbox_messages = imbox.messages() for uid, message in all_inbox_messages: d = message.date.replace('(GMT)', '').strip() date = datetime.strptime(d, '%a, %d %b %Y %H:%M:%S %z') if date > last_message_date: #uid = message.message_id.replace('<','').replace('@mail.gmail.com>','') subject = message.subject from_name = message.sent_from[0]['name'] from_email = message.sent_from[0]['email'] to_name = message.sent_to[0]['name'] to_email = message.sent_to[0]['email'] body_plain = message.body['plain'][0] body_html = message.body['html'][0] date = date incoming = IncomingEmail( uid=uid, subject=subject, from_name=from_name, from_email=from_email, to_name=to_name, to_email=to_email, body_plain=body_plain, body_html=body_html, date=date, ) incoming.save() print('yenimail eklendi') new_coming_message = IncomingEmail.objects.filter( date__gt=last_message_date) reply_array = [] #my_incoming = [] #all_incoming = [] for mail in new_coming_message: reply_persons = mail.reply_persons.all() reply_p = serializers.serialize('json', reply_persons) reply_array.append(reply_p) #if person in reply_persons: # my_incoming.append([mail,reply_persons]) #all_incoming.append([mail,reply_persons]) new_msg = serializers.serialize('json', new_coming_message) reply_json = json.dumps(reply_array) data = {'reply': reply_json, 'new_msg': new_msg} return JsonResponse(data)
async def call(): print("websocet contect kurdu ve 100 sn bir new message istiyor!!!") imbox = Imbox('imap.gmail.com', username='******', password='******', ssl=True, ssl_context=None, starttls=False) last_message = MessageBlock.objects.last() ll = LastMessage.objects.all()[0] if last_message != None: ll.date = last_message.date ll.save() last_message_date = ll.date print(last_message_date, "consemer içinden son mesaj zamanı") all_inbox_messages = imbox.messages(date__gt=last_message_date) new_messages = [] instance_new_messages = [] for uid, message in all_inbox_messages: d = message.date.replace('(GMT)', '').strip() date = datetime.strptime(d, '%a, %d %b %Y %H:%M:%S %z') if date > last_message_date: # uid = message.message_id.replace('<','').replace('@mail.gmail.com>','') subject = message.subject my_attachments = message.attachments print('*****************Sadece gelen mesaj öncekileri istemiyoruz************************') cc = message.body['html'][0] print(cc.partition("<br>")) print('*****************************************') instance = cc.partition('<br>') only_coming_message = instance[0] from_name = message.sent_from[0]['name'] from_email = message.sent_from[0]['email'] to_name = message.sent_to[0]['name'] to_email = message.sent_to[0]['email'] body_plain = message.body['plain'][0] body_html = only_coming_message date = date incoming = Message( message_type='incoming', uid=uid, subject=subject, name=from_name, email=from_email, body_plain=body_plain, body_html=body_html, date=date, ) # incoming.mail_attachments.add() incoming.save() new_messages.append(incoming) instance_new_messages.append(incoming) print('yenimail eklendi db-ye ve new_mails array ine eklendi eklendi') all_message_blocks = MessageBlock.objects.all() message_blocks = [[e.email, e.subject] for e in all_message_blocks] print(message_blocks, "Query deneme") for message in new_messages: for message_block in all_message_blocks: if message_block.messages.filter(subject='Re:{}'.format(message.subject),email=message.email).exists(): print('cevap message ı geldi') instance_new_messages = [x for x in instance_new_messages if x != message] print(instance_new_messages,'Buda hiç bir messaga bloğunda olmayan mesagges') new_blocks = [ MessageBlock( subject=nw_blk.subject, name=nw_blk.name, email=nw_blk.email, date=nw_blk.date ) for nw_blk in instance_new_messages ] print(new_blocks,"news blocks listesi") for i in range(len(new_blocks)): new_blocks[i].save() new_blocks[i].messages.add(instance_new_messages[i]) await self.send(text_data=json.dumps({ 'message': 'message' }))
if cursor.fetchone() == None: add(conn, cursor, (remitente, 1, "diary", 7), "insert") else: add(conn, cursor, (remitente, 1, "diary", 7), "update") sendmail("alta", remitente, 6) def baja(remitente): cursor, conn = fusuario(remitente) rem = cursor.fetchone() if rem != None: delete(conn, cursor, rem[2]) sendmail("baja", rem[2], 6) imbox = Imbox(IMAPserver, mailUser, PASSWORD, True) unread_messages = imbox.messages(unread=True) for iud, message in unread_messages: sender = message.sent_from[0]['email'] command = message.subject.lower() if sender == "*****@*****.**" or sender == "*****@*****.**": pass elif command.find('alta') > -1: alta(sender) imbox.delete(iud) elif command.find('baja') > -1: baja(sender)
def verify_account_in_plain_email(http_settings, imap_server, username, password, sender, regexes, ssl=True): import re from imbox import Imbox from bringyourownproxies.errors import VerificationLinkNotFound, AccountProblem email_box = Imbox(imap_server, username, password, ssl) msgs = email_box.messages(sent_from=sender) verification_link = None if isinstance(regexes, (list, tuple)): if not isinstance(regexes[0], (list, tuple)): if len(regexes) != 2: raise AccountProblem( 'regexes needs to be a list/tuple each containing 2 tuple/list items' \ ' one item is the regex the other is the group num when found') else: regexes = [regexes] else: for regex in regexes: if isinstance(regex, (list, tuple)): if len(regex) != 2: raise AccountProblem( 'regexes needs to be a list/tuple each containing 2 tuple/list items' \ ' one item is the regex the other is the group num when found') else: raise AccountProblem( 'regexes needs to be a list/tuple each containing 2 tuple/list items' \ ' one item is the regex the other is the group num when found') else: raise AccountProblem( 'regexes needs to be a list/tuple each containing 2 tuple/list items' \ ' one item is the regex the other is the group num when found') for msg in msgs: uid, email = msg content = email.body['plain'][0] for regex_config in regexes: regex, group_num = regex_config found = re.search(regex, content) if found: verification_link = found.group(group_num) break if verification_link: break if not verification_link: raise VerificationLinkNotFound('Could not find verification' \ 'link from sender:{sender}'.format(sender=sender)) session = http_settings.session proxy = http_settings.proxy verify = session.get(verification_link, proxies=proxy) return verify.content
class EmailSource(BaseSource): """Email source.""" imbox: Imbox current_uid: Optional[bytes] = None search_url: str search_date_format: str mark_read = False archive = False archive_folder: str def pull(self, connection_info: JsonDict) -> Iterator[JsonDict]: """Pull emails from GMail.""" self.imbox = Imbox( connection_info["hostname"], port=connection_info.get("port", None), username=connection_info["user"], password=connection_info["password"], ssl=True, ssl_context=None, starttls=False, ) self.search_url = connection_info["search_url"] self.search_date_format = connection_info["search_date_format"] self.mark_read = connection_info.get("mark_read", False) self.archive = connection_info.get("archive", False) self.archive_folder = connection_info["archive_folder"] kwargs = {} search_from = connection_info.get("from") if search_from: kwargs["sent_from"] = search_from label = connection_info.get("label") if label: kwargs.update(folder="all", label=label) folder = connection_info.get("folder") if folder: kwargs.update(folder=folder) messages = self.imbox.messages(**kwargs) if not messages: return [] for uid, message in messages: self.current_uid = uid date = pendulum.instance(message.parsed_date).date() subject: str = " ".join(message.subject.splitlines()) # First sender of the email message_from = message.sent_from[0].get( "email") if message.sent_from else None url = self.build_search_url(search_from or message_from, date, date.add(days=1), subject) yield { "from_": search_from or message_from, "uid": uid.decode(), "url": url, "subject": subject, "parsed_date": message.parsed_date.isoformat(), } def build_search_url(self, from_: str = None, after: pendulum.Date = None, before: pendulum.Date = None, subject=None) -> str: """Build the email search URL.""" search_terms = [] if from_: search_terms.append(f"from:({from_})") if after: search_terms.append( f"after:{after.format(self.search_date_format)}") if before: search_terms.append( f"before:{before.format(self.search_date_format)}") if subject: search_terms.append(f'subject:"{subject}"') quoted_terms = quote_plus(" ".join(search_terms)) return f"{self.search_url}{quoted_terms}" def on_success(self): """Mark email as read and/or archive it, if requested.""" if not self.current_uid: return if self.mark_read: self.imbox.mark_seen(self.current_uid) if self.archive: self.imbox.move(self.current_uid, self.archive_folder)
def test_user_connection_data(imap, username, password): try: Imbox(imap, username, password, True, None, False) return True except Exception as e: return False
def create_and_login(self, username, password): self.inbox = Imbox('imap.gmail.com', username=str(username), password=str(password), ssl=True, ssl_context=None)
# -*- coding:utf-8 -*- """ 作者:snackdeng 日期:2020/07/08 """ import keyring from imbox import Imbox password = keyring.get_password('yagmail', '*****@*****.**') print(password) with Imbox('imap.163.com', '*****@*****.**', 'DINODMVPZVSBPYKA', ssl=True) as imbox: all_index_messages = imbox.messages() # imbox.messages(unread=True) 未读邮件 # imbox.messages(flagged=True) 红旗邮件 # imbox.messages(send_to='*****@*****.**') 某收件人邮件 for uid, message in all_index_messages: # imbox.mark_seen(uid) 标记已读 # imbox.delete(uid) 删除邮件 print(message.subject) print(message.body['plain']) # message.sent_from 收件人 # message.sent_to 发件人 # message.subject 主题 # message.date 时间 # message.body['plain'] 文本格式内容 # message.body['html'] HTML格式内容 # message.attachments 附件
import traceback import argparse parser = argparse.ArgumentParser() parser.add_argument("-d", "--folder", help="Show download_folder", type=str) args = parser.parse_args() host = "imap.gmail.com" username = "******" password = '******' download_folder = args.folder 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() for (uid, message) in messages: mail.mark_seen(uid) for idx, attachment in enumerate(message.attachments): try: att_fn = attachment.get('filename') download_path = f"{download_folder}\{att_fn}" print(download_path) if ((att_fn.find('CV') != -1) or (att_fn.find('cv') != -1)) and ( (os.path.splitext(att_fn)[1] == '.pdf') or (os.path.splitext(att_fn)[1] == '.doc') or
config = json.load(f) username = config['username'] password = config['password'] server = config['server'] repo = config['gh_repo'] token = config['gh_token'] if 'ssl' in config: ssl = config['ssl'] if 'delete' in config: delete_after = int(config['delete']) if delete_after < 0: delete_after = 0 print("Config for " + username + " loaded!") with Imbox(server, username=username, password=password, ssl=ssl) as inbox: print("Connected...", end="") inbox_messages = inbox.messages(unread=True) print("mails loaded") for uid, message in inbox_messages: issueReport = "" issueReport += "# " + message.subject + "\r\n" issueReport += "Reported by: [" + message.sent_from[0]['name'] + "](mailto:" + message.sent_from[0][ 'email'] + ")\r\n" for line in message.body['plain']: issueReport += line inbox.mark_seen(uid) response = requests.post('https://api.github.com/repos/' + repo + '/issues', json={ 'title': message.subject, 'body': issueReport
from imbox import Imbox '''imap.gmail.com''' imbox = Imbox('imap.mail.yahoo.com', username='******', password='******', ssl=True) unread_messages = imbox.messages(unread=True) for uid, message in unread_messages: print("Sent from ", message.sent_from) print("Subject ", message.subject) print("Body ", message.body.get('plain'))
def login(self, *args, **kwds): self.args = args self.kwds = kwds with Imbox(*args, **kwds): self.connected = True
def handle(self): if not self.connected: raise Exception('Not connected') return Imbox(*self.args, **self.kwds)
class EmailRecv(EmailBase): """ messages.sent_from 发件人 messages.sent_to 收件人 messages.subject 邮件主题 messages.date 发送日期 messages.body['plain'] 文本格式的正文 messages.body['html'] HTML格式的正文 messages.attachments 附件 messages.parsed_date datetime 类型 """ def __init__(self, *args, **kwargs): self._server = None super(EmailRecv, self).__init__(*args, **kwargs) 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 logout(self, *args, **kwargs): """ 退出 :param args: :param kwargs: :return: """ if isinstance(self._server, Imbox): try: self._server.logout() except Exception as e: logging.error('logout error %s', e) def folders(self): temp_list = list() folder_tuple = self._server.folders() if folder_tuple[0] != 'OK': return for folder in folder_tuple[1]: readable_folder = mail_decode(folder) temp_list.append(readable_folder.split()[-1].strip('"')) return temp_list def mark_seen(self, uuid): """ 标记本邮件已读 :param uuid: 邮箱唯一编号 :return: """ self._server.mark_seen(uuid) def mark_flag(self, uuid): """ 标记红旗邮件 :param uuid: 邮箱唯一编号 :return: """ self._server.mark_flag(uuid) def delete(self, uuid): """ 删除邮件 :param uuid: 邮箱唯一编号 :return: """ self._server.delete(uuid) def read(self, **kwargs): """ param folder: INBOX: (收件箱) 草稿箱 已发送 已删除 垃圾邮件 病毒邮件 广告邮件 param unread: 未读邮件 bool param unflagged: 不是红旗邮件 bool param flagged: 红旗邮件 bool param sent_from: 读取某个发件人的邮件 str param sent_to: 读取某个收件人的邮件 str param date__gt: 某天之后 param date__lt: 某天之前 datetime.date(2019, 10, 28) param lookup_error_ignore: 忽略LookUpError 错误 Bool param date__on:某天 param subject: 主提邮件 :param kwargs: :return: iter obj example (email_id: str, msg:object ) """ lookup_error_ignore = kwargs.pop('lookup_error_ignore', False) self.__parser_folders(kwargs) all_messages = self._server.messages(**kwargs) if lookup_error_ignore: return self.iter_all_messages(all_messages) return all_messages @staticmethod def iter_all_messages(all_messages): n = 0 length = len(all_messages._uid_list) # noqa while n < length: try: uid = all_messages._uid_list[n] # noqa msg = all_messages._fetch_email(uid) # noqa n += 1 yield uid, msg except LookupError as e: logging.error('uid %s error %s', uid, e) # noqa n += 1 @staticmethod def __parser_folders(folder_other): folder = folder_other.pop('folder', None) if folder: folder_other['folder'] = mail_encode(folder) def copy(self, uid, destination_folder): """ :param uid: 邮箱唯一编号 :param destination_folder: 目标文件夹 :return: """ return self._server.copy(uid, destination_folder) def move(self, uid, destination_folder): """ :param uid: 邮箱唯一编号 :param destination_folder: 目标文件夹 :return: """ self._server.move(uid, destination_folder)
def run(self, config, title): self._title = title SERVER_URL = str(config['mt_server_url']) CHANNEL_ID = str(config['mt_channel_id']) while True: try: imbox = Imbox(config["server"], username=config["username"], password=config["password"], ssl=config["ssl"] == 'True', ssl_context=config["ssl_context"], starttls=config["starttls"] == 'True') except: self.write_error_log("Failed to connect to Mailserver.") sys.exit(2) try: unread_inbox_messages = imbox.messages(unread=True) for uid, message in unread_inbox_messages: s = requests.Session() s.headers.update({ "Authorization": "Bearer " + str(config['mt_bearer']) }) msg = "---\r\n|Field|Value|\r\n|---|---|\r\n" if self.config_boolean(config["mail_subject"]): msg += self.add_message_field("Subject", message.subject) if self.config_boolean(config["mail_sent_from"]): msg += self.add_message_field("Sender", message.sent_from) if self.config_boolean(config["mail_sent_to"]): msg += self.add_message_field("Recipient", message.sent_to) if self.config_boolean(config["mail_date"]): msg += self.add_message_field("Date", message.date) FILE_IDS = list() if self.config_boolean(config["mail_attachments"]): try: att_counter = 0 for att in message.attachments: try: if att_counter < 5: # max. 5 attachments per post readable_hash = hashlib.sha256( att['content'].read()).hexdigest() FILE_PATH = str( config['workingdir']) + str( config['data_folder']) + str( att['filename'] ) #str(readable_hash) att['content'].seek(0) with open(FILE_PATH, 'wb') as file: file.write(att['content'].read()) form_data = { "channel_id": ('', CHANNEL_ID), "client_ids": ('', str(readable_hash)), "files": (os.path.basename(FILE_PATH), open(FILE_PATH, 'rb')), } r = s.post(SERVER_URL + '/api/v4/files', files=form_data) FILE_IDS.append( str(r.json()["file_infos"][0] ["id"])) att_counter += 1 msg += "|Attachment| `" + str( att['filename'] ) + " [sha256: " + readable_hash + "]`|\r\n" except: self.write_error_log( "Failed to save attachment to disk and post to Mattermost." ) except: self.write_error_log("Failed to parse attachment.") if self.config_boolean(config["mail_message_id"]): msg += self.add_message_field("Message-ID", message.message_id) if self.config_boolean(config["mail_headers"]): msg += self.add_message_field("Headers", message.headers) if self.config_boolean(config["mail_body_plain"]): msg += self.add_message_field("Body", message.body["plain"]) elif self.config_boolean(config["mail_body_html"]): msg += self.add_message_field("Body", message.body["html"]) if str(config["mail_tlp"]) != "": msg += self.add_message_field("TLP", config["mail_tlp"]) data_dict = { "channel_id": CHANNEL_ID, "message": msg, "file_ids": [] } data_dict["file_ids"] = FILE_IDS data = json.dumps(data_dict) p = s.post(SERVER_URL + '/api/v4/posts', data) imbox.mark_seen(uid) except Exception as e: self.write_error_log("Failed to parse email message.") exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] uid = None if "uid" in sys.exc_info()[2].tb_next.tb_frame.f_locals: uid = sys.exc_info()[2].tb_next.tb_frame.f_locals["uid"] if uid is not None: imbox.mark_seen(uid) time.sleep(int(config["sleep"]))
def get_emails(): try: given_params = app.current_request.query_params # print(given_params) folder = given_params["RequestedFolder"] email = given_params["email"] password = given_params["password"] imap_server = given_params["imap_server"] imap_port = given_params["imap_port"] # print(folder) # print(email) # print(password) # print(imap_server) # print(imap_port) with Imbox(hostname=imap_server, port=imap_port, username=email, password=password, ssl=True) as imbox: all_inbox_messages = imbox.messages(folder=folder)[::-1][0:10] if all_inbox_messages: # if all_inbox_messages[::-1][0:10]: randnum = 0 emails = [] # for uid, message in all_inbox_messages[::-1][0:10]: for uid, message in all_inbox_messages: # print(uid) randnum = randnum + 1 email = {} email["id"] = str(uuid4()) # name if message.sent_from[0]["name"]: email["name"] = str(message.sent_from[0]["name"]) # email if message.sent_from[0]["email"]: email["email"] = str(message.sent_from[0]["email"]) # body (html) if message.body["html"]: email["body_html"] = str(message.body["html"][0]) # body (plain) if message.body["plain"]: email["body_plain"] = str(message.body["plain"][0]) # subject if message.subject: email["subject"] = str(message.subject) # date if message.date: try: email["date"] = parser.parse(str( message.date)).strftime( '%Y-%m-%dT%H:%M:%S.%f')[:-3] + "Z" except: email["date"] = datetime.now().strftime( '%Y-%m-%dT%H:%M:%S.%f')[:-3] + "Z" # # bucket # email["bucket"] = "Inbox" # read # email["read"] = False # if message.attachments: # email["attachments"] = message.attachments emails.append(email) print(randnum) return Response(body={ 'emails': emails, "number_of_emails": len(emails) }, status_code=200, headers=custom_headers) else: return Response(body={'emails': "No Emails!"}, status_code=204, headers=custom_headers) except Exception as error: # print("Get Emails Error:") # print(error) return Response(body={'AppError': str(error)}, status_code=500, headers=custom_headers)
def __init__(self): self.inbox = Imbox("imap.gmail.com", username=userName, password=userPassword, ssl=True) self.getMessages()