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):
mailboxes = imap.list()
print(mailboxes)
def search_relevant_mails(self, user: User, mailbox: str):
if not mailbox:
print("Specified mailbox is invalid.")
return
if not user or not user.mail or not user.password:
print("Missing or invalid user data")
return
@staticmethod
def parse_mail(msg):
for response in msg:
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1])
if msg.is_multipart():
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.login(user.mail, user.password)
return imap
imap.select(mailbox)
status, messages = imap.search(None, f"SUBJECT {subject_identifier}")
if messages[0] == b"":
print(f"No messages with the subject {subject_identifier} found!")
return
messages = len(messages[0].decode("utf-8").split(" "))
@staticmethod
def imap_logout(imap: imaplib.IMAP4_SSL):
imap.close()
imap.logout()
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)")
for response in msg:
if isinstance(response, tuple):
msg = email.message_from_bytes(response[1])
try:
mails.append(
Mail(
date=msg.get("date"),
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
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
......@@ -50,6 +50,17 @@ def init() -> FastAPI:
pprint(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")
def send_mail(data: SendMail):
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