-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_unit.py
38 lines (30 loc) · 839 Bytes
/
email_unit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import re, base64
import email
import re
from bs4 import BeautifulSoup
from email.parser import Parser
parser = Parser()
class Email(object):
"""docstring for Email"""
def __init__(self, **email_props ):
super(Email, self).__init__()
for key, prop in email_props.items():
setattr(Email, key, prop)
def get_raw_email(self):
return self.raw
def get_formatted_body(self):
return self.body
def parse(self):
encrypt = re.search("base64",str(self.raw))
if encrypt:
f1 = re.split('(base64*)', str(self.raw))
self.body = base64.b64decode(f1[2])
self.body = re.sub(r'[^\x00-\x7F]+',' ', self.body )
else:
try:
if( type(self.raw) is not str ):
self.raw = str(self.raw)
raw = BeautifulSoup( self.raw , 'html.parser')
self.body = raw.body.get_text()
except Exception as e:
raise e