-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDraftEmail.py
More file actions
66 lines (55 loc) · 2.34 KB
/
DraftEmail.py
File metadata and controls
66 lines (55 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from email import generator
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
def DraftEmail(subject=None, emailto=None, body=[], fname='DraftEmail.eml',emailfrom='[email protected]',attach=0):
"""
% (C) Nick Holschuh - Amherst College - 2022 ([email protected])
% This function produces a .eml file that can be added to drafts in
% Thunderbird to evventually be sent
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The inputs are as follows:
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The outputs are as follows:
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
"""
################################## Print an example email if you aren't sure how to structure things.
if len(body) == 0:
html_data = """\
\"\"\"
<html>
<head></head>
<body>
<p> Hi Nick</p>
<p> Congratulations on finishing the first part of the exam process! Below, I provide instructions for part two: exam revisions.</p><p> Your original responses to each question could fall into one of three categories: <p>
<ul>
<li><em>Minor Revisions</em> -- it's clear you were thinking about things correctly, but your answer is imprecise or could otherwise be refined.</li>
<li><em>Major Revisions</em> -- an important idea was missing in your response.</li>
</ul>
\"\"\"
"""
print(html_data)
else:
if fname[-3:] != 'eml':
fname = fname+'.eml'
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = emailfrom
msg['To'] = emailto
if '<' in body:
part = MIMEText(body, 'html')
else:
part = MIMEText(body, 'plain')
msg.attach(part)
if isinstance(attach,str):
with open(attach, 'rb') as f:
pdf_part = MIMEApplication(f.read(), _subtype='pdf')
pdf_part.add_header('Content-Disposition', 'attachment', filename=attach.split('/')[-1])
msg.attach(pdf_part)
with open(fname, 'w') as outfile:
gen = generator.Generator(outfile)
gen.flatten(msg)