def get_email_text(filename): # Import the email modules we'll need from email.parser import Parser import email parser = Parser() file = open(filename, 'r', encoding='ISO-8859-1') emailText = file.read() email = parser.parsestr(emailText) # Getting the fields of email. Commented out. #print(email.get('From')) #print(email.get('To')) #print(email.get('Subject')) # Getting the body of the email textlist = [] if email.is_multipart(): for part in email.get_payload(): textlist.extend(email.get_payload()) text = [items.as_string() for items in textlist] email_text = '\n'.join(text) else: email_text = email.get_payload() file.close() return email_text
def format_email(email): ''' Takes an email and formats it as it should be for the terminal ''' template = """From: {sender} To: {to} Date: {date} Subject: {subject} {body}""" if email.is_multipart(): body = email.get_payload()[0].get_payload() else: body = email.get_payload() template = template.format(sender=email['from'], to=email['to'], date=email['date'], subject=email['Subject'], body=body) return template
def getBodyfromEmail(email): body = "" if email.is_multipart(): for payload in email.get_payload()[:-1]: body += payload.get_payload() else: body = email.get_payload() body = body[:-1] # extra newline for some reason return body
def get_email_body(email): # retrieve all parts of the email if email.is_multipart(): for part in email.get_payload(): body = part.get_payload() # more processing? else: body = email.get_payload() return body
def all_payload_text(email): text = '' if isinstance(email.get_payload(), basestring): text = email.get_payload() else: for part in email.get_payload(): if part.get_content_type() == 'text/plain': text += part.get_payload() return text
def from_email(email): output = EMail() output.subject = email["SUBJECT"] output.to_address = email["TO"] output.from_address = email["FROM"] output.content = {} if email.is_multipart(): parts = email.get_payload() for p in parts: output.content[p.get_content_type()] = p.get_payload() else: output.content[email.get_content_type()] = email.get_payload() return output
def decode_body(email): """Return the body of an email.message.Message, decoded, as unicode.""" body = email.get_payload(decode=True) default_charset = email.get_charsets()[0] if default_charset: body = body.decode(default_charset) return body
def get_pin(self, email) -> str: for link in BeautifulSoup(email.get_payload(decode=True).decode(), parse_only=SoupStrainer('a'), features="html.parser"): content = link.contents[0] if content.isnumeric(): return content
def get_email_structure(email): if isinstance(email, str): return email payload = email.get_payload() if isinstance(payload, list): return "multipart({})".format(','.join([get_email_structure(sub_email) for sub_email in payload])) else: return email.get_content_type()
def mailTest(): mail.list() #Gets all emails mail.select("inbox") # Gets inbox (instead of like "sent") result, data = mail.search(None, "ALL") #Gets "all" emails, I think maybe ids = data[0] # data is a list. id_list = ids.split() # ids is a space separated string latest_email_id = id_list[-1] # 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] #print raw_email with open('data.txt', 'w') as outfile: json.dump(raw_email, outfile) #Gets email body email = parser.parsestr(raw_email) bodytext = email.get_payload()[0].get_payload() if type(bodytext) is list: bodytext = ','.join(str(v) for v in bodytext) #print bodytext def getName(): #Creating getName method num = bodytext.index('ID') start = num + 3 stop = start + 18 #Add 15 char to end of ID acctID = bodytext[start:stop] print acctID def checkName(): subject = email.get('Subject') print subject ''' if subject == 'FW: Order Notification' or 'Fw: Order Notification': print 'The script works' getName() else: print "Not the Notification email" print subject print '' print "End of checkName and getName script" ''' checkName()
def get_email_structure(email): if isinstance(email, str): return 'text/plain' payload = email.get_payload() if isinstance(payload, list): return ", ".join([ get_email_structure(sub_email) for sub_email in payload ]) else: return email.get_content_type()
def create_messages_column(emails, df): """ Adds column `message` to df containing the email's content emails: email object df: DataFrame to add column to """ msgs = [] for email in emails: msg = email.get_payload() msg = msg.replace('\n', ' ') msg = msg.replace('\t', ' ') msgs.append(msg) df['message'] = msgs
def get_email_structure(email): if isinstance(email, str): # blank email return email payload = email.get_payload( ) # get the payload of email and return a list. if isinstance(payload, list): return "multipart({})".format( ", ".join([ get_email_structure(sub_email) # regression in email for sub_email in payload ]) ) # string like '{}, {}, {}'.format('a', 'b', 'c') output: 'a, b, c' else: return email.get_content_type()
def mecha(): mail.list() #Gets all emails mail.select("inbox") # Gets inbox (instead of like "sent") result, data = mail.search(None, "ALL") #Gets "all" emails, I think maybe ids = data[0] # data is a list. id_list = ids.split() # ids is a space separated string latest_email_id = id_list[-1] # 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] #print raw_email with open('data.txt', 'w') as outfile: json.dump(raw_email, outfile) global bodytext #Gets email body email = parser.parsestr(raw_email) bodytext=email.get_payload()[0].get_payload() if type(bodytext) is list: bodytext=','.join(str(v) for v in bodytext) def getName(): #Creating getName method num = bodytext.index('ID') start = num + 4 stop = start + 18 #Add 15 char to end of ID global acctID acctID = bodytext[start:stop] acctID = acctID.strip() def checkName(): subject = email.get('Subject') if subject == 'FW: Order Notification' or subject == 'Fw: Order Notification': print 'The script works' getName() else: print "Not the Notification email" print '' print "End of checkName and getName script" checkName() print 'acctID: ' + acctID print acctID report = sf.Report.get('') obj2 = report["factMap"]["T!T"]["rows"] count = len(obj2) counter = 0 while counter < count: global acctID global opptid sfdcID = obj2[counter]["dataCells"][6]["label"] #actually going to use salesforceID instead of opptid print sfdcID opptid = obj2[counter]["dataCells"][1]["label"] print opptid opptpayready = obj2[counter]["dataCells"][2]["label"] mechaclosed = obj2[counter]["dataCells"][3]["label"] print '' if acctID == sfdcID and opptpayready == 'true' and mechaclosed == 'false': global opptidperm global opptid print sfdcID opptidperm = opptid opptpayreadyperm = opptpayready print "The if else loop with payready true worked." today = date.today().isoformat() closedwon = sf.Opportunity.update( opptidperm, {'StageName': 'Closed Won'}) closedwondate = sf.Opportunity.update( opptidperm, {'CloseDate': today}) mechaclosedmark = sf.Opportunity.update( opptidperm, {'mechaclosed__c': 'true'}) print "This is pass %s" % counter counter = counter + 1 #test this section with testing.py and api.py print 'opptidperm: ' + opptidperm print 'acctID: ' + acctID print 'sfdcID: ' + sfdcID
def addDBEntry(connect,cur, tablename, email, filepath): print '**************************' print filepath sender = mdb.escape_string(enron.stripCharacters(email['From'])) to = email['To'] if (to != None): to = re.sub("(E-mail)", "", to) to = re.sub('<', '', to) to = re.sub('>', '', to) else: to = 'unknown' to = enron.stripCharacters(to) to = mdb.escape_string(to) cc = email['X-cc'] if (cc != None): cc = re.sub('(E-mail)', '', cc) cc = re.sub('<', '', cc) cc = re.sub('>', '', cc) else: cc = '' cc = enron.stripCharacters(cc) cc = mdb.escape_string(cc) bcc=email['X-bcc'] if (bcc != None): bcc = re.sub('(E-mail)', '', bcc) bcc = re.sub('<', '', bcc) bcc = re.sub('>', '', bcc) else: bcc = '' bcc = enron.stripCharacters(bcc) bcc = mdb.escape_string(bcc) subject=enron.stripCharacters(email['Subject']) subject = mdb.escape_string(subject) date = email['Date'] formated_date = formatDate(date) localfile = filepath #keep all the raw text formatting rawtext = enron.stripCharacters(email.get_payload(),backslash_char = False) cleantext = enron.cleanString(rawtext) rawtext = mdb.escape_string(rawtext) cleantext = mdb.escape_string(cleantext) #now create the syntax to add an entry to the db query = """INSERT INTO {0} (`sender`, `to`, `date`,`subject`, `cc`, `bcc`, \ `rawtext`, `text`, `fileloc`) VALUES ("{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", \ "{8}","{9}");""".format(tablename, sender, to, formated_date, subject, cc, bcc,\ rawtext,cleantext,filepath) #print query logfile = open('logfile', 'a') try: cur.execute(query) connect.commit() print 'Added file: {0}'.format(filepath) except mdb.Error, err: print err logfile.write("Error {0} File {1}\n".format(err, filepath))
def get_body(email): body = email.get_payload() if isinstance(body, str): return body else: return get_body(body[0])
def assetChange(): #Main class mail.list() #Gets all emails mail.select("inbox") # Gets inbox (instead of like "sent") result, data = mail.search(None, "ALL") #Gets "all" emails, I think maybe ids = data[0] # data is a list. id_list = ids.split() # ids is a space separated string latest_email_id = id_list[-1] # 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] #print raw_email with open('data.txt', 'w') as outfile: json.dump(raw_email, outfile) global bodytext #Gets email body email = parser.parsestr(raw_email) bodytext=email.get_payload()[0].get_payload() if type(bodytext) is list: bodytext=','.join(str(v) for v in bodytext) def getIMEI(): #Gets IMEI from email #Creating getIMEI method num = bodytext.index('ay:') #Line before IMEIs in email #print bodytext start = num + 4 print len(bodytext) - num endOfString = len(bodytext) - num stop = start + endOfString global IMEI global IMEIs IMEI = bodytext[start:stop] IMEI = IMEI.strip() acctIdByLine = IMEI.splitlines() for x in acctIdByLine: #print x if x.startswith('35'): print x[0:15] IMEIs.append(x[0:15]) print IMEIs def checkName(): subject = email.get('Subject') if subject == 'FW: Device Status Change' or subject == 'Fw: Device Status Change': #checks subject of Asset Status Email print 'The script works' getIMEI() else: print "Not the Asset Change email email" print '' checkName() print 'IMEI: ' + IMEI for imei in IMEIs: query = sf.query("SELECT Id FROM Asset WHERE Name = '%s'" % imei ) print "query" #print query['OrderedDict'] jsonquery = json.dumps(query) loads = json.loads(jsonquery) IdRaw = loads['records'][0]['Id'] print IdRaw[0:15] Idtrim = IdRaw[0:15] today = date.today().isoformat() assetStatus = sf.Asset.update(Idtrim, {'Status': 'Active'}) StatusDate = sf.Asset.update(Idtrim, {'InstallDate': today}) print ''
def store_mail(mail): try: for line in mail: mail_parser.feed(line) email = mail_parser.close() redis.incr('mail:id') id = redis.get('mail:id') redis.hmset(id, {'To:':email['To'],'From:':email['From'], 'Date:':email['Date'], 'Message:':email.get_payload()}) except: print 'Unexpected error:', sys.exc_info()[0] exit(1) return 'Message was stored successfully'
def addDBEntry(connect, cur, tablename, email, filepath): print '**************************' print filepath sender = mdb.escape_string(enron.stripCharacters(email['From'])) to = email['To'] if (to != None): to = re.sub("(E-mail)", "", to) to = re.sub('<', '', to) to = re.sub('>', '', to) else: to = 'unknown' to = enron.stripCharacters(to) to = mdb.escape_string(to) cc = email['X-cc'] if (cc != None): cc = re.sub('(E-mail)', '', cc) cc = re.sub('<', '', cc) cc = re.sub('>', '', cc) else: cc = '' cc = enron.stripCharacters(cc) cc = mdb.escape_string(cc) bcc = email['X-bcc'] if (bcc != None): bcc = re.sub('(E-mail)', '', bcc) bcc = re.sub('<', '', bcc) bcc = re.sub('>', '', bcc) else: bcc = '' bcc = enron.stripCharacters(bcc) bcc = mdb.escape_string(bcc) subject = enron.stripCharacters(email['Subject']) subject = mdb.escape_string(subject) date = email['Date'] formated_date = formatDate(date) localfile = filepath #keep all the raw text formatting rawtext = enron.stripCharacters(email.get_payload(), backslash_char=False) cleantext = enron.cleanString(rawtext) rawtext = mdb.escape_string(rawtext) cleantext = mdb.escape_string(cleantext) #now create the syntax to add an entry to the db query = """INSERT INTO {0} (`sender`, `to`, `date`,`subject`, `cc`, `bcc`, \ `rawtext`, `text`, `fileloc`) VALUES ("{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", \ "{8}","{9}");""".format(tablename, sender, to, formated_date, subject, cc, bcc,\ rawtext,cleantext,filepath) #print query logfile = open('logfile', 'a') try: cur.execute(query) connect.commit() print 'Added file: {0}'.format(filepath) except mdb.Error, err: print err logfile.write("Error {0} File {1}\n".format(err, filepath))
sender_realname, sender = parseaddr(email.get('From')) if sender not in contacts.keys(): contacts[sender] = {} contacts[sender]['email'] = sender contacts[sender]['realname'] = sender_realname contacts[sender][ 'gravatar'] = "https://www.gravatar.com/avatar/" + md5( sender.strip().lower()).hexdigest() contacts[sender]['mails'] = [] contacts[sender]['tel'] = [] contacts[sender]['mails'].append((email.get('Subject'), email.get('Date'))) if email.is_multipart(): for part in email.get_payload(): body = part.get_payload() else: body = email.get_payload() discovered_numbers = re.findall(r'[\d \(\)-]+', body) for discovered_number in discovered_numbers: try: number = phonenumbers.parse(discovered_number, "GB") if phonenumbers.is_valid_number(number): contacts[sender]['tel'].append( phonenumbers.format_number( number, phonenumbers.PhoneNumberFormat.E164)) except phonenumbers.NumberParseException: pass
print soup.prettify() parser = Parser() email = parser.parsestr(raw_email) print "From: ", email.get('From') print "To: ", email.get('To') print "Subject: ", email.get('Subject') if email.is_multipart(): for payloads in email.get_payload(): # if payload.is_multipart(): ... print "Body: ", payloads.get_payload() else: print "Body: ",email.get_payload() k = id v = raw_email emails[k] = v # create a subclass and override the handler methods class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs):
email = parser.parsestr(emailText) sender_realname, sender = parseaddr(email.get('From')) if sender not in contacts.keys(): contacts[sender] = {} contacts[sender]['email'] = sender contacts[sender]['realname'] = sender_realname contacts[sender]['gravatar'] = "https://www.gravatar.com/avatar/" + md5(sender.strip().lower()).hexdigest() contacts[sender]['mails'] = [] contacts[sender]['tel'] = [] contacts[sender]['mails'].append((email.get('Subject'), email.get('Date'))) if email.is_multipart(): for part in email.get_payload(): body = part.get_payload() else: body = email.get_payload() discovered_numbers = re.findall(r'[\d \(\)-]+', body) for discovered_number in discovered_numbers: try: number = phonenumbers.parse(discovered_number, "GB") if phonenumbers.is_valid_number(number): contacts[sender]['tel'].append(phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164)) except phonenumbers.NumberParseException: pass ##### Web Frontend
def _get_content(self, email): """Get the body content of an email. :param: email (object) the email object to extract the content from. :return: (string) body of the message. """ # get the body content of the email maintype = email.get_content_maintype() if maintype == 'multipart': for part in email.get_payload(): if part.get_content_maintype() == 'text': return part.get_payload() elif maintype == 'text': return email.get_payload()