From f911e8aacd8b8cd1d86c3076bfa139e095ec5f42 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Dec 2023 23:31:47 +0100 Subject: [PATCH 1/3] add imbox --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index a17d5a2..88b903b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ pytest-playwright python-dotenv icecream loguru -beautifulsoup4 \ No newline at end of file +beautifulsoup4 +imbox From 6da13d1120bec486a743b15fdd9c29ed8f68e29b Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Dec 2023 23:34:19 +0100 Subject: [PATCH 2/3] fix credentials --- prototyping/email_stuff.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/prototyping/email_stuff.py b/prototyping/email_stuff.py index a8896f8..ce0d850 100644 --- a/prototyping/email_stuff.py +++ b/prototyping/email_stuff.py @@ -1,6 +1,7 @@ # %% import email import json +import os from email.header import decode_header from imaplib import IMAP4, IMAP4_SSL from pathlib import Path @@ -11,19 +12,20 @@ cred_file = Path("../credentials.json") with open(cred_file, "r") as f: CREDENTIALS = json.load(f) -username = CREDENTIALS["imap_user"] -password = CREDENTIALS["imap_pass"] +for key, value in CREDENTIALS.items(): + os.environ[key] = value + +IMAP_HOST = os.environ["IMAP_HOST"] +IMAP_PORT = os.environ["IMAP_PORT"] +IMAP_USER = os.environ["IMAP_USER"] +IMAP_PASS = os.environ["IMAP_PASS"] # ----------------------------------- imap ----------------------------------- # -host = "mail.local-it.org" -imap_port = 143 -imap_ssl_port = 993 - -with IMAP4_SSL(host=host) as imap_server: - imap_server.login(username, password) +with IMAP4_SSL(host=IMAP_HOST) as imap_server: + imap_server.login(IMAP_USER, IMAP_PASS) imap_server.select("INBOX") # Search for all emails in the folder From 8f720f889b60f37f23d6f2f0b5093e197584941f Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Dec 2023 23:42:12 +0100 Subject: [PATCH 3/3] initial commit --- prototyping/email_stuff_2.py | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 prototyping/email_stuff_2.py diff --git a/prototyping/email_stuff_2.py b/prototyping/email_stuff_2.py new file mode 100644 index 0000000..15cf562 --- /dev/null +++ b/prototyping/email_stuff_2.py @@ -0,0 +1,53 @@ +# %% +import datetime +import json +import os +from pathlib import Path + +from imbox import Imbox + +cred_file = Path("../credentials.json") +with open(cred_file, "r") as f: + CREDENTIALS = json.load(f) + +for key, value in CREDENTIALS.items(): + os.environ[key] = value + +IMAP_HOST = os.environ["IMAP_HOST"] +IMAP_PORT = os.environ["IMAP_PORT"] +IMAP_USER = os.environ["IMAP_USER"] +IMAP_PASS = os.environ["IMAP_PASS"] + + +with Imbox( + hostname=os.environ["IMAP_HOST"], + port=os.environ["IMAP_PORT"], + username=os.environ["IMAP_USER"], + password=os.environ["IMAP_PASS"], + ssl=True, + ssl_context=None, + starttls=False, +) as imbox: + # Get all folders + status, folders_with_additional_info = imbox.folders() + + # Gets all messages from the inbox + all_inbox_messages = imbox.messages() + + # Messages received after specific date + inbox_messages_received_after = imbox.messages(date__gt=datetime.date(2018, 7, 30)) + + # Messages whose subjects contain a string + inbox_messages_subject_christmas = imbox.messages(subject="Christmas") + + for uid, message in all_inbox_messages: + print(uid, message.subject, message.date) + # # Every message is an object with the following keys + + # message.sent_from + # message.sent_to + # message.subject + # message.headers + # message.message_id + # message.date + # message.body.plain