(20241210) EMail filter added for listing emails.

This commit is contained in:
2024-12-10 10:48:46 +05:30
parent a3589c4eb7
commit e7ca685e20
2 changed files with 63 additions and 4 deletions
+4 -1
View File
@@ -165,7 +165,10 @@ class MailRetrieveModel(BaseModel):
# Get the data from the database:
mails_list = await mongo_conn.find_many(
collection = self.MAIL_COLLECTION,
filter = {"tokenId": {"$in": token_id}},
filter = {
"tokenId": {"$in": token_id},
"serviceType": "email"
},
projection = {
"_id": True,
"serviceType": True,
+59 -3
View File
@@ -60,6 +60,8 @@ from typing import Any, Dict
import base64
import quopri
import time
# *****************************************************************************************************************
# ***** ****
@@ -88,6 +90,31 @@ import quopri
# *****************************************************************************************************************
def parse_part(
# part: email.message.Message
part
) -> Dict[str, Any]:
part_json = {
"contentType": part.get_content_type(),
"contentMainType": part.get_content_maintype(),
"contentSubType": part.get_content_subtype(),
"contentCharset": part.get_content_charset(),
"contentDisposition": part.get_content_disposition(),
"filename": part.get_filename(),
"payload": part.get_payload(decode = True)
}
if part_json["contentCharset"] is not None:
part_json["payload"] = part_json["payload"].decode(part_json["contentCharset"])
print("PARSED PART:", json.to_string(part_json, default = str))
return part_json
# ---------------------------------------------------------------------------------------------------------------------
def parse(raw_mail: str | bytes) -> Dict[str, Any]:
"""
@@ -106,11 +133,40 @@ def parse(raw_mail: str | bytes) -> Dict[str, Any]:
# Make variables:
parts = []
attachments = []
# Iterate through each part of the mail for multipart mails:
if parsed_mail.is_multipart():
for part in parsed_mail.walk():
print("MULTIPART PART:", part)
parse_part(part)
# # Start by extracting basic details about the part:
# part_json = {
# "contentType": part.get("Content-Type"),
# "contentDisposition": part.get("Content-Disposition"),
# "contentTransferEncoding": part.get("Content-Transfer-Encoding"),
# "contentId": part.get("Content-ID"),
# # "part": part.get_payload(decode=True)
# }
#
# # Now check if this part is either plain or HTML text:
# content_type = part_json["contentType"].lower()
# if (
# content_type.find("text/html") >= 0 or
# content_type.find("text/plain") >= 0
# ): parts.append(parse_part(part))
#
# # print("MULTIPART PART:", json.to_string(part_json, default = str))
# # if (
# # isinstance(part_json["part"], bytes) and
# # part_json[""].find()
# # ):
# # time.sleep(1.0)
# # try: part_json["part"] = part_json["part"].decode()
# # except: pass
# # print("MULTIPART PART:", json.to_string(part_json))
# print("\n\n---\n\n")
# # if part_json[]
# When the mails are not multipart, just plaintext:
else: print("PLAINTEXT PART:", parsed_mail.get_payload())
@@ -127,7 +183,7 @@ if __name__ == "__main__":
from utils_v2.system import files
mail_string_raw = files.read_file(r"/home/developer/Downloads/raw_mail.txt")
mail_string_raw = files.read_file(r"/home/developer/Downloads/raw_mail_test.txt")
parse_results = parse(mail_string_raw)
print(json.to_string(parse_results, default = str))
# print(json.to_string(parse_results, default = str))