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

Commit cc48651e authored by scmalte's avatar scmalte
Browse files

mails.py: added CLI option for specifying sender address, subject, and Mbox file to target

parent a0731ec9
......@@ -52,6 +52,12 @@ Argument `--upgrade` (re)installs the package, even if the version number hasn't
* eDoz exports: *Communication/List**Course units**<Your course>**Export data (Zip/Text)*, then extract CSV (`.txt`) file from downloaded ZIP file. Pass files to `mu-aggr` via the mandatory `--edoz-exports` option.
1. Optional: run `mu-mails`.
**NOTE:** `mu-mails` is not yet *fully* configurable and expects `mu-aggr` to have used its default file and directory names! Run `mu-mails --help` for options that must/can already be configured.
`mu-mails` creates an e-mail per identified cluster, from the template file `_static/cluster_mail.txt` (e.g. `~/moss_ex1/_static/cluster_mail.txt`). The e-mails are written to an Mbox file, as used by Thunderbird. If you pass your local Thunderbird outbox' unsent messages file to `mu-mails` via the mandatory `--mbox-file` option (e.g. `~/thunderbird/xxxxxxxx.profile/Mail/Local Folders/Unsent Messages`), then you should afterwards be able to directly send the mails from inside Thunderbird via *File**Send Unsent Messages*. You can also review and edit the messages beforehand, they should be visible in the *Outbox* folder of your *Local Folders* account.
## Python Package Tutorials
I've used the following tutorials to get started with developing a Python package:
......
import logging
import mailbox
import email
import argparse
from email.message import EmailMessage
from email.headerregistry import Address
# import quopri
......@@ -9,29 +10,27 @@ import pathlib
import glob
import csv
import datetime
import re
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_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_address=DEFAULT_SENDER_ADDRESS,
subject=DEFAULT_SUBJECT):
def create_emails(
cluster_files_dir,
cluster_students_csv_file_glob,
mail_body_file,
students_receive_mails_as_header,
sender_address,
subject,
mbox_filename):
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")
mbox_path = pathlib.Path(mbox_filename)
mbox_msf_path = pathlib.Path(mbox_filename + ".msf")
logging.warn("ATTENTION: If you continue, the files")
logging.warn(" {}".format(mbox_path))
......@@ -55,10 +54,12 @@ def main(
mbox.lock()
try:
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):
cluster_csv_files = glob.glob(cluster_students_glob)
logging.info("Found {} matching files".format(len(cluster_csv_files)))
for students_per_cluster_csv in cluster_csv_files:
with open(students_per_cluster_csv, newline='') as csv_fh:
cluster_csv = list(csv.DictReader(csv_fh))
......@@ -79,11 +80,64 @@ def main(
mbox.add(msg)
mbox.flush()
break
finally:
mbox.unlock()
def configure_cli_parser(parser):
parser.add_argument(
"-f", "--from",
dest="sender", # Create attribute 'sender', since 'from' is a keyword
type=str,
help="E-mail sender name and address, as 'Name <user@server.tld>'",
required=True)
parser.add_argument(
"-s", "--subject",
type=str,
help="E-mail subject",
required=True)
parser.add_argument(
"-m", "--mbox-file",
type=str,
help="Target MBox file for e-mails to create. E.g. 'C:\\Program Files\\Thunderbird\\Data\\profile\\Mail\\Local Folders\\Unsent Messages'.",
required=True)
logutils.add_loglevel_argument(parser)
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):
parser = argparse.ArgumentParser()
configure_cli_parser(parser)
args = parser.parse_args()
logutils.configure_level_and_format(args.log_level)
sender_pattern = r"\s*(.+?)\s*<(.+?)@(.+?)>"
sender_match = re.search(sender_pattern, args.sender)
assert \
sender_match and sender_match.lastindex == 3, \
"Argument provided for --format does not match expected pattern"
sender_address = Address(*sender_match.groups())
create_emails(
cluster_files_dir,
cluster_students_csv_file_glob,
mail_body_file,
students_receive_mails_as_header,
sender_address,
args.subject,
args.mbox_file)
if __name__ == "__main__":
main()
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