mails.py 2.93 KB
Newer Older
1
2
import logging
import mailbox
scmalte's avatar
scmalte committed
3
4
5
import email
from email.message import EmailMessage
from email.headerregistry import Address
6
# import quopri
7
import os
8
9
10
import pathlib
import glob
import csv
scmalte's avatar
scmalte committed
11
import datetime
12
13
from .utils import logging as logutils

14
15
16
DEFAULT_CLUSTER_FILES_DIR="_clusters"
DEFAULT_CLUSTER_STUDENTS_CSV_FILE_GLOB="cluster-students-[0-9]*.csv"
DEFAULT_MAIL_BODY_FILE="./_static/cluster_mail.txt"
17
DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER="To" # To, CC, BCC
scmalte's avatar
scmalte committed
18
19
DEFAULT_SENDER_ADDRESS = Address("Malte Schwerhoff", "malte.schwerhoff", "inf.ethz.ch")
DEFAULT_SUBJECT="You've been caught — gö höme!"
20
21

def main(
22
23
    cluster_files_dir=DEFAULT_CLUSTER_FILES_DIR,
    cluster_students_csv_file_glob=DEFAULT_CLUSTER_STUDENTS_CSV_FILE_GLOB,
24
    mail_body_file=DEFAULT_MAIL_BODY_FILE,
25
    students_receive_mails_as_header=DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER,
scmalte's avatar
scmalte committed
26
    sender_address=DEFAULT_SENDER_ADDRESS,
27
28
    subject=DEFAULT_SUBJECT):

29
30
31
32
33
34
35
  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
36
  logging.warn("ATTENTION: If you continue, the files")
37
38
39
  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!")
40
41
42
43
44
45
46
47
48
  
  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)

scmalte's avatar
scmalte committed
49
50
51
  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:
52
53
54
55
56
    mail_body = body_fh.read()

  mbox = mailbox.mbox(mbox_path)
  mbox.lock()
  try:
57
58
59
60
61
62
63
64
    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))

scmalte's avatar
scmalte committed
65
      recipients = [Address(addr_spec=row["Email"]) for row in cluster_csv]
66

scmalte's avatar
scmalte committed
67
68
69
70
71
72
73
74
      # 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
75
      msg["Subject"] = subject
scmalte's avatar
scmalte committed
76
77
78
      msg['Date'] = email.utils.localtime()
      msg.set_content(mail_body)

79
80
81
      mbox.add(msg)
      mbox.flush()

scmalte's avatar
scmalte committed
82
83
      break

84
85
86
  finally:
    mbox.unlock()

87

88
89
if __name__ == "__main__":
  main()