def process_message(self, peer, sender, recipients, body, mail_options=None, rcpt_options=None): """ Handle incoming message. :param tuple peer: Connecting client's IP and port number. :param string sender: Sender email address. :param string recipients: List or recipient email addresses. :param string data: Message body with headers. :param list mail_options: Unused. :param list rcpt_options: Unused. :return: Nothing. :rtype: None """ try: mail = mailparser.parse_from_bytes(body) if mail['subject'] == 'mailwarm bridge restart': exit(0) if DUMP_FOLDER: self.dump(mail) if API_KEY is not None: self.forward(mail) except Exception as e: return '554 Transaction failed: %s' % e
def process_ingestion_emails(): """ Gets all new mail documents in the bucket and process each message. """ processor = CalendarInteractionEmailProcessor() for message in get_mail_docs_in_bucket(): source = message['source'] try: documents.delete_document(bucket_id=BUCKET_ID, document_key=message['source']) except Exception as e: logger.exception('Error deleting message: "%s", error: "%s"', source, e) continue try: email = mailparser.parse_from_bytes(message['content']) processed, reason = processor.process_email(message=email) if not processed: logger.error('Error parsing message: "%s", error: "%s"', source, reason) else: logger.info(reason) except Exception as e: logger.exception('Error processing message: "%s", error: "%s"', source, e) logger.info( 'Successfully processed message "%s" and deleted document from bucket "%s"', source, BUCKET_ID, )
def read_mail(self, id): typ, data = self.M.fetch(id, '(RFC822)') for response_part in data: if isinstance(response_part, tuple): parts = mailparser.parse_from_bytes(response_part[1]) if len(parts._from[0][0]) > 3 and check_clean( parts._from[0][0]): sender = clean_string(parts._from[0][0]) else: sender = clean_string(parts._from[0][1]) betreff = clean_string(parts.subject) print(parts._from) to_speak = f"Neue Nachricht. Von {sender}. Betreff. {betreff}".replace( r"/\n/g", ' ') print(to_speak) if len(to_speak) > 200: print("Too long message, probably there is an error") print(to_speak) break filename = f"mail{self.counter}.mp3" self.counter += 1 try: get_audio(to_speak, tofile=filename) except RuntimeError as e: print(e) break if os.uname()[1] == "raspberrypi": subprocess.call(["mplayer", filename], stdout=subprocess.DEVNULL) else: playsound(filename, True) print("playing sound")
def get_emails(self): with IMAPClient(self.host, ssl=self.ssl) as server: try: server.login(self.login, self.password) except LoginError: logger.error("Could not authenticate.") return server.select_folder(self.folder, readonly=self.read_only) mails = server.search(self.criteria) for uid, message_data in server.fetch(mails, "RFC822").items(): if uid <= self.last_uid: continue try: mail = mailparser.parse_from_bytes(message_data[b"RFC822"]) except TypeError: pass else: yield mail, uid
def __init__(self, message_bytes: bytes, include_raw_body: bool, save_file: bool, id_: int) -> None: """ Initialize Email class with all relevant data Args: id_: The unique ID with which the email can be fetched from the server specifically message_bytes: The raw email bytes include_raw_body: Whether to include the raw body of the mail in the incident's body save_file: Whether to save the .eml file of the incident's mail """ email_object = parse_from_bytes(message_bytes) self.id = id_ self.mail_bytes = message_bytes self.to = [mail_addresses for _, mail_addresses in email_object.to] self.cc = [mail_addresses for _, mail_addresses in email_object.cc] self.bcc = [mail_addresses for _, mail_addresses in email_object.bcc] self.attachments = email_object.attachments self.from_ = [ mail_addresses for _, mail_addresses in email_object.from_ ][0] self.format = email_object.message.get_content_type() self.html = email_object.text_html[0] if email_object.text_html else '' self.text = email_object.text_plain[ 0] if email_object.text_plain else '' self.subject = email_object.subject self.headers = email_object.headers self.raw_body = email_object.body if include_raw_body else None # According to the mailparser documentation the datetime object is in utc self.date = email_object.date.replace(tzinfo=timezone.utc) self.raw_json = self.generate_raw_json() self.save_eml_file = save_file self.labels = self._generate_labels()
def check_validity(self, file): try: email = mailparser.parse_from_bytes(file) return { 'Verified Type': self.extension, 'Subject': str(email.subject), 'Delivered To': str(email.to if email.to else email.delievered_to), 'From': str(email.from_), 'Attachments': ', '.join([f['filename'] for f in email.attachments]), 'Body': str(email.text_plain if email.text_plain else email.body), 'Headers': str(email.headers), 'Date': str(email.received if email.received else email.date), 'Timezone': str(email.timezone), } except Exception as e: return False
def get_emails(host: str, username: str, password: str, msg_type: str = "UNSEEN", last_uid: int = 0, read_only: bool = False) -> int: """Разделение текста на части :param host: Адрес IMAP сервера :param username: Имя пользователя (почтовый ящик) :param password: Пароль :param msg_type: Критерий для поиска писем (по умолчанию возвращаются только непрочитанные письма) :param last_uid: ID последнего прочитанного письма :param read_only: Не помечать письма прочитанными? :rtype: int :returns: Возвращает ID последнего прочитанного письма """ #try: os.chdir("/home/m.kostromin/send_tickets/INBOX") #except FileNotFoundError: #os.mkdir("INBOX") #os.chdir("/home/m.kostromin/send_tickets/INBOX") with IMAPClient(host) as server: server.login(username, password) server.select_folder("INBOX", readonly=read_only) mails = server.search(msg_type) for uid, message_data in server.fetch(mails, "RFC822").items(): if uid <= last_uid: continue mail = mailparser.parse_from_bytes(message_data[b"RFC822"]) subject = mail.subject from_ = " ".join(mail.from_[0]) dir_name = "{} от {}".format(subject, from_) #os.mkdir(dir_name) if not os.path.exists(dir_name): # os.mkdir(f"{dir_name}{random.randint(10000000, 999999999999)}") os.mkdir(dir_name) os.chdir(dir_name) if mail.text_plain: text_plain = "\n".join(mail.text_plain) with open("text_plain.txt", "w") as f: f.write("{}\n\n{}".format(dir_name, text_plain)) if mail.text_html: text_html = "\n".join(mail.text_html) with open("text_html.html", "w") as f: f.write(text_html) if mail.attachments: mail.write_attachments("attachments") os.chdir("../") os.chdir("../") return mails[-1] if mails else last_uid
def get_email_headers(self, index: int = 1) -> MIMEText: if not isinstance(index, int): raise ReadEmailServicesException( f"wrong arg type: {type(index)}; int required") email_headers = MIMEText("") if index <= 0: return email_headers status, response = self.__server.fetch( str(index).encode("utf-8"), "(BODY[HEADER.FIELDS (SUBJECT FROM TO DATE CONTENT-TYPE)])") if not response[0]: return email_headers headers = parse_from_bytes(response[0][1]).headers for header_name, header_value in headers.items(): header_value = BasicEmailHeadersParser.process_charset( header_value) if header_name.lower() == "content-type": header_value = header_value.split("boundary")[0] email_headers.set_type(header_value) continue email_headers[header_name] = header_value return email_headers
def get_inbox(): mail = imaplib.IMAP4_SSL(host) mail.login(username, password) mail.select("inbox") _, search_data = mail.search(None, "FROM", "'*****@*****.**'") my_message = [] for num in search_data[0].split(): email_data = {} _, data = mail.fetch(num, '(RFC822)') _, b = data[0] email_message = mailparser.parse_from_bytes(b) # for header in ['subject', 'to', 'from', 'date']: # print("{}: {}".format(header, email_message[header])) # email_data[header] = email_message[header] # for part in email_message.walk(): # msg = '' # if part.get_content_type() == "text/plain": # body = part.get_payload(decode=True) # email_data['body'] = body.decode() # msg = str(email_data['body']).replace('\r\n', '').replace('=3D', '=').replace(' ', '').format() # elif part.get_content_type() == "text/html": # html_body = part.get_payload(decode=True) # email_data['html_body'] = html_body.decode() # msg = str(email_data['body']).replace('\r\n', '').replace('=3D', '=').format() # # my_message.append(msg) return email_message.body
def refresh_mailbox(self, mailbox): emails = [] self.connection.select(mailbox, readonly=True) self.connection.search(None, 'ALL') # print(self.get_n_emails(mailbox)) for i in range(1, self.get_n_emails(mailbox) + 1): _, msg_data = self.connection.fetch(str(i), "RFC822") mail = mailparser.parse_from_bytes(msg_data[0][1]) encrypted = False if (len(mail.text_plain) > 0) and "BEGIN PGP MESSAGE" in mail.text_plain[0]: encrypted = True emails.append({ "id": str(i), "date": str(mail.date), "delivered_to": mail.delivered_to, "from": mail.from_, "subject": mail.subject, "to": mail.to, "timezone": mail.timezone, "read": self.is_read(str(i)), "encrypted:": encrypted }) from pprint import pprint pprint(emails) return emails
def update(self): """Update data from Email API.""" self._attr = { ATTR_EMAILS: [], ATTR_TRACKING_NUMBERS: {} } emails = [] server = IMAPClient(self.imap_server, use_uid=True) try: server.login(self.email_address, self.password) server.select_folder(self.email_folder, readonly=True) except Exception as err: _LOGGER.error('IMAPClient login error {}'.format(err)) return False try: messages = server.search(self.flag ) for uid, message_data in server.fetch(messages, 'RFC822').items(): try: mail = parse_from_bytes(message_data[b'RFC822']) emails.append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, EMAIL_ATTR_BODY: mail.body }) self._attr[ATTR_EMAILS].append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, }) except Exception as err: _LOGGER.error('mailparser parse_from_bytes error: {}'.format(err)) except Exception as err: _LOGGER.error('IMAPClient update error: {}'.format(err)) self._attr[ATTR_COUNT] = len(emails) self._attr[ATTR_TRACKING_NUMBERS] = {} # empty out all parser arrays for ATTR, parser in parsers: self._attr[ATTR_TRACKING_NUMBERS][ATTR] = [] # for each email run each parser and save in the corresponding ATTR for email in emails: for ATTR, parser in parsers: try: self._attr[ATTR_TRACKING_NUMBERS][ATTR] = self._attr[ATTR_TRACKING_NUMBERS][ATTR] + parser(email) except Exception as err: _LOGGER.error('{} error: {}'.format(ATTR, err)) # remove duplicates for ATTR, parser in parsers: self._attr[ATTR_TRACKING_NUMBERS][ATTR] = list( dict.fromkeys(self._attr[ATTR_TRACKING_NUMBERS][ATTR])) server.logout()
def update(self): """Update data from Email API.""" self._attr = {ATTR_EMAILS: [], ATTR_TRACKING_NUMBERS: {}} emails = [] server = IMAPClient(self.smtp_server, use_uid=True) try: server.login(self.email_address, self.password) server.select_folder(self.email_folder, readonly=True) except Exception as err: _LOGGER.error('IMAPClient login error {}'.format(err)) return False try: messages = server.search(self.flag) for uid, message_data in server.fetch(messages, 'RFC822').items(): try: mail = parse_from_bytes(message_data[b'RFC822']) emails.append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, EMAIL_ATTR_BODY: mail.body }) self._attr[ATTR_EMAILS].append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, }) except Exception as err: _LOGGER.error( 'mailparser parse_from_bytes error: {}'.format(err)) except Exception as err: _LOGGER.error('IMAPClient update error: {}'.format(err)) self._attr[ATTR_COUNT] = len(emails) try: self._attr[ATTR_TRACKING_NUMBERS][ATTR_UPS] = parse_ups(emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_FEDEX] = parse_fedex(emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_USPS] = parse_usps(emails) self._attr[ATTR_TRACKING_NUMBERS][ ATTR_ALI_EXPRESS] = parse_ali_express(emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_NEWEGG] = parse_newegg( emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_ROCKAUTO] = parse_rockauto( emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_BH_PHOTO] = parse_bh_photo( emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_EBAY] = parse_ebay(emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_DHL] = parse_dhl(emails) except Exception as err: _LOGGER.error('Parsers error: {}'.format(err)) server.logout()
def idle_loop(host, user, password): imap_client = aioimaplib.IMAP4_SSL(host=host, timeout=30) yield from imap_client.wait_hello_from_server() login_response = yield from imap_client.login(user, password) if login_response.result == "NO": raise Exception("Authentication failed") response = yield from imap_client.select(IMAP_CHECK_FOLDER) while True: response = yield from imap_client.uid('fetch', '1:*', 'RFC822') # start is: 2 FETCH (UID 18 RFC822 {42} # middle is the actual email content # end is simply ")" # the last line is removed as it's only "success"-ish information # the iter + zip tricks is to iterate three by three iterator = iter(response.lines[:-1]) for start, middle, _end in zip(iterator, iterator, iterator): if not isinstance(middle, bytes): continue # index 3 because the actual id is at the position 3 # 0 1 2 3 # | | | | # v v v v # 2 FETCH (UID 18 RFC822 {42} email_uid = start.split(' ')[3] parsed_email = mailparser.parse_from_bytes(middle) treated_correctly = treat_email(parsed_email) if treated_correctly: yield from imap_client.uid( 'move', email_uid + ':' + email_uid , IMAP_SUCCESS_FOLDER, ) else: yield from imap_client.uid( 'store', email_uid, '-FLAGS', '(\Seen)' ) yield from imap_client.uid( 'move', email_uid + ':' + email_uid , IMAP_FAILURE_FOLDER, ) idle = yield from imap_client.idle_start(timeout=60) print((yield from imap_client.wait_server_push())) imap_client.idle_done() yield from asyncio.wait_for(idle, 30)
def __init__(self, message_bytes: bytes, include_raw_body: bool, save_file: bool, id_: int) -> None: """ Initialize Email class with all relevant data Args: id_: The unique ID with which the email can be fetched from the server specifically message_bytes: The raw email bytes include_raw_body: Whether to include the raw body of the mail in the incident's body save_file: Whether to save the .eml file of the incident's mail """ self.mail_bytes = message_bytes try: email_object = parse_from_bytes(message_bytes) except UnicodeDecodeError as e: demisto.info( f'Failed parsing mail from bytes: [{e}]\n{traceback.format_exc()}.' '\nWill replace backslash and try to parse again') message_bytes = message_bytes.replace(b'\\U', b'\\\\U').replace( b'\\u', b'\\\\u') email_object = parse_from_bytes(message_bytes) self.id = id_ self.to = [mail_addresses for _, mail_addresses in email_object.to] self.cc = [mail_addresses for _, mail_addresses in email_object.cc] self.bcc = [mail_addresses for _, mail_addresses in email_object.bcc] self.attachments = email_object.attachments self.from_ = [ mail_addresses for _, mail_addresses in email_object.from_ ][0] self.format = email_object.message.get_content_type() self.html = email_object.text_html[0] if email_object.text_html else '' self.text = email_object.text_plain[ 0] if email_object.text_plain else '' self.subject = email_object.subject self.headers = email_object.headers self.raw_body = email_object.body if include_raw_body else None # According to the mailparser documentation the datetime object is in utc self.date = email_object.date.replace(tzinfo=timezone.utc) self.raw_json = self.generate_raw_json() self.save_eml_file = save_file self.labels = self._generate_labels()
def get_body(self, index: int = 1) -> str: if not isinstance(index, int): raise ReadEmailServicesException( f"wrong arg type: {type(index)}; int required") try: status, data = self.__server.fetch( str(index).encode("utf-8"), "(RFC822)") email_message = parse_from_bytes(data[0][1]) return str(email_message.body) except Exception: self.__output(colored("[-] body fetching failed", "red")) return ""
def download_messages(auth, folder): with imapclient.IMAPClient(auth.imap) as server: server.login(auth.username, auth.password) server.select_folder(folder, readonly=True) messages = server.search('ALL') email_message_list = [] for uid, message_data in server.fetch(messages, 'RFC822').items(): email_message = mailparser.parse_from_bytes( message_data[b'RFC822']) m = Message(uid, email_message.date, email_message.from_[0][1], 'inbox', email_message.subject, email_message.body) email_message_list.append(m) return email_message_list
def get_last_mails(email,token): account = EmailAccount.objects.get(email=email,token=token) client = imapclient.IMAPClient('imap.yandex.ru') client.oauth2_login(email,token) mail_folders = ['Inbox','Spam',] user = EmailAccount.objects.get(email=email).user # loop th rough mail folders for folder in mail_folders: client.select_folder(folder) messages = client.search(['UNSEEN','NOT','Flagged']) print(messages) try: last_num = Email.objects.filter(folder=folder).last().num except: last_num = 0 for uid, message_data in client.fetch(messages,'RFC822').items(): if int(last_num) < uid: raw_email = message_data['RFC822'.encode()] email_message = mailparser.parse_from_bytes(raw_email) created = False try: new_email = Email.objects.get(folder=folder,user=user,num=uid, sender=sender, receiver=receiver, date=date) created=True except: new_email = Email() if created: continue new_email.user = user new_email.num = uid new_email.sender = email_message.from_[0][1] new_email.date = email_message.date new_email.subject = email_message.subject if email_message.subject != "" else 'No subject' content = email_message.text_html[0] if email_message.text_html != [] else "" new_email.content = content new_email.folder = folder new_email.flag = 'Unseen' new_email.save() for i in email_message.to: Receiver.objects.create(email=new_email,receiver=i[1]) if email_message.attachments is not None: for i in email_message.attachments: print(i['filename']) if Attachment.objects.filter(name=i['filename'],email=new_email).exists(): continue attachment = Attachment(name=i['filename'],email=new_email) attachment.file.save(i['filename'],ContentFile(base64.b64decode(i['payload']))) attachment.save() return 'Last mails saved'
def fetchAll(self, lower, upper): try: mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login(obj7.fetchCredential()[0], obj7.fetchCredential()[1]) mail.list() mail.select("inbox") # connect to inbox. result, data = mail.search(None, "ALL") ids = data[0] # data is a list. id_list = ids.split() # ids is a space separated string i = 1 for x in reversed(range(lower, upper)): latest_email_id = id_list[x] # get the latest result, data = mail.fetch( latest_email_id, "(RFC822)" ) # fetch the email body (RFC822) for the given ID raw_email = data[0][ 1] # here's the body, which is raw text of the whole email mail5 = mailparser.parse_from_bytes(raw_email) # print(mail5.body) for name in mail5.from_: self.audioString = str( str(i) + ". Name is " + name[0] + ". Mail ID is" + name[1] + ", Subject, is, " + mail5.subject) obj7.textToSpeech(self.audioString) obj7.textToSpeech( "Do you want to read the body of this message?") if obj7.hear() in ["yes", "ya", "yeah"]: obj7.textToSpeech( str(mail5.body.split("--- mail_boundary ---")[0])) i += 1 return True except IndexError: obj7.textToSpeech("\nNo more messages") #self.outcome = False return False except imaplib.IMAP4.error: obj7.textToSpeech( "Authentication Error! please check your credentials") return False except TypeError: obj7.textToSpeech("No credentials have been provided") return False
def ExtractSubPayload(filename): ''' Extract the subject and payload from the .eml file. ''' if not os.path.exists(filename): # dest path doesnot exist print("ERROR: input file does not exist:", filename) os.exit(1) print(filename) fp = open(filename, 'rb') b = fp.read() msg = mailparser.parse_from_bytes(b) return ProcessMessage(msg)
def search(self): try: imap = imaplib.IMAP4_SSL(host=self.host) imap.login(self.userName, self.password) imap.select() error, searchResult = imap.search(None, "SUBJECT " + self.subject) num = searchResult[0] if 0 != len(num): self.found = True if b' ' not in num: typ, data = imap.fetch(num, '(RFC822)') mail = mailparser.parse_from_bytes(data[0][1]) htmlParser = ParseLink() htmlParser.feed(mail.body) url = htmlParser.link if 0 != len(url): QDesktopServices.openUrl(url) self.delete(imap, num) self.errorList = "" else: self.errorList = "URL not found in mail" else: self.errorList = "More than one email found, possible attack" else: self.errorList = "Email not found" imap.close() imap.logout() except socket.gaierror as error: code, self.errorList = error.args self.found = True except imaplib.IMAP4.error as error: self.controller.resetPassword(self.address) reason, = error.args self.errorList = reason.decode() self.found = True self.done = True
def GetMimeMessage(service, user_id, msg_id, idx): """Get a Message and use it to create a MIME Message. Args: service: Authorized Gmail API service instance. user_id: User's email address. The special value "me" can be used to indicate the authenticated user. msg_id: The ID of the Message required. Returns: A MIME Message, consisting of data from Message. """ try: message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute() msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII')) mail = mailparser.parse_from_bytes(msg_str) msg_str = str(mail.text_plain) msg_str = msg_str.strip("") msg_str = clean_text(msg_str) msg_str = preprocess(msg_str) #print(msg_str) except errors.HttpError: print('An error occurred:') try: met = service.users().messages().get(userId=user_id, id=msg_id, format='metadata').execute() pay = met['payload'] head = pay['headers'] sub="" for h in head: if (h['name'] == 'Subject'): sub = "Subject: "+str(h['value']) except errors.HttpError: print('An error occurred:') filename = "./ham/email" file_extension = ".txt" new_fname = "{}-{}{}".format(filename, idx, file_extension) #print(new_fname) f= open(new_fname,"w+") f.write(sub+"\n") f.write(msg_str) f.close()
def update(self): """Update data from Email API.""" self._attr = {ATTR_EMAILS: [], ATTR_TRACKING_NUMBERS: {}} emails = [] server = IMAPClient(self.smtp_server, use_uid=True) try: server.login(self.email_address, self.password) server.select_folder(self.email_folder, readonly=True) except Exception as err: _LOGGER.error('IMAPClient login error {}'.format(err)) return False try: messages = server.search(self.flag) for uid, message_data in server.fetch(messages, 'RFC822').items(): try: mail = parse_from_bytes(message_data[b'RFC822']) emails.append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, EMAIL_ATTR_BODY: mail.body }) self._attr[ATTR_EMAILS].append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, }) except Exception as err: _LOGGER.error( 'mailparser parse_from_bytes error: {}'.format(err)) except Exception as err: _LOGGER.error('IMAPClient update error: {}'.format(err)) self._attr[ATTR_COUNT] = len(emails) for email in emails: for ATTR, parser in parsers: try: self._attr[ATTR_TRACKING_NUMBERS].setdefault(ATTR, []) self._attr[ATTR_TRACKING_NUMBERS][ATTR] = self._attr[ ATTR_TRACKING_NUMBERS][ATTR] + parser(email) except Exception as err: _LOGGER.error('{} error: {}'.format(ATTR, err)) server.logout()
def update(self): """Update data from Email API.""" import mailparser self._attr = {ATTR_EMAILS: [], ATTR_TRACKING_NUMBERS: {}} emails = [] try: messages = self.server.search('UNSEEN') for uid, message_data in self.server.fetch(messages, 'RFC822').items(): try: mail = mailparser.parse_from_bytes(message_data[b'RFC822']) emails.append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, EMAIL_ATTR_BODY: mail.body }) self._attr[ATTR_EMAILS].append({ EMAIL_ATTR_FROM: mail.from_, EMAIL_ATTR_SUBJECT: mail.subject, }) except Exception as err: _LOGGER.error(f'mailparser parse_from_bytes error: {err}') except Exception as err: _LOGGER.error(f'IMAPClient update error: {err}') self._attr[ATTR_COUNT] = len(emails) try: self._attr[ATTR_TRACKING_NUMBERS][ATTR_UPS] = parse_ups(emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_FEDEX] = parse_fedex(emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_USPS] = parse_usps(emails) self._attr[ATTR_TRACKING_NUMBERS][ ATTR_ALI_EXPRESS] = parse_ali_express(emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_NEWEGG] = parse_newegg( emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_ROCKAUTO] = parse_rockauto( emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_BH_PHOTO] = parse_bh_photo( emails) self._attr[ATTR_TRACKING_NUMBERS][ATTR_PAYPAL] = parse_paypal( emails) except Exception as err: _LOGGER.error(f'Parsers error: {err}')
def test_parse_from_bytes(self): with open(mail_test_2, "rb") as f: mail_bytes = f.read() mail = mailparser.parse_from_bytes(mail_bytes) trust = "smtp.customers.net" self.assertEqual(False, mail.has_defects) raw = "217.76.210.112" result = mail.get_server_ipaddress(trust) self.assertEqual(raw, result) raw = "<*****@*****.**>" result = mail.message_id self.assertEqual(raw, result) raw = "*****@*****.**" result = mail.to self.assertEqual(len(result), 2) self.assertIsInstance(result, list) self.assertIsInstance(result[0], tuple) self.assertIsInstance(mail.to_json, six.text_type) self.assertIsInstance(mail.to_raw, six.text_type) self.assertEqual(raw, result[0][1]) raw = "*****@*****.**" result = mail.from_ self.assertEqual(raw, result[0][1]) raw = "Bollettino Meteorologico del 29/11/2015" result = mail.subject self.assertEqual(raw, result) result = mail.has_defects self.assertEqual(False, result) result = len(mail.attachments) self.assertEqual(3, result) # raw = "Sun, 29 Nov 2015 09:45:18 +0100" self.assertIsInstance(mail.date_raw, six.text_type) self.assertIsInstance(mail.date_json, six.text_type) raw_utc = datetime.datetime(2015, 11, 29, 8, 45, 18, 0).isoformat() result = mail.date.isoformat() self.assertEqual(raw_utc, result)
def mutate_and_send(mail): mail_to = mail.to[0][1] mail_from = mail.from_[0][1] # mail_to = '*****@*****.**' # mail_from = '*****@*****.**' search_criteria = '(TO "' + mail_to + '")' mailbox = '"[Gmail]/Sent Mail"' # search_criteria = '(FROM "' + mail_from + '")' # mailbox = 'inbox' last_email = fm.fetch_mail(mail_from, 'emailmutation', search_criteria, mailbox) if last_email is None: excp_str = "Email not found using search criteria: {} in mailbox: {}".format( search_criteria, mailbox) raise FileNotFoundError(excp_str) last_mail_object = mailparser.parse_from_bytes(last_email) # print(mail_to) # print(mail_from) # last_mail_body = last_mail_object.body.split('--- mail_boundary ---')[0] last_mail_body = last_mail_object.body # print(last_mail_body) m_id = do_mutate(last_mail_body) splitted_from = mail_from.split('@') before_at = splitted_from[0] after_at = splitted_from[1] mutated_from = before_at + '.' + str(m_id) + '@' + after_at # print(mutated_from) msg = MIMEText(mail.body) msg['Subject'] = mail.subject msg['From'] = mutated_from msg['To'] = mail_to send_email.send_(mutated_from, 'emailmutation', [mail_to], msg.as_string())
def get_contents(self): files = [self] if self.real_type.is_archive and 'Encryption' not in self.meta: contents = zipfile.ZipFile(BytesIO(self.get_data())) for zipped_file in contents.filelist: if zipped_file.file_size: file_name = os.path.basename(zipped_file.filename) new_files = __main__.scan_file(contents.read(zipped_file), file_name, self.file_name) files.extend(new_files) if self.real_type.is_email: contents = mailparser.parse_from_bytes(self.get_data()) for attached_file in contents.attachments: decrypted_file = base64.b64decode(attached_file['payload']) new_files = __main__.scan_file(decrypted_file, attached_file['filename'], self.file_name) files.extend(new_files) return files
def list_content_and_parse(id): id = id conn = get_client() final_list = [] received_mails_folder = [] try: check = conn.list_objects(Bucket=bucket, Prefix=id) == True if check == False: conn.put_object(Bucket=bucket, Key=id + '/received/') conn.put_object(Bucket=bucket, Key=id + '/sent/') except: check pass for ie in conn.list_objects(Bucket=bucket, Prefix=id + '/received/')['Contents']: if ie['Key'][-1] != '/': received_mails_folder.append(ie['Key']) # elif ie['Key'][-1] != '/': # sent_mails_folder.append(ie['Key']) else: pass for list_values in received_mails_folder: user_content = {} data = conn.get_object(Bucket=bucket, Key=list_values) read_content = data['Body'].read() mail = mailparser.parse_from_bytes(read_content) receiver_name, receiver_mail = [x for x in mail.to][0] sender_name, sender_mail = [x for x in mail.from_][0] subject = mail.subject content = list(mail.text_plain) body = mail.body date = mail.date user_content['TO'] = receiver_mail user_content['FROM'] = sender_mail user_content['SUBJECT'] = subject user_content['MESSAGE'] = content user_content['NAME'] = sender_name user_content['DATE'] = str(date) final_list.append(user_content) return final_list
def serialize_mail(raw_mail, compress_eml=False): mail = mailparser.parse_from_bytes(raw_mail) body = { 'headers': { 'subject': mail.subject, 'to': [x[1] for x in mail.to] if mail.to else [], 'to+': get_to_plus(mail), 'from': [x[1] for x in mail._from] if mail.from_ else [], 'date': mail.date.isoformat() if mail.date else [], 'cc': [x[1] for x in mail.cc] if mail.cc else [], 'message_id': mail.message_id, 'auto_reply_type': get_auto_reply_type(mail) }, 'text': get_text(mail), 'eml': get_eml(raw_mail, compress_eml), 'files_count': len(mail.attachments), 'files': get_attachments(mail) } return body
def delete_message(request): s3 = get_client() user = request.POST.get('receiversEmail') sender = request.POST.get('sendersEmail') msg_date = request.POST.get('msgDate') received_mail_folder = [] for ie in s3.list_objects(Bucket=bucket, Prefix=user + '/received/')['Contents']: if ie['Key'][-1] != '/': received_mail_folder.append(ie['Key']) for file_to_delete in received_mail_folder: data = s3.get_object(Bucket=bucket, Key=file_to_delete) read_content = data['Body'].read() mail = mailparser.parse_from_bytes(read_content) sender_name, sender_mail = [x for x in mail.from_][0] dateEmail = mail.date if str(dateEmail) == str(msg_date) and sender == sender_mail: s3.delete_object(Bucket=bucket, Key=file_to_delete) return JsonResponse({'success': 'Message deleted successfully'})
def process_mailbox(M, sender): mails_dict = {} rv, data = M.search(None, '(FROM "' + sender + '")') if rv != 'OK': print("No messages found!") return for num in data[0].split(): rv, data = M.fetch(num, '(RFC822)') if rv != 'OK': print("ERROR getting message", num) return mail = mailparser.parse_from_bytes(data[0][1]) mail_id = mail.message_id.split('@')[0][1:] if mail_id in get_db(): pass else: print("Writing message ", mail_id) mails_dict.update(convert_mail_to_dict(mail)) return mails_dict