Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dinfk-lecturers
mossutils
Commits
de0d7624
Commit
de0d7624
authored
May 08, 2020
by
scmalte
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mails.py: consolidating experiments
parent
376b6002
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
17 deletions
+25
-17
mossutils/data/cluster_mail.txt
mossutils/data/cluster_mail.txt
+1
-1
mossutils/mails.py
mossutils/mails.py
+24
-16
No files found.
mossutils/data/cluster_mail.txt
View file @
de0d7624
Sehr geehrte Studierende
Sehr geehrte Studierende
Sie sind beim Kopieren der Bonusaufgabe erwischt worden. Das ist höchst unerfreulich — für Sie! Denn nun müssen wir Ihnen leider die Punkte wieder abziehen.
...
...
mossutils/mails.py
View file @
de0d7624
import
logging
import
mailbox
import
email
from
email.message
import
EmailMessage
from
email.headerregistry
import
Address
# import quopri
import
os
import
pathlib
import
glob
import
csv
import
datetime
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
=
"malte.schwerhoff
@
inf.ethz.ch"
DEFAULT_SUBJECT
=
"You've been caught!"
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
=
DEFAULT_SENDER
,
sender
_address
=
DEFAULT_SENDER
_ADDRESS
,
subject
=
DEFAULT_SUBJECT
):
logutils
.
configure_level_and_format
()
...
...
@@ -42,7 +46,9 @@ def main(
mbox_path
.
unlink
(
missing_ok
=
True
)
mbox_msf_path
.
unlink
(
missing_ok
=
True
)
with
open
(
mail_body_file
)
as
body_fh
:
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
:
mail_body
=
body_fh
.
read
()
mbox
=
mailbox
.
mbox
(
mbox_path
)
...
...
@@ -56,23 +62,25 @@ def main(
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
))
recipients
=
[
Address
(
addr_spec
=
row
[
"Email"
])
for
row
in
cluster_csv
]
msg
=
mailbox
.
mboxMessage
()
# msg.set_unixfrom("author Sat Feb 7 01:05:34 2009")
msg
[
"From"
]
=
sender
# 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
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
)
msg
[
'Date'
]
=
email
.
utils
.
localtime
()
msg
.
set_content
(
mail_body
)
mbox
.
add
(
msg
)
mbox
.
flush
()
break
finally
:
mbox
.
unlock
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment