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 376b6002 authored by scmalte's avatar scmalte
Browse files

mails.py: continued experimenting, trying content encodings

parent bd514b9e
import logging import logging
import mailbox import mailbox
# import quopri
import os import os
import pathlib import pathlib
import glob
import csv
from .utils import logging as logutils 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_STUDENTS_RECEIVE_MAILS_AS_HEADER="To" # To, CC, BCC
DEFAULT_SENDER="malte.schwerhoff@inf.ethz.ch"
DEFAULT_SUBJECT="You've been caught!"
def main( 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, 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() logutils.configure_level_and_format()
unsent_mbox = "D:\\Program_Data\\Thunderbird Portable\\Data\\profile\\Mail\\Local Folders\\Unsent Messages" unsent_mbox = "D:\\Program_Data\\Thunderbird Portable\\Data\\profile\\Mail\\Local Folders\\Unsent Messages"
...@@ -18,9 +30,9 @@ def main( ...@@ -18,9 +30,9 @@ def main(
mbox_msf_path = pathlib.Path(unsent_mbox + ".msf") mbox_msf_path = pathlib.Path(unsent_mbox + ".msf")
logging.warn("ATTENTION: If you continue, the files") logging.warn("ATTENTION: If you continue, the files")
logging.warn(" {}") logging.warn(" {}".format(mbox_path))
logging.warn(" {}") logging.warn(" {}".format(mbox_msf_path))
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("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"): if (input("Do you want to continue (enter 'yes')? ").lower() != "yes"):
logging.debug("Exiting program because user did not confirm to continue") logging.debug("Exiting program because user did not confirm to continue")
...@@ -36,27 +48,34 @@ def main( ...@@ -36,27 +48,34 @@ def main(
mbox = mailbox.mbox(mbox_path) mbox = mailbox.mbox(mbox_path)
mbox.lock() mbox.lock()
try: try:
msg = mailbox.mboxMessage() cluster_students_glob = os.path.join(cluster_files_dir, cluster_students_csv_file_glob)
msg.set_unixfrom("author Sat Feb 7 01:05:34 2009")
msg["From"] = "malte@schwerhoff.de" logging.info("Taking students per cluster from files {}".format(cluster_students_glob))
msg["To"] = "scmalte@inf.ethz.ch, malte.schwerhoff@inf.ethz.h"
msg["Subject"] = "Sample message 1" for students_per_cluster_csv in glob.iglob(cluster_students_glob):
msg.set_payload("This is the body.\\nFrom (should be escaped).\nThere are 3 lines.\n") with open(students_per_cluster_csv, newline='') as csv_fh:
mbox.add(msg) cluster_csv = list(csv.DictReader(csv_fh))
mbox.flush()
recipients = ", ".join([row["Email"] for row in cluster_csv])
logging.info("Creating mail to {}".format(recipients))
msg = mailbox.mboxMessage() msg = mailbox.mboxMessage()
msg.set_unixfrom("author") # msg.set_unixfrom("author Sat Feb 7 01:05:34 2009")
msg["From"] = "malte@schwerhoff.de" msg["From"] = sender
msg["To"] = "mun123456789@gmail.com" msg["Subject"] = subject
msg["CC"] = "scmalte@inf.ethz.ch" msg[students_receive_mails_as_header] = recipients
msg["BCC"] = "malte.schwerhoff@inf.ethz.h" # msg["Content-Transfer-Encoding"] = "quoted-printable"
msg["Subject"] = "Sample message 2" 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.set_payload(mail_body)
mbox.add(msg) mbox.add(msg)
mbox.flush() mbox.flush()
finally: finally:
mbox.unlock() mbox.unlock()
if __name__ == "__main__": if __name__ == "__main__":
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