* refactoring and rework: runner now has setups / tests / cleanups as lists * add nextcloud runner * add email testing prototype with imap fixture * add dependency resolution (sort env files in input so that test order is correct) Reviewed-on: local-it-infrastructure/e2e_tests#5 Co-authored-by: Daniel <d.brummerloh@gmail.com> Co-committed-by: Daniel <d.brummerloh@gmail.com>
47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
# %%
|
|
import email
|
|
import json
|
|
from email.header import decode_header
|
|
from imaplib import IMAP4, IMAP4_SSL
|
|
from pathlib import Path
|
|
|
|
# -------------------------------- credentials ------------------------------- #
|
|
|
|
cred_file = Path("../credentials.json")
|
|
with open(cred_file, "r") as f:
|
|
CREDENTIALS = json.load(f)
|
|
|
|
username = CREDENTIALS["imap_user"]
|
|
password = CREDENTIALS["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)
|
|
imap_server.select("INBOX")
|
|
|
|
# Search for all emails in the folder
|
|
status, email_ids = imap_server.search(None, "ALL")
|
|
email_ids = email_ids[0].split()
|
|
|
|
# Fetch email details using the retrieved IDs
|
|
for email_id in email_ids:
|
|
result, data = imap_server.fetch(email_id, "(RFC822)")
|
|
raw_email = data[0][1] # Raw content of the email
|
|
email_message = email.message_from_bytes(raw_email)
|
|
|
|
# Extract the subject
|
|
subject_encoded = email_message.get("Subject")
|
|
decoded_subject = decode_header(subject_encoded)[0][0]
|
|
|
|
if isinstance(decoded_subject, bytes):
|
|
decoded_subject = decoded_subject.decode()
|
|
|
|
# Print or use the subject as needed
|
|
print("Subject:", decoded_subject)
|