Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mailparser/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def main():
parser = get_parser(args)
process_output(args, parser)
except Exception as e:
log.error(f"An error occurred: {e}")
log.exception(f"An error occurred: {e}")
sys.exit(1)


Expand Down
8 changes: 3 additions & 5 deletions src/mailparser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,12 @@ def body(self):
)

@property
def headers(self):
def headers(self) -> dict:
"""
Return only the headers as Python object
"""
d = {}
for i in self.message.keys():
d[i] = getattr(self, i)
return d
all_headers = set(self.message.keys()) - set(["headers"])
return {i: getattr(self, i) for i in all_headers}

@property
def headers_json(self):
Expand Down
26 changes: 26 additions & 0 deletions tests/mails/mail_test_16
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Subject: Test spam mail (GTUBE)
Message-ID: <GTUBE1.1010101@example.net>
Date: Wed, 23 Jul 2003 23:30:00 +0200
From: Sender <sender@example.net>
To: Recipient <recipient@example.net>
Precedence: junk
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
headers: hello-world

This is the GTUBE, the
Generic
Test for
Unsolicited
Bulk
Email

If your spam filter supports it, the GTUBE provides a test by which you
can verify that the filter is installed correctly and is detecting incoming
spam. You can send yourself a test mail containing the following string of
characters (in upper case and with no white spaces and line breaks):

XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X

You should send this test mail from an account outside of your network.
15 changes: 15 additions & 0 deletions tests/test_mail_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
mail_test_13 = os.path.join(base_path, "mails", "mail_test_13")
mail_test_14 = os.path.join(base_path, "mails", "mail_test_14")
mail_test_15 = os.path.join(base_path, "mails", "mail_test_15")
mail_test_16 = os.path.join(base_path, "mails", "mail_test_16")
mail_malformed_1 = os.path.join(base_path, "mails", "mail_malformed_1")
mail_malformed_2 = os.path.join(base_path, "mails", "mail_malformed_2")
mail_malformed_3 = os.path.join(base_path, "mails", "mail_malformed_3")
Expand Down Expand Up @@ -646,3 +647,17 @@ def test_write_uuencode_attachment(self):
md5.update(f.read())
shutil.rmtree(temp_dir)
self.assertEqual(md5.hexdigest(), "4f2cf891e7cfb349fca812091f184ecc")

def test_issue_139(self):
mail = mailparser.parse_from_file(mail_test_16)
assert mail.headers == {
"MIME-Version": "1.0",
"Precedence": "junk",
"Content-Type": "text/plain; charset=us-ascii",
"From": [("Sender", "sender@example.net")],
"Date": "Wed, 23 Jul 2003 23:30:00 +0200",
"Content-Transfer-Encoding": "7bit",
"Message-ID": "<GTUBE1.1010101@example.net>",
"Subject": "Test spam mail (GTUBE)",
"To": [("Recipient", "recipient@example.net")],
}