Commit 376b6002 authored by scmalte's avatar scmalte

mails.py: continued experimenting, trying content encodings

parent bd514b9e
import logging
import mailbox
# import quopri
import os
import pathlib
import pathlib
import glob
import csv
from .utils import logging as logutils
DEFAULT_MAIL_BODY_FILE="mail_per_cluster.txt"
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!"
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):
students_receive_mails_as_header=DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER,
sender=DEFAULT_SENDER,
subject=DEFAULT_SUBJECT):
logutils.configure_level_and_format()
unsent_mbox = "D:\\Program_Data\\Thunderbird Portable\\Data\\profile\\Mail\\Local Folders\\Unsent Messages"
......@@ -18,9 +30,9 @@ def main(
mbox_msf_path = pathlib.Path(unsent_mbox + ".msf")
logging.warn("ATTENTION: If you continue, the files")
logging.warn(" {}")
logging.warn(" {}")
logging.warn("will be overwritten, if they exist. Make sure that do not have any pending unsent messages!".format(mbox_path, mbox_msf_path))
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!")
if (input("Do you want to continue (enter 'yes')? ").lower() != "yes"):
logging.debug("Exiting program because user did not confirm to continue")
......@@ -36,27 +48,34 @@ def main(
mbox = mailbox.mbox(mbox_path)
mbox.lock()
try:
msg = mailbox.mboxMessage()
msg.set_unixfrom("author Sat Feb 7 01:05:34 2009")
msg["From"] = "malte@schwerhoff.de"
msg["To"] = "scmalte@inf.ethz.ch, malte.schwerhoff@inf.ethz.h"
msg["Subject"] = "Sample message 1"
msg.set_payload("This is the body.\\nFrom (should be escaped).\nThere are 3 lines.\n")
mbox.add(msg)
mbox.flush()
msg = mailbox.mboxMessage()
msg.set_unixfrom("author")
msg["From"] = "malte@schwerhoff.de"
msg["To"] = "mun123456789@gmail.com"
msg["CC"] = "scmalte@inf.ethz.ch"
msg["BCC"] = "malte.schwerhoff@inf.ethz.h"
msg["Subject"] = "Sample message 2"
msg.set_payload(mail_body)
mbox.add(msg)
mbox.flush()
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()
finally:
mbox.unlock()
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