diff --git a/parsedmarc/__init__.py b/parsedmarc/__init__.py index e909c59..a20dc65 100644 --- a/parsedmarc/__init__.py +++ b/parsedmarc/__init__.py @@ -36,7 +36,8 @@ import mailparser from parsedmarc.__version__ import __version__ from parsedmarc.utils import get_base_domain, get_ip_address_info from parsedmarc.utils import is_outlook_msg, convert_outlook_msg -from parsedmarc.utils import timestamp_to_human, parse_email, EmailParserError +from parsedmarc.utils import timestamp_to_human, human_timestamp_to_datetime +from parsedmarc.utils import parse_email, EmailParserError logger = logging.getLogger("parsedmarc") logger.debug("parsedmarc v{0}".format(__version__)) @@ -641,18 +642,20 @@ def parse_report_email(input_, nameservers=None, timeout=2.0): input_ = convert_outlook_msg(input_) msg = mailparser.parse_from_string(input_) msg_headers = json.loads(msg.headers_json) + date = email.utils.format_datetime(datetime.utcnow()) + if "date" in msg_headers: + date = human_timestamp_to_datetime( + msg_headers["date"].replace("T", "")) msg = email.message_from_string(input_) + except Exception as e: raise ParserError(e.__str__()) - date = email.utils.format_datetime(datetime.utcnow()) subject = None feedback_report = None sample_headers_only = False sample = None if "subject" in msg_headers: subject = msg_headers["subject"] - if "date" in msg_headers: - date = msg_headers["date"] for part in msg.walk(): content_type = part.get_content_type() payload = part.get_payload() diff --git a/parsedmarc/utils.py b/parsedmarc/utils.py index e8e82cb..1c44ba3 100644 --- a/parsedmarc/utils.py +++ b/parsedmarc/utils.py @@ -390,8 +390,8 @@ def parse_email(data): headers = json.loads(parsed_email.headers_json).copy() parsed_email = json.loads(parsed_email.mail_json).copy() parsed_email["headers"] = headers - if "date" in parsed_email: - parsed_email["date"] = parsed_email["date"].replace("T", " ") + if "date" in headers: + parsed_email["date"] = headers["date"].replace("T", " ") if "received" in parsed_email: for received in parsed_email["received"]: if "date_utc" in received: