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 1.97 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import logging
import mailbox
import os
import pathlib 
from .utils import logging as logutils

DEFAULT_MAIL_BODY_FILE="mail_per_cluster.txt"
DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER="To" # To, CC, BCC

def main(
    mail_body_file=DEFAULT_MAIL_BODY_FILE,
    students_receive_mails_as_header=DEFAULT_STUDENTS_RECEIVE_MAILS_AS_HEADER):
  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
20
21
22
23
  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))
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  
  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:
    msg = mailbox.mboxMessage()
scmalte's avatar
scmalte committed
40
41
42
43
44
    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")
45
46
47
48
    mbox.add(msg)
    mbox.flush()

    msg = mailbox.mboxMessage()
scmalte's avatar
scmalte committed
49
50
51
52
53
54
    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"
55
56
57
58
59
60
61
62
    msg.set_payload(mail_body)
    mbox.add(msg)
    mbox.flush()
  finally:
    mbox.unlock()

if __name__ == "__main__":
  main()