You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, fast_mail_parser return only the last header value if there are multiple headers with the same key (like Received, which is nearly almost the case).
An example program with Python's build-in parser:
# Hardcoded email data (for the sake of example, the email data is included here as a raw string)email_data="""\From: sender@example.comTo: recipient@example.comSubject: Test EmailDate: Mon, 13 Sep 2024 10:00:00 +0200Received: from mail.example.com (mail.example.com [192.0.2.1]) by smtp.example.com with ESMTP id abc123 for <recipient@example.com>; Mon, 13 Sep 2024 09:55:00 +0200Received: from smtp2.example.com (smtp2.example.com [192.0.2.2]) by mail.example.com with ESMTP id def456 for <recipient@example.com>; Mon, 13 Sep 2024 09:50:00 +0200Received: from relay.example.com (relay.example.com [192.0.2.3]) by smtp2.example.com with ESMTP id ghi789 for <recipient@example.com>; Mon, 13 Sep 2024 09:45:00 +0200MIME-Version: 1.0Content-Type: text/plain; charset="utf-8"Content-Transfer-Encoding: 7bitThis is a test email."""importemailfromemailimportpolicyfromemail.parserimportBytesParserfromioimportBytesIOdefparse_received_headers(email_data):
"""Parse the Received: headers from a hardcoded EML string."""# Convert the email string to bytes (as we would normally read an EML file in bytes)email_bytes=BytesIO(email_data.encode('utf-8'))
# Parse the email messageemail_message=BytesParser(policy=policy.default).parse(email_bytes)
# Extract Received headers in the order they appearreceived_headers= []
forheader, valueinemail_message.items():
ifheader.lower() =='received':
received_headers.append(value)
returnreceived_headers# Call the function with the hardcoded emailreceived_headers=parse_received_headers(email_data)
# Print out the Received headers in orderfori, headerinenumerate(received_headers, 1):
print(f"Received Header {i}:\n{header}\n")
$ python test.py
Received Header 1:
from mail.example.com (mail.example.com [192.0.2.1]) by smtp.example.com with ESMTP id abc123 for<recipient@example.com>; Mon, 13 Sep 2024 09:55:00 +0200
Received Header 2:
from smtp2.example.com (smtp2.example.com [192.0.2.2]) by mail.example.com with ESMTP id def456 for<recipient@example.com>; Mon, 13 Sep 2024 09:50:00 +0200
Received Header 3:
from relay.example.com (relay.example.com [192.0.2.3]) by smtp2.example.com with ESMTP id ghi789 for<recipient@example.com>; Mon, 13 Sep 2024 09:45:00 +0200
and the same with fast_mail_parser:
email_data="""\From: sender@example.comTo: recipient@example.comSubject: Test EmailDate: Mon, 13 Sep 2024 10:00:00 +0200Received: from mail.example.com (mail.example.com [192.0.2.1]) by smtp.example.com with ESMTP id abc123 for <recipient@example.com>; Mon, 13 Sep 2024 09:55:00 +0200Received: from smtp2.example.com (smtp2.example.com [192.0.2.2]) by mail.example.com with ESMTP id def456 for <recipient@example.com>; Mon, 13 Sep 2024 09:50:00 +0200Received: from relay.example.com (relay.example.com [192.0.2.3]) by smtp2.example.com with ESMTP id ghi789 for <recipient@example.com>; Mon, 13 Sep 2024 09:45:00 +0200MIME-Version: 1.0Content-Type: text/plain; charset="utf-8"Content-Transfer-Encoding: 7bitThis is a test email."""fromfast_mail_parserimportparse_email, ParseErrorfrompprintimportpprintasppemail=parse_email(email_data)
pp(email.headers)
Specifically for the Received header, the most significant one if the first, but fast_mail_parser returns only the last one. Could you please add (not to break compatibility) another header representation, which correctly lists all headers, maybe with a list of tuples?
The text was updated successfully, but these errors were encountered:
Currently,
fast_mail_parser
return only the last header value if there are multiple headers with the same key (likeReceived
, which is nearly almost the case).An example program with Python's build-in parser:
and the same with
fast_mail_parser
:Specifically for the
Received
header, the most significant one if the first, butfast_mail_parser
returns only the last one. Could you please add (not to break compatibility) another header representation, which correctly lists all headers, maybe with a list of tuples?The text was updated successfully, but these errors were encountered: