create mail forwarder

This commit is contained in:
alex 2022-05-22 22:37:01 -04:00
parent fe0db8588a
commit 62477be9e5
3 changed files with 105 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
command.sh
# ---> Python # ---> Python
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/

View File

@ -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()

14
pyproject.toml Normal file
View File

@ -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"