To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

mails.py 2.73 KB
Newer Older
1 2
import logging
import mailbox
3
# import quopri
4
import os
5 6 7
import pathlib
import glob
import csv
8 9
from .utils import logging as logutils

10 11 12
DEFAULT_CLUSTER_FILES_DIR="_clusters"
DEFAULT_CLUSTER_STUDENTS_CSV_FILE_GLOB="cluster-students-[0-9]*.csv"
DEFAULT_MAIL_BODY_FILE="./_static/cluster_mail.txt"
13
DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER="To" # To, CC, BCC
14 15
DEFAULT_SENDER="malte.schwerhoff@inf.ethz.ch"
DEFAULT_SUBJECT="You've been caught!"
16 17

def main(
18 19
    cluster_files_dir=DEFAULT_CLUSTER_FILES_DIR,
    cluster_students_csv_file_glob=DEFAULT_CLUSTER_STUDENTS_CSV_FILE_GLOB,
20
    mail_body_file=DEFAULT_MAIL_BODY_FILE,
21 22 23 24
    students_receive_mails_as_header=DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER,
    sender=DEFAULT_SENDER,
    subject=DEFAULT_SUBJECT):

25 26 27 28 29 30 31
  logutils.configure_level_and_format()

  unsent_mbox = "D:\\Program_Data\\Thunderbird Portable\\Data\\profile\\Mail\\Local Folders\\Unsent Messages"

  mbox_path = pathlib.Path(unsent_mbox)
  mbox_msf_path = pathlib.Path(unsent_mbox + ".msf")

scmalte's avatar
scmalte committed
32
  logging.warn("ATTENTION: If you continue, the files")
33 34 35
  logging.warn("  {}".format(mbox_path))
  logging.warn("  {}".format(mbox_msf_path))
  logging.warn("will be overwritten, if they exist. Make sure that do not have any pending unsent messages!")
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  
  if (input("Do you want to continue (enter 'yes')? ").lower() != "yes"):
    logging.debug("Exiting program because user did not confirm to continue")
    exit(0)

  # Delete mailbox file and corresponding summary/index file
  mbox_path.unlink(missing_ok=True)
  mbox_msf_path.unlink(missing_ok=True)

  with open(mail_body_file) as body_fh:
    mail_body = body_fh.read()

  mbox = mailbox.mbox(mbox_path)
  mbox.lock()
  try:
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    cluster_students_glob = os.path.join(cluster_files_dir, cluster_students_csv_file_glob)

    logging.info("Taking students per cluster from files {}".format(cluster_students_glob))

    for students_per_cluster_csv in glob.iglob(cluster_students_glob):
      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))

      msg = mailbox.mboxMessage()
      # msg.set_unixfrom("author Sat Feb  7 01:05:34 2009")
      msg["From"] = sender
      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)
      mbox.add(msg)
      mbox.flush()

76 77 78
  finally:
    mbox.unlock()

79

80 81
if __name__ == "__main__":
  main()