50 lines
1.7 KiB
Python
Executable File
50 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Re-send an original example mail (found by subject) as a fresh test mail
|
|
from chancen@ to chancen@. Fallback: synthetic single-project mail."""
|
|
import os
|
|
import smtplib
|
|
import ssl
|
|
import sys
|
|
import time
|
|
from email.message import EmailMessage
|
|
from email.policy import default as default_policy
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", ".."))
|
|
from projektmatch import mailer, mailparse # noqa: E402
|
|
|
|
USER = "chancen@destengs.com"
|
|
PW = os.environ["PM_IMAP_PASSWORD"]
|
|
SUBJECT = sys.argv[1] # e.g. "Example 1 for Langflow process"
|
|
FALLBACK_URL = sys.argv[2] if len(sys.argv) > 2 else ""
|
|
|
|
box = mailer.MailBox(USER, PW)
|
|
html = ""
|
|
for folder in ("INBOX", box.trash_folder()):
|
|
box.conn.select(folder)
|
|
typ, data = box.conn.uid("SEARCH", None,
|
|
f'SUBJECT "{SUBJECT}"')
|
|
uids = data[0].split() if data and data[0] else []
|
|
if uids:
|
|
msg = box.fetch(uids[-1].decode())
|
|
html, _ = mailparse.bodies(msg)
|
|
break
|
|
box.close()
|
|
|
|
out = EmailMessage(policy=default_policy)
|
|
out["From"], out["To"] = USER, USER
|
|
out["Subject"] = f"Test {SUBJECT} {int(time.time())}"
|
|
if html:
|
|
out.set_content("siehe HTML")
|
|
out.add_alternative(html, subtype="html")
|
|
print(f"re-sending original '{SUBJECT}' body")
|
|
else:
|
|
if not FALLBACK_URL:
|
|
sys.exit(f"original mail '{SUBJECT}' not found and no fallback URL")
|
|
out.set_content(f"Neues Projekt:\n\nTestprojekt\n{FALLBACK_URL}\n")
|
|
print("original not found -> synthetic fallback mail")
|
|
with smtplib.SMTP(mailer.SMTP_HOST, mailer.SMTP_PORT, timeout=30) as server:
|
|
server.starttls(context=ssl.create_default_context())
|
|
server.login(USER, PW)
|
|
server.send_message(out)
|
|
print(f"sent: {out['Subject']}")
|