Skip to content

Commit

Permalink
Merge branch 'release/0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
fedelemantuano committed Nov 8, 2016
2 parents 5379dec + 5fdb11e commit 724d5e6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
1 change: 0 additions & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ Then you can get all parts
parser.subject
parser.text_plain_list: only text plain mail parts in a list
parser.attachments_list: list of all attachments
parser.charset
parser.date_mail
parser.parsed_mail_obj: tokenized mail in a object
parser.parsed_mail_json: tokenized mail in a JSON
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ parser.from_
parser.subject
parser.text_plain_list: only text plain mail parts in a list
parser.attachments_list: list of all attachments
parser.charset
parser.date_mail
parser.parsed_mail_obj: tokenized mail in a object
parser.parsed_mail_json: tokenized mail in a JSON
Expand Down
28 changes: 13 additions & 15 deletions mailparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from __future__ import unicode_literals
from email.errors import HeaderParseError
from email.header import decode_header
from exceptions import InvalidMail, NotUnicodeError
from .exceptions import InvalidMail, NotUnicodeError
import datetime
import email
import logging
Expand Down Expand Up @@ -76,11 +76,11 @@ def _decode_header_part(self, header):

return output

def _force_unicode(self, s):
def _force_unicode(self, string, encoding):
try:
u = unicode(s, encoding=self.charset, errors='ignore')
u = unicode(string, encoding=encoding, errors='ignore')
except:
u = unicode(s, errors='ignore',)
u = unicode(string, errors='ignore',)

if not isinstance(u, unicode):
raise NotUnicodeError("Body part is not unicode")
Expand Down Expand Up @@ -125,7 +125,6 @@ def _make_mail(self):
"message_id": self.message_id,
"subject": self.subject,
"to": self.to_,
"charset": self.charset,
"has_defects": self._has_defects,
"has_anomalies": self._has_anomalies,
}
Expand All @@ -150,8 +149,8 @@ def _parse(self):
epilogue = self.find_between(
self._message.epilogue,
"{}".format("--" + self._message.get_boundary()),
"{}".format("--" + self._message.get_boundary() + "--"),
)
"{}".format("--" + self._message.get_boundary() + "--"))

try:
p = email.message_from_string(epilogue)
parts.append(p)
Expand All @@ -162,19 +161,21 @@ def _parse(self):
for p in parts:
if not p.is_multipart():
f = p.get_filename()
charset = p.get_content_charset('utf-8')

if f:
filename = self._decode_header_part(f)
mail_content_type = self._decode_header_part(
p.get_content_type(),
)
p.get_content_type())
transfer_encoding = \
unicode(p.get('content-transfer-encoding', '')).lower()

if transfer_encoding == "base64":
payload = p.get_payload(decode=False)
else:
payload = self._force_unicode(
p.get_payload(decode=True))
string=p.get_payload(decode=True),
encoding=charset)

self._attachments.append(
{
Expand All @@ -186,7 +187,8 @@ def _parse(self):
)
else:
payload = self._force_unicode(
p.get_payload(decode=True))
string=p.get_payload(decode=True),
encoding=charset)
self._text_plain.append(payload)

# Parsed object mail
Expand Down Expand Up @@ -257,10 +259,6 @@ def text_plain_list(self):
def attachments_list(self):
return self._attachments

@property
def charset(self):
return self._message.get_content_charset('utf-8')

@property
def date_mail(self):
date_ = self._message.get('date')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from setuptools import setup


VERSION = (0, 3, 7)
VERSION = (0, 4, 0)
__version__ = VERSION
__versionstr__ = '.'.join(map(str, VERSION))

Expand Down

0 comments on commit 724d5e6

Please sign in to comment.