@@ -1,7 +1,8 @@
|
|||||||
import re
|
import re
|
||||||
import StringIO
|
import StringIO
|
||||||
import email
|
import email
|
||||||
from email.header import decode_header
|
import base64, quopri
|
||||||
|
from email.header import Header, decode_header
|
||||||
|
|
||||||
|
|
||||||
class Struct(object):
|
class Struct(object):
|
||||||
@@ -45,6 +46,26 @@ def get_mail_addresses(message, header_name):
|
|||||||
|
|
||||||
return addresses
|
return addresses
|
||||||
|
|
||||||
|
|
||||||
|
def decode_param(param):
|
||||||
|
name, v = param.split('=', 1)
|
||||||
|
values = v.split('\n')
|
||||||
|
value_results = []
|
||||||
|
for value in values:
|
||||||
|
match = re.search(r'=\?(\w+)\?(Q|B)\?(.+)\?=', value)
|
||||||
|
if match:
|
||||||
|
encoding, type_, code = match.groups()
|
||||||
|
if type_ == 'Q':
|
||||||
|
value = quopri.decodestring(code)
|
||||||
|
elif type_ == 'B':
|
||||||
|
value = base64.decodestring(code)
|
||||||
|
value = unicode(value, encoding)
|
||||||
|
value_results.append(value)
|
||||||
|
if value_results: v = ''.join(value_results)
|
||||||
|
return name, v
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse_attachment(message_part):
|
def parse_attachment(message_part):
|
||||||
content_disposition = message_part.get("Content-Disposition", None) # Check again if this is a valid attachment
|
content_disposition = message_part.get("Content-Disposition", None) # Check again if this is a valid attachment
|
||||||
if content_disposition != None:
|
if content_disposition != None:
|
||||||
@@ -59,10 +80,8 @@ def parse_attachment(message_part):
|
|||||||
'content': StringIO.StringIO(file_data)
|
'content': StringIO.StringIO(file_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for param in dispositions[1:]:
|
for param in dispositions[1:]:
|
||||||
name,value = param.split("=")
|
name, value = decode_param(param)
|
||||||
name = name.lower()
|
|
||||||
|
|
||||||
if 'file' in name:
|
if 'file' in name:
|
||||||
attachment['filename'] = value
|
attachment['filename'] = value
|
||||||
@@ -72,7 +91,7 @@ def parse_attachment(message_part):
|
|||||||
|
|
||||||
return attachment
|
return attachment
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def parse_email(raw_email):
|
def parse_email(raw_email):
|
||||||
email_message = email.message_from_string(raw_email)
|
email_message = email.message_from_string(raw_email)
|
||||||
@@ -96,13 +115,13 @@ def parse_email(raw_email):
|
|||||||
elif content_type == "text/html" and content_disposition == None:
|
elif content_type == "text/html" and content_disposition == None:
|
||||||
body['html'].append(content)
|
body['html'].append(content)
|
||||||
elif content_disposition:
|
elif content_disposition:
|
||||||
attachments.append(parse_attachment(part))
|
attachment = parse_attachment(part)
|
||||||
|
if attachment: attachments.append(attachment)
|
||||||
|
|
||||||
elif maintype == 'text':
|
elif maintype == 'text':
|
||||||
body['plain'].append(email_message.get_payload(decode=True))
|
body['plain'].append(email_message.get_payload(decode=True))
|
||||||
|
|
||||||
if len(attachments) > 0:
|
parsed_email['attachments'] = attachments
|
||||||
parsed_email['attachments'] = attachments
|
|
||||||
|
|
||||||
parsed_email['body'] = body
|
parsed_email['body'] = body
|
||||||
email_dict = dict(email_message.items())
|
email_dict = dict(email_message.items())
|
||||||
@@ -129,4 +148,5 @@ def parse_email(raw_email):
|
|||||||
parsed_email['headers'].append({'Name': key,
|
parsed_email['headers'].append({'Name': key,
|
||||||
'Value': value})
|
'Value': value})
|
||||||
|
|
||||||
return Struct(**parsed_email)
|
return Struct(**parsed_email)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user