From 62477be9e531a11a17f50466a775717de400f07b Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 22 May 2022 22:37:01 -0400 Subject: [PATCH] create mail forwarder --- .gitignore | 2 + mail-forwarder/__main__.py | 89 ++++++++++++++++++++++++++++++++++++++ pyproject.toml | 14 ++++++ 3 files changed, 105 insertions(+) create mode 100644 mail-forwarder/__main__.py create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore index f8b73e7..7aad2c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +command.sh + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/mail-forwarder/__main__.py b/mail-forwarder/__main__.py new file mode 100644 index 0000000..85b62a0 --- /dev/null +++ b/mail-forwarder/__main__.py @@ -0,0 +1,89 @@ +import smtplib, imaplib, email, ssl, os +from email import policy + +imap_host = os.getenv('IMAP_HOST', "imap.mail.yahoo.com") +smtp_host = os.getenv('SMTP_HOST', "smtp.mail.yahoo.com") + +smtp_port = os.getenv('SMTP_PORT', 587) # SSL + +sender = os.environ.get('SENDER_USERNAME') +passwd = os.environ.get('SENDER_PASSWORD') +recipient = os.environ.get('RECIPIENT_EMAIL') + +subject_search = os.environ.get('SUBJECT_SEARCH') + +if sender is None: + print("ERROR: Environment variable SENDER_USERNAME is not defined") + exit() + +if passwd is None: + print("ERROR: Environment variable SENDER_PASSWORD is not defined") + exit() + +if recipient is None: + print("ERROR: Environment variable RECIPIENT_EMAIL is not defined") + exit() + +if subject_search is None: + print("ERROR: Environment variable SUBJECT_SEARCH is not defined") + exit() + +# open IMAP connection and fetch message with id msgid +# store message data in email_data +print("Creating IMAP client...") +imap = imaplib.IMAP4_SSL(imap_host) + +print("Logging in to IMAP server...") +imap.login(sender, passwd) + +print("Listing mailboxes...") +print(imap.list()) + +print("Selecting INBOX...") +imap.select('INBOX') + +print("Searching emails...") +status, msgs = imap.search(None, 'SUBJECT', f'"{subject_search}"') + +context = ssl.SSLContext(ssl.PROTOCOL_TLS) + +print("Instantiating SMTP client...") +smtp = smtplib.SMTP(smtp_host, smtp_port) +smtp.starttls(context=context) + +print("Logging in to SMTP server...") +smtp.login(sender, passwd) + +for num in msgs[0].split(): + status, data = imap.fetch(num, '(RFC822)') + message_bytes = data[0][1] + + print("Parsing message with default policy...") + original_msg = email.message_from_bytes(message_bytes, policy=policy.default) + + print("Constructing message copy...") + copied_msg = email.message.EmailMessage() + copied_msg['From'] = sender + copied_msg['To'] = recipient + copied_msg['Subject'] = original_msg['Subject'] + copied_msg['Date'] = original_msg['Date'] + copied_msg['MIME-Version'] = original_msg['MIME-Version'] + copied_msg['Content-Type'] = original_msg['Content-Type'] + + print("Copying message contents...") + for part in original_msg.walk(): + copied_msg.attach(part) + + print("Sending new message...") + print("Date is: ", copied_msg['Date']) + smtp.send_message(copied_msg, sender, recipient) + print("Message sent.") + + print("Sending message to trash...") + imap.store(num, '+FLAGS', '\\Deleted') + print("Message marked for deletion...") + +imap.expunge() +imap.close() +imap.logout() +smtp.quit() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..9b3ea04 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[tool.poetry] +name = "mail-forwarder" +version = "0.1.0" +description = "" +authors = ["alex"] + +[tool.poetry.dependencies] +python = "^3.9" + +[tool.poetry.dev-dependencies] + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api"