Commit de0d7624 authored by scmalte's avatar scmalte

mails.py: consolidating experiments

parent 376b6002
Sehr geehrte Studierende
Sehr geehrte Studierende
Sie sind beim Kopieren der Bonusaufgabe erwischt worden. Das ist höchst unerfreulich — für Sie! Denn nun müssen wir Ihnen leider die Punkte wieder abziehen.
......
import logging
import mailbox
import email
from email.message import EmailMessage
from email.headerregistry import Address
# import quopri
import os
import pathlib
import glob
import csv
import datetime
from .utils import logging as logutils
DEFAULT_CLUSTER_FILES_DIR="_clusters"
DEFAULT_CLUSTER_STUDENTS_CSV_FILE_GLOB="cluster-students-[0-9]*.csv"
DEFAULT_MAIL_BODY_FILE="./_static/cluster_mail.txt"
DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER="To" # To, CC, BCC
DEFAULT_SENDER="malte.schwerhoff@inf.ethz.ch"
DEFAULT_SUBJECT="You've been caught!"
DEFAULT_SENDER_ADDRESS = Address("Malte Schwerhoff", "malte.schwerhoff", "inf.ethz.ch")
DEFAULT_SUBJECT="You've been caught — gö höme!"
def main(
cluster_files_dir=DEFAULT_CLUSTER_FILES_DIR,
cluster_students_csv_file_glob=DEFAULT_CLUSTER_STUDENTS_CSV_FILE_GLOB,
mail_body_file=DEFAULT_MAIL_BODY_FILE,
students_receive_mails_as_header=DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER,
sender=DEFAULT_SENDER,
sender_address=DEFAULT_SENDER_ADDRESS,
subject=DEFAULT_SUBJECT):
logutils.configure_level_and_format()
......@@ -42,7 +46,9 @@ def main(
mbox_path.unlink(missing_ok=True)
mbox_msf_path.unlink(missing_ok=True)
with open(mail_body_file) as body_fh:
logging.info("Reading mail body from file {}".format(mail_body_file))
logging.info("IMPORTANT: File encoding must be UTF-8!")
with open(mail_body_file, encoding="utf-8") as body_fh:
mail_body = body_fh.read()
mbox = mailbox.mbox(mbox_path)
......@@ -56,23 +62,25 @@ def main(
with open(students_per_cluster_csv, newline='') as csv_fh:
cluster_csv = list(csv.DictReader(csv_fh))
recipients = ", ".join([row["Email"] for row in cluster_csv])
logging.info("Creating mail to {}".format(recipients))
recipients = [Address(addr_spec=row["Email"]) for row in cluster_csv]
msg = mailbox.mboxMessage()
# msg.set_unixfrom("author Sat Feb 7 01:05:34 2009")
msg["From"] = sender
# test_addresses = ["foo@bar.com"]
# recipients = [Address(addr_spec=addr) for adrr in test_addresses]
logging.debug("Creating mail to {}".format(", ".join([str(r) for r in recipients])))
msg = EmailMessage()
msg["From"] = sender_address
msg["To"] = recipients
msg["Subject"] = subject
msg[students_receive_mails_as_header] = recipients
# msg["Content-Transfer-Encoding"] = "quoted-printable"
msg["Content-Type"] = "text/plain; charset=utf-8"
msg["Content-Transfer-Encoding"] = "quoted-printable"
msg["Content-Disposition"] = "inline"
msg["MIME-Version"] = "1.0"
msg.set_payload(mail_body)
msg['Date'] = email.utils.localtime()
msg.set_content(mail_body)
mbox.add(msg)
mbox.flush()
break
finally:
mbox.unlock()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment