def _unpack_batch_response(response): """Convert requests.Response -> [(headers, payload)]. Creates a generator of tuples of emulating the responses to :meth:`requests.Session.request`. :type response: :class:`requests.Response` :param response: HTTP response / headers from a request. """ parser = Parser() message = _generate_faux_mime_message(parser, response) if not isinstance(message._payload, list): raise ValueError("Bad response: not multi-part") for subrequest in message._payload: status_line, rest = subrequest._payload.split("\n", 1) _, status, _ = status_line.split(" ", 2) sub_message = parser.parsestr(rest) payload = sub_message._payload msg_headers = dict(sub_message._headers) content_id = msg_headers.get("Content-ID") subresponse = requests.Response() subresponse.request = requests.Request( method="BATCH", url="contentid://{}".format(content_id) ).prepare() subresponse.status_code = int(status) subresponse.headers.update(msg_headers) subresponse._content = payload.encode("utf-8") yield subresponse
def testCommentMailing(self): """ Make sure we're mailing comments """ mailhost = self.portal.MailHost self.assertEqual(len(mailhost.messages), 0) # try to notify discussion_notify(self.tutorial.page1) # there is no sendto address, so we expect no outgoing mail self.assertEqual(len(mailhost.messages), 0) # set an owner email address and try again owner = self.tutorial.page1.Creator() member = self.portal.portal_membership.getMemberById(owner) member.setMemberProperties({'fullname': 'fullname', 'email': '*****@*****.**', }) discussion_notify(self.tutorial.page1) self.assertEqual(len(mailhost.messages), 1) if PLONE4: msg_text = mailhost.messages[0] parser = Parser() msg = parser.parsestr(msg_text) msgTo = msg['To'] msgSubject = msg['subject'] payload = msg.get_payload() else: msg = mailhost.messages[0] msgTo = msg.mto[0] msgSubject = msg.message['subject'] payload = msg.message.get_payload().decode('base64') self.failUnlessEqual(msgTo, '*****@*****.**') self.failUnlessEqual(msgSubject, '=?utf-8?q?New_comment_on_page1?=') self.failUnless(payload.find('Someone added a comment on your HelpCenterLeafPage:\npage1.') > 0)
def parse_email(self, fname): '''Used for get important parts of the mail.''' parser = Parser() email = parser.parsestr(self.file_as_lower_string(fname)) sender = self.get_sender(email) subject = self.get_subject(email) return (sender, subject)
def process_command(group, email, request): '''Process a command in an email message :param obj group: The group that recieved the email message. :param email: The email message that was recieved (which may or may not contain a command). :type email: str or :class:`email.message.Message` :param obj request: The current browser request object. :returns: If a command was processed, and if email processing should continue. :rtype: :class:`.CommandResult` When an email is recieved it needs to checked to see if its ``Subject`` header is command, and the command executed if necessary. The :func:`.process_command` function performs both of these tasks. The result will be either * :attr:`.CommandResut.notACommand` if the email is a normal message, * :attr:`.CommandResut.commandStop` if the email contained a command and processing should stop, or * :attr:`.CommandResut.commandContinue` if the email contained a command and processing should continue. ''' if isinstance(email, Message): e = email elif isinstance(email, STRING): p = Parser() e = p.parsestr(email) else: m = 'email must be a string or a email.message.Message' raise TypeError(m) emailProcessor = ProcessEmailCommand(group, e, request) retval = emailProcessor.process() return retval
def ac(content): lines = re.split("\r\n|\r|\n",content) lines = list(filter(lambda s: s and s.strip(), lines)) lines = [bytes(x, encoding = "utf8") for x in lines] msg_content = b'\r\n'.join(lines).decode(_CHARSET) #print(msg_content) # 稍后解析出邮件:_CHARSET = 'utf-8' msg = Parser().parsestr(msg_content) __em = email_entity.email_enty() __ae_obj = alert_entity.alert() __em = dm.parse(msg,__em) print(type(__em)) email_time_str = None print("----------------1--------------------") if not __em == None: for e in __em.contents: print("----------------2--------------------") __ae_obj = dm.dismantle(e) if __ae_obj: print("----------------3--------------------") email_time_str = __ae_obj.event_time print("----------------4--------------------") if email_time_str == '' or email_time_str == None: email_time_str = dm.parse_x(msg.__str__()) print("----------------5--------------------") print(email_time_str) return email_time_str
def _process_single_node(self, node): """ Extract the contents of a single XML dump node :param node: The XML node corresponding to a message :return: An EmailMessage instance containing the message contents """ text = unicode(node.find('text').text) text = unicode.lstrip(text, u'>') # remove leading char that got into the text somehow if use_full_parser(text): text = fix_broken_hotmail_headers(text) parser = Parser() mime_message = parser.parse(StringIO(text)) return_message = get_nested_payload(mime_message) else: return_message = EmailMessage() subject_node = node.find('subject') from_node = node.find('from') to_node = node.find('to') date_node = node.find('receivedat') subject = unicode(subject_node.text, 'utf-8') if not subject_node is None else '' sender = clean_sender('{} <{}>'.format(from_node.find('name').text, from_node.find('email').text)) recipient = clean_recipient('{} <{}>'.format(to_node.find('name').text, to_node.find('email').text)) date_string = '{} {}'.format(date_node.find('date').text, date_node.find('time').text) return_message.append_body(unicode(text)) return_message.subject = subject return_message.sender = sender return_message.recipient = recipient return_message.date = parse(date_string) return_message.date = normalize_to_utc(return_message.date, self._timezone) return_message.source = "XML File {} node {}".format(self._process_path, node.attrib) return return_message
def parse_mail(f): from email.parser import Parser p = Parser() fo = open(f) msg = p.parse(fo, True) # True for parsing header only fo.close() return msg
def mail (conf) : mail_conf = conf["mail"] parser = Parser() if mail_conf["ssl"] : obj = imaplib.IMAP4_SSL(mail_conf["server"], int(mail_conf["port"])) else : obj = imaplib.IMAP4(mail_conf["server"], int(mail_conf["port"])) obj.login(mail_conf["username"], mail_conf["passwd"]) obj.select(readonly=True) data = obj.search(None, 'UnSeen') header = "" for num in data[1][0].split() : msg = parser.parsestr(obj.fetch(num, '(BODY[HEADER.FIELDS (SUBJECT FROM)])')[1][0][1]) sender_tuple = re.sub(r'<(.+)>', '', msg["from"]) sender = "" for i in decode_header(sender_tuple) : if i[1] is not None : sender += i[0].decode(i[1]) else : sender += i[0] subject_tuple = msg["subject"] subject = "" for j in decode_header(subject_tuple) : if j[1] is not None : subject += j[0].decode(i[1]) else : subject += j[0] header += "Message de " + sender + " : " + subject.replace("RE:", "").replace("Re:", "").replace("Fwd:", "").replace("FWD:", "").replace("Tr:", "").replace("TR:", "") + ". " return len(data[1][0].split()), header
def modify_headers(email, group, request): '''Modifiy the headers in an email message prior to sending :param email: The email message to modify. :type emaill: :class:`email.message.Message` or ``str`` :param group: The group that is sending the email. :type group: :class:`gs.group.base.interfaces.IGSGroupMarker` :param request: The HTTP request that causes the email to be sent. :type request: :class:`zope.publisher.interfaces.browser.IDefaultBrowserLayer` :returns: The modified email message. :rtype: :class:`email.message.Message` or ``str``, depending on what was provided.''' if isinstance(email, Message): e = email fromString = False elif isinstance(email, STRING): p = Parser() e = p.parsestr(email) fromString = True else: m = 'email must be a string or a email.message.Message' raise TypeError(m) hm = HeaderModifier(group, request) modifiedEmail = hm.modify_headers(e) retval = modifiedEmail.as_string() if fromString else modifiedEmail return retval
def decode(cls, content, content_type): if not isinstance(content, bytes): raise TypeError("content should be an instance of bytes") type_helper = EmailParser().parsestr('Content-Type: {}'.format(content_type)) content_type = type_helper.get_content_type() charset = type_helper.get_content_charset() return cls(content, content_type, charset)
def get_email(self, email_id, mailbox=None): if mailbox and mailbox != self.selected_mailbox: self._select_mailbox(mailbox) if not self.selected_mailbox: raise ValueError('No Mailbox selected') self.log('fetch %s' % email_id) ret, data = self.connection.fetch(email_id, DEFAULT_FETCH_ALL) self.log(ret) data = [d for d in data if isinstance(d, tuple)][0] parser = Parser() parsed_email = parser.parsestr(data[1]) body_text = '' for part in parsed_email.walk(): if part.get_content_type() == 'text/plain': body_text += self.__decode_header(part.get_payload(decode=True)) email = { 'id': email_id, 'mailbox': mailbox, 'subject': self.__decode_header(parsed_email.get('Subject')), 'from': self.__decode_header(parsed_email.get('From')), 'to': self.__decode_header(parsed_email.get('To')), 'date': self.__decode_header(parsed_email.get('Date')), 'body_text': body_text, } return email
def preprocessEmailData(self): ''' this method is used as a driver that calls different methods for processing data :return: void ''' parser = Parser() # Added for extracting only the body of the email tokens = self.Ngram.loadCorpus() email = parser.parsestr(tokens) # Added for extracting only the body of the email email_body_list = [email.split('Body:')[-1] for email in tokens.split('##########################################################')] # Added for extracting only the body of the email tokendata=[] for txt in email_body_list: tokendata.append(nltk.wordpunct_tokenize(txt)) merged = list(itertools.chain.from_iterable(tokendata)) # flattening the tokendata which is a list of list. preprocessedText = self.Ngram.preprocessData(merged) stemmedwords = self.Ngram.stemWords(preprocessedText) self.Ngram.writeToFile(stemmedwords,'preprocessedEmailBody') posTaggedList = self.Ngram.createPOSTagging(stemmedwords) preprocessedListwithoutNouns = self.Ngram.removeProperNouns(posTaggedList) print 'removed proper nouns' print datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') bigramslist = self.Ngram.createBigrams(preprocessedListwithoutNouns) print 'bigrams created successfully' print datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') trigramslist = self.Ngram.createTrigrams(preprocessedListwithoutNouns) print 'trigrams created successfully' self.Ngram.writeToFile(bigramslist,'emailbigramList') self.Ngram.writeToFile(trigramslist,'emailtrigramList') print datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
def fetch_mail(server, user, passwd, what_to_fetch): """fetch task mail from server :param SERVER: server address :param USER: mail account user :param PASSWD: mail account password :return tasks: list of tasks """ tasks = [] mail = imaplib.IMAP4_SSL(server) mail.login(user, passwd) mail.list() mail.select("inbox") result, data = mail.search(None, what_to_fetch) if result == 'OK': id_list = data[0].split() for mail_id in id_list: result, data = mail.fetch(mail_id, "(RFC822)") email_ = Parser().parsestr((data[0][1].decode(encoding='UTF-8'))) tasks.append(email_.get_payload().rstrip()) if DELETE_FLAG: mail.store(mail_id, '+FLAGS', '\\DELETED') else: print("error: " + result) sys.exit(1) return tasks, mail
def loadCorpus(self): ''' loads the corpus, reads from all files and writes to a single large file result.txt in working directory. :return:a list of tokens for the entire corpus ''' corpus_root = self.path rawcontent = PlaintextCorpusReader(corpus_root, ".*") reload(sys) #sys.setdefaultencoding('utf-8') read_files = rawcontent._fileids print 'files to be read ' + str(read_files) with open("result.txt", "wb") as outfile: for f in read_files: if(f != '.DS_Store'): with open(os.path.join(self.path + '/' + f), "rb") as infile: outfile.write(infile.read()) raw = open("result.txt").read() parser = Parser() email = parser.parsestr(raw) email_body_dict = {} i = 0 for email in raw.split('##########################################################'): i += 1 email_body_list.append(email.split('Body:')[-1]) email_body_dict[i] = email.split('Body:')[-1] self.save_obj(email_body_dict,'emailBodyDict') return raw
def read_exchange(self): url = config_file.get("config_exchange", "url_exchange") port = int(config_file.get("config_exchange", "port_exchange")) user = config_file.get("config_exchange", "user_exchange") password = config_file.get("config_exchange", "pass_exchange") conn = imaplib.IMAP4_SSL(url, port) conn.login(user, password) conn.select('INBOX') results, data = conn.search(None,'(UNSEEN)') for index in range(len(data)): msg_ids = data[index] msg_id_list = msg_ids.split() try: latest_email_id = msg_id_list[-1] result,data = conn.fetch(latest_email_id,"(RFC822)") raw_email = data[index][1] p = Parser() msg = p.parsestr(raw_email) print msg.get('From') print msg.get('Subject') from_date = msg.get('Date') say = "New Email\n>From: %s\n>Date: %s\n>Subject: %s\n>\n>" % \ (msg.get('From'), from_date, msg.get('Subject')) self.slack.direct_message(say, self.slack_user_id, self.slack_app) except IndexError: print "No mensajes recientes" return
def featurize(email): #print email fp = open(email) fp.readline() # discard first garbage line msg = Parser().parse(fp) features = [] # Results are better if we *don't* use the headers # for header in msg.keys(): # for (v, cs) in decode_header(msg[header]): # try: # v = v.decode(cs or 'utf-8') # except UnicodeDecodeError: # v = v.decode('iso-8859-1') # for vp in v.split(): # features.append(header + ':' + vp) for part in msg.walk(): if part.is_multipart(): continue try: cs = msg.get_charset() or 'utf-8' data = part.get_payload(decode = True).decode(cs) except UnicodeDecodeError: data = part.get_payload(decode = True).decode('iso-8859-1') for token in data.split(): if len(token) < 1000: features.append(token) return features
def receive_message(self): """Reads the raw message from stdin This function first reads the raw message from stdin and parses into an email.message.Message object. It also writes out the raw data to a file. """ lines = [] for line in fileinput.input(): lines.append(line) raw_email = ''.join(lines) parser = Parser() self.__message = parser.parsestr(raw_email) # Write out the raw email epoch = email.utils.mktime_tz( email.utils.parsedate_tz(self.__message['Date'])) addr_from = self.parse_address_field('From') if 'In-Reply-To' in self.__message: in_reply_to = self.parse_address_field( 'In-Reply-To').replace('/', '_') epoch = email.utils.mktime_tz( email.utils.parsedate_tz(self.__message['Date'])) addr_from = self.parse_address_field('From') self.__msg_file = '%s-%s-%s' % (in_reply_to, addr_from, epoch) else: self.__msg_file = '%s-%s' % (addr_from, epoch) filepath = os.path.join(BASE_DIR, 'incoming', self.__msg_file) logger.info("Writing raw message to %s", filepath) with open(filepath, 'w+') as outfile: outfile.write(raw_email)
def get_message(self, msguid): # input mbox = IMAP stream, mid = message id # output all the following: # the message may in htmlmsg, plainmsg, or both data = self.server.fetch([msguid], ['RFC822']) msg_data = data[int(msguid)] message = MessageParser().parsestr(msg_data['RFC822']) msg = {} msg['headers'] = self._parse_msg_headers(msguid, message) msg['uid'] = msguid msg['parts'] = parts = [] default_charset = 'utf-8' for p in message.walk(): if p.is_multipart(): default_charset = p.get_content_charset() or default_charset continue part = {} charset = p.get_content_charset() or default_charset type = p.get_content_maintype() part['type'] = type part['subtype'] = p.get_content_subtype() if type == 'text': part['charset'] = charset part['body'] = unicode(p.get_payload(decode=True), charset) parts.append(part) return msg
def bring_me_enron(path): """ Scans all emails from the Enron Email Dataset, a freely available corpus of c515K emails. Used successfully with the May 7, 2015 version of the dataset, available from: https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz Based on code originally by Bryan Nehl <http://soloso.blogspot.com/2011/07/getting-enron-mail-database-into.html> and the author of <http://mongodb-enron-email.s3-website-us-east-1.amazonaws.com/> Yields (message-id, subject, date) header strings """ p = Parser() for root, dirs, files in os.walk(path, topdown=False): # distinct file name for filename in files: nameOfFileToOpen = "{0}/{1}".format(root, filename) dataFile = open(nameOfFileToOpen) raw_contents = "" try: for dataLine in dataFile: raw_contents += dataLine finally: dataFile.close() contents = raw_contents.decode('cp1252') msg = p.parsestr(contents.encode("utf-8")) yield (msg['message-id'], msg['subject'], msg['date'])
def email_parse(self, content): p = Parser() msgobj = p.parsestr(content) if msgobj['Subject'] is not None: decodefrag = decode_header(msgobj['Subject']) subj_fragments = [] for s , enc in decodefrag: if enc: s = unicode(s , enc).encode('utf8','replace') subj_fragments.append(s) subject = ''.join(subj_fragments) else: subject = None attachments = [] body_text = "" body_html = "" for part in msgobj.walk(): attachment = self.email_parse_attachment(part) if attachment: attachments.append(attachment) elif part.get_content_type() == "text/plain": body_text += unicode(part.get_payload(decode=True),part.get_content_charset(),'replace').encode('utf8','replace') elif part.get_content_type() == "text/html": body_html += unicode(part.get_payload(decode=True),part.get_content_charset(),'replace').encode('utf8','replace') return { 'subject': subject, 'body_text': body_text, 'body_html': body_html, 'from': parseaddr(msgobj.get('From'))[1], 'to': parseaddr(msgobj.get('To'))[1], 'attachments': attachments }
def _unpack_batch_response(response, content): """Convert response, content -> [(headers, payload)]. Creates a generator of tuples of emulating the responses to :meth:`httplib2.Http.request` (a pair of headers and payload). :type response: :class:`httplib2.Response` :param response: HTTP response / headers from a request. :type content: str :param content: Response payload with a batch response. """ parser = Parser() message = _generate_faux_mime_message(parser, response, content) if not isinstance(message._payload, list): raise ValueError('Bad response: not multi-part') for subrequest in message._payload: status_line, rest = subrequest._payload.split('\n', 1) _, status, _ = status_line.split(' ', 2) sub_message = parser.parsestr(rest) payload = sub_message._payload ctype = sub_message['Content-Type'] msg_headers = dict(sub_message._headers) msg_headers['status'] = status headers = httplib2.Response(msg_headers) if ctype and ctype.startswith('application/json'): payload = json.loads(payload) yield headers, payload
def process_message(self, peer, mailfrom, rcpttos, data): print 'Receiving message from:', peer print 'Message addressed from:', mailfrom print 'Message addressed to :', rcpttos print 'Message length :', len(data) loot_directory = helpers.ea_path() + '/data' p = Parser() msgobj = p.parsestr(data) for part in msgobj.walk(): attachment = self.email_parse_attachment(part) if type(attachment) is dict and 'filedata' in attachment: decoded_file_data = base64.b64decode(attachment['filedata']) attach_file_name = attachment['filename'] with open(loot_directory + "/" + attach_file_name, 'wb') as attached_file: attached_file.write(decoded_file_data) else: current_date = time.strftime("%m/%d/%Y") current_time = time.strftime("%H:%M:%S") file_name = current_date.replace("/", "") +\ "_" + current_time.replace(":", "") + "email_data.txt" with open(loot_directory + "/" + file_name, 'a') as email_file: email_file.write(data) return
def _parseMessage(self, fp): """ Extract fromaddr and toaddrs from the X-Actually-{To,From} headers. Returns message string which has those headers stripped. """ parser = Parser() message = parser.parse(fp) fromaddr = message['X-Actually-From'] if fromaddr is not None: decoded_fromaddr = header.decode_header(fromaddr) assert len(decoded_fromaddr) == 1, 'From header has multiple parts.' encoded_fromaddr, charset = decoded_fromaddr[0] if charset is not None: fromaddr = encoded_fromaddr.decode(charset) else: fromaddr = '' del message['X-Actually-From'] toaddrs = message['X-Actually-To'] if toaddrs is not None: decoded_toaddrs = header.decode_header(toaddrs) assert len(decoded_toaddrs) == 1, 'To header has multiple parts.' encoded_toaddrs, charset = decoded_toaddrs[0] if charset is not None: toaddrs = encoded_toaddrs.decode(charset) toaddrs = tuple(a.strip() for a in toaddrs.split(',')) else: toaddrs = () del message['X-Actually-To'] return fromaddr, toaddrs, message
class ErrorEmailParser(object): def __init__(self, verbose): self.verbose = verbose def parse_email(self, msg_file): self.parsed_email = Parser().parse(msg_file) email_parts = [p for p in self.parsed_email.walk()] if len(email_parts) > 1: raise Exception("unexpected parts to email: %s" % msg_file.name) self.body = self.parsed_email.get_payload(decode=True) traceback, request = self.body.split("\n\n\n", 1) self.parse_traceback(traceback) self.parse_request(request) def parse_traceback(self, traceback): tb_lines = traceback.split("\n") self.one_line_error = tb_lines[-1] self.one_word_error = self.one_line_error.split(':', 1)[0] matches = file_line_re.match(tb_lines[-4]) # TODO: nice way to report the error if matches: self.tb_final_path = matches.group("path") self.tb_final_line_no = matches.group("line_no") self.tb_final_function = matches.group("function") def parse_request(self, request): request_lines = request.split("\n") self.extract_meta(request_lines) def extract_meta(self, request_lines): meta_start = find_line_matching(request_lines, r'^META') meta_end = find_line_matching(request_lines, r'\}>$') + 1 meta_lines = [l for l in request_lines[meta_start:meta_end] if not l.endswith(">,")] meta_text = "\n".join(meta_lines)[5:-1] try: self.meta = ast.literal_eval(meta_text) except SyntaxError: # TODO: better error handling/reporting self.meta = {} if self.verbose: click.echo("could not parse:\n%s" % meta_text, err=True) #raise def assemble_output(self, max_len, one_word, server_name, path, query): bits = [] if server_name: bits.append(self.meta.get("SERVER_NAME", "unknown")) if path: bits.append(self.meta.get("PATH_INFO", "unknown")) if query: bits.append(self.meta.get("QUERY_STRING", "unknown")) if one_word: bits.append(self.one_word_error) else: bits.append(self.one_line_error) out = " ".join(bits) if max_len > 0: out = out[:max_len] return out
def _fetch_email_from_s3_and_parse(s3_obj): logging.info('Parsing email message for %s', s3_obj.key) email_msg_str = s3_obj.get_contents_as_string() msg = Parser().parsestr(email_msg_str) subparts = [p for p in msg.walk()] for i, subpart in enumerate(subparts): logging.info('Subpart %d - %s', i, subpart.get_content_type()) if subpart.get_content_type() == 'text/plain': text_payload = subpart.get_payload() assert text_payload is not None merchant_raw, merchant_parsed = _parse_for_merchant(text_payload) amount_raw, amount_parsed = _parse_for_amount(text_payload) date_raw, date_parsed = _parse_for_date(text_payload) logging.info('Raw value -> Parsed value') logging.info('"%s" -> "%s"', merchant_raw, merchant_parsed) logging.info('"%s" -> "%s"', amount_raw, amount_parsed) logging.info('"%s" -> "%s"', date_raw, date_parsed) return {'merchant_parsed': merchant_parsed, 'amount_parsed': amount_parsed, 'date_parsed': date_parsed}
def setUp(self): context = MagicMock() request = MagicMock() self.notifier = Notifier(context, request) parser = Parser() self.msg = parser.parsestr(self.m)
def prepare_msg(msg_str): headers = Parser().parsestr(msg_str) payload = headers.get_payload() msg_subject = headers['subject'] if not headers['subject']: alarm_info = json.loads(payload)['body'] subject = msg_subject + alarm_info['alarm_name'] template = generate_subbody(mail_alarm_info, reason=alarm_info['reason'], severity=alarm_info['severity'], alarm_name=alarm_info['alarm_name'], alarm_id=alarm_info['alarm_id']) else: subject = msg_subject template = generate_subbody(mail_confirm_link, confirm_link=get_confirm_link(payload)) session = get_admin_session() corp_info = get_corp_info(session) msg = generate_msg( template, headers['to'], corp_info['from'], subject, logo_url=corp_info['logo_url'], corp_name=corp_info['corp_name'], home_link=corp_info['home_link']) return msg
def read_emails(self): email_list = list() for i in range(self.email_cnt): resp, lines, octets = self.server.retr(i + 1) email = b'\n'.join(lines).decode('utf-8') email = Parser().parsestr(email) data = { 'index': i + 1, 'size': octets, 'from_name': '', 'from_addr': '', 'subject': '', 'plain': '', 'html': '', 'attachment': list() } # parse email sender from_info = email.get('From', '') from_info, addr = parseaddr(from_info) name = self.__parse_basic(from_info) data.update(from_name=name) data.update(from_addr=addr) # parse subject subject_info = email.get('Subject', '') subject = self.__parse_basic(subject_info) data.update(subject=subject) # parse content self.__parse_content(email, data) email_list.append(data) return email_list
def process_message(self, peer, mailfrom, rcpttos, data): print """ /////////////////////////////////////////////////////// ======================================================= """ print 'Received at', datetime.datetime.now() print 'Receiving message from:', peer print 'Message addressed from:', mailfrom print 'Message addressed to :', rcpttos print 'Message length :', len(data) msg = Parser().parsestr(data) for part in msg.walk(): if part.get_content_type() in ['text/html']: filename = '/tmp/message.%s.html' % datetime.datetime.now() temp = open(filename, 'w+b') try: temp.write(base64.b64decode(part.get_payload())) webbrowser.open_new_tab(temp.name) finally: # Automatically cleans up the file temp.close() if part.get_content_type() in ['text/plain', 'text/html']: print """ ******************************************************* ======================================================= """ print base64.b64decode(part.get_payload()) print """
class SMTPSerialiser(object): """ Utility class to proxy SMTP data from mock to boss. It is used to serialise SMTP data as JSON data. """ def __init__(self, **kwargs): self.data = kwargs email = ensure_is_python_string(self.data['data']) self.message = Parser().parsestr(email) def serialize(self): return json.dumps(self.data) def __getitem__(self, key): return self.message.__getitem__(key) @property def content(self): payload = self.message.get_payload(decode=True) return ensure_is_python_string(payload) @property def subject(self): subj = email_lib.header.decode_header(self.message['Subject'])[0][0] return ensure_is_python_string(subj)
def prepare_message(email, recipient, recipients_list): message = email.message if not message: return "" if email.add_unsubscribe_link and email.reference_doctype: # is missing the check for unsubscribe message but will not add as there will be no unsubscribe url unsubscribe_url = get_unsubcribed_url(email.reference_doctype, email.reference_name, recipient, email.unsubscribe_method, email.unsubscribe_params) message = message.replace("<!--unsubscribe url-->", quopri.encodestring(unsubscribe_url)) if email.expose_recipients == "header": pass else: if email.expose_recipients == "footer": if isinstance(email.show_as_cc, basestring): email.show_as_cc = email.show_as_cc.split(",") email_sent_to = [r.recipient for r in recipients_list] email_sent_cc = ", ".join( [e for e in email_sent_to if e in email.show_as_cc]) email_sent_to = ", ".join( [e for e in email_sent_to if e not in email.show_as_cc]) if email_sent_cc: email_sent_message = _( "This email was sent to {0} and copied to {1}").format( email_sent_to, email_sent_cc) else: email_sent_message = _("This email was sent to {0}").format( email_sent_to) message = message.replace("<!--cc message-->", quopri.encodestring(email_sent_message)) message = message.replace("<!--recipient-->", recipient) message = (message and message.encode('utf8')) or '' if not email.attachments: return message # On-demand attachments from email.parser import Parser msg_obj = Parser().parsestr(message) attachments = json.loads(email.attachments) for attachment in attachments: if attachment.get('fcontent'): continue fid = attachment.get('fid') if not fid: continue fname, fcontent = get_file(fid) attachment.update({ 'fname': fname, 'fcontent': fcontent, 'parent': msg_obj }) attachment.pop("fid", None) add_attachment(**attachment) return msg_obj.as_string()
def header(self): """ Returns the message header, if it exists. Otherwise it will generate one. """ try: return self._header except AttributeError: headerText = self._getStringStream('__substg1.0_007D') if headerText is not None: self._header = EmailParser().parsestr(headerText) self._header['date'] = self.date else: logger.info( 'Header is empty or was not found. Header will be generated from other streams.' ) header = EmailParser().parsestr('') header.add_header('Date', self.date) header.add_header('From', self.sender) header.add_header('To', self.to) header.add_header('Cc', self.cc) header.add_header('Message-Id', self.message_id) # TODO find authentication results outside of header header.add_header('Authenitcation-Results', None) self._header = header return self._header
print_info(part, depth + 1) else: content_type = msg.get_content_type() if content_type == 'text/plain' or content_type == 'text/html': content = msg.get_payload(decode=True) charset = guess_charset(msg) if charset: content = content.decode(charset) print('%sText: %s' % (' ' * depth, content + '...')) else: print('%sAttachment: %s' % (' ' * depth, content_type)) email = '*****@*****.**' passwd = 'aiying1007' pop3_addr = 'pop3.163.com' pop3 = poplib.POP3(pop3_addr) pop3.set_debuglevel(1) print(pop3.getwelcome().decode('utf-8')) pop3.user(email) pop3.pass_(passwd) print('Message %s; Size %s' % pop3.stat()) resp, mails, octetcs = pop3.list() print(mails, "%%%%%%%%%%%%%%%") index = len(mails) resp, lines, octetcs = pop3.retr(index) content = b'\r\n'.join(lines).decode('utf-8') msg = Parser().parsestr(content) print_info(msg) pop3.quit()
server.pass_(password) # stat()返回郵件數量和佔用空間: print('Messages: %s. Size: %s' % server.stat()) # list()返回所有郵件的編號: resp, mails, octets = server.list() # 可以查看返回的列表類似['1 82923', '2 2184', ...] print mails # 獲取最新一封郵件, 注意索引號從1開始: index = len(mails) resp, lines, octets = server.retr(index) # retr()把每一封郵件內容拿到 # lines存儲了郵件的原始文本的每一行, # 可以獲得整個郵件的原始文本: msg_content = '\r\n'.join(lines) # 稍後解析出郵件: msg = Parser().parsestr(msg_content) # 可以根據郵件索引號直接從服務器刪除郵件: # server.dele(index) # 關閉連接: server.quit() # part 2 parser import email from email.parser import Parser from email.header import decode_header from email.utils import parseaddr msg = Parser().parsetr(msg_content) # indent用於縮進顯示: def print_info(msg, indent=0):