Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mossutils
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
dinfk-lecturers
mossutils
Commits
376b6002
Commit
376b6002
authored
May 07, 2020
by
scmalte
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mails.py: continued experimenting, trying content encodings
parent
bd514b9e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
25 deletions
+44
-25
mossutils/mails.py
mossutils/mails.py
+44
-25
No files found.
mossutils/mails.py
View file @
376b6002
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).
\n
There 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
()
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