Commit 75b691be authored by Benjamin Krause's avatar Benjamin Krause
Browse files

Adjust get_mail functions

parent 47439860
...@@ -34,43 +34,67 @@ class MailProvider(BaseModel): ...@@ -34,43 +34,67 @@ class MailProvider(BaseModel):
mailboxes = imap.list() mailboxes = imap.list()
print(mailboxes) print(mailboxes)
def search_relevant_mails(self, user: User, mailbox: str): @staticmethod
if not mailbox: def parse_mail(msg):
print("Specified mailbox is invalid.") for response in msg:
return if isinstance(response, tuple):
if not user or not user.mail or not user.password: msg = email.message_from_bytes(response[1])
print("Missing or invalid user data") if msg.is_multipart():
return return
try:
return Mail(
date=msg.get("date"),
subject=msg.get("subject"),
mail_from=msg.get("from"),
mail_to=msg.get("to"),
cc="",
body=msg.get_payload(decode=True).decode(),
)
except Exception as e:
print(e)
def imap_login(self, user: User) -> imaplib.IMAP4_SSL:
imap = imaplib.IMAP4_SSL(self.imap_url) imap = imaplib.IMAP4_SSL(self.imap_url)
imap.login(user.mail, user.password) imap.login(user.mail, user.password)
return imap
imap.select(mailbox) @staticmethod
status, messages = imap.search(None, f"SUBJECT {subject_identifier}") def imap_logout(imap: imaplib.IMAP4_SSL):
if messages[0] == b"": imap.close()
print(f"No messages with the subject {subject_identifier} found!") imap.logout()
return
messages = len(messages[0].decode("utf-8").split(" "))
mails = [] def get_all_mails_from_mailbox(self, user: User, mailbox: str):
imap = self.imap_login(user)
status, byte_message_count = imap.select(mailbox)
mail_count = int(byte_message_count[0])
raw_messages = []
# fetch raw mails
for i in range(mail_count, 0, -1):
res, msg = imap.fetch(str(i), "(RFC822)")
raw_messages.append(msg)
self.imap_logout(imap)
messages = []
for message in raw_messages:
m = self.parse_mail(message)
messages.append(m) if m else None
return messages
for i in range(messages, 0, -1): def search_relevant_mails(self, user: User, mailbox: str):
imap = self.imap_login(user)
imap.select(mailbox)
status, byte_message_position_ids = imap.search(None, f"SUBJECT {subject_identifier}")
if byte_message_position_ids[0] == b"":
print(f"No messages with the subject {subject_identifier} found!")
return []
message_position_ids = byte_message_position_ids[0].decode("utf-8").split(" ")
raw_messages = []
# fetch raw mails
for i in message_position_ids:
res, msg = imap.fetch(str(i), "(RFC822)") res, msg = imap.fetch(str(i), "(RFC822)")
for response in msg: raw_messages.append(msg)
if isinstance(response, tuple): self.imap_logout(imap)
msg = email.message_from_bytes(response[1]) messages = []
try: for message in raw_messages:
mails.append( m = self.parse_mail(message)
Mail( messages.append(m) if m else None
date=msg.get("date"), return messages
subject=msg.get("subject"),
mail_from=msg.get("from"),
mail_to=msg.get("to"),
cc=msg.get("cc"),
body=msg.get_payload(decode=True).decode(),
)
)
except Exception as e:
print(e)
imap.close()
imap.logout()
return mails
...@@ -50,6 +50,17 @@ def init() -> FastAPI: ...@@ -50,6 +50,17 @@ def init() -> FastAPI:
pprint(mails) pprint(mails)
return mails return mails
@app.post("/mails/get_all")
def get_all_mails(data: GetMail):
"""
Returns all non multipart emails
:param data:
:return:
"""
mails = mail_providers.get("webde").get_all_mails_from_mailbox(data.user, data.mailbox)
pprint(mails)
return mails
@app.post("/mails/send") @app.post("/mails/send")
def send_mail(data: SendMail): def send_mail(data: SendMail):
print("Send a mail.") print("Send a mail.")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment