-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathst_email.py
234 lines (190 loc) · 7.05 KB
/
st_email.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
import re
import time
def htmlfy_b(text):
"""Returns html bold text.
"""
b = "<b>{}</b>".format(text)
return(b)
def htmlfy_i(text):
"""Returns html italic text.
"""
i = "<i>{}</i>".format(text)
return(i)
def htmlfy_a(text, url):
"""Returns html hyperlink.
"""
a = '<a href="{}">{}</a>'.format(url, text)
return(a)
def htmlfy_p(text):
"""Returns html paragraph.
"""
p = "<p>{}</p>".format(text)
return(p)
def htmlfy_h3(text):
"""Returns html heading 3.
"""
h3 = "<h3>{}</h3>".format(text)
return(h3)
def htmlfy_h4(text):
"""Returns html heading 4.
"""
h3 = "<h4>{}</h4>".format(text)
return(h3)
def htmlfy_br(text):
"""Returns html breakline.
"""
br = "<br>{}</br>".format(text)
return(br)
def htmlfy_li(textlist):
"""Returns html unordered list.
"""
u_list_items = ['<li style="margin-top: 0.5em">{}</li>'.format(item) for item in textlist]
u_list = ['<ul style="list-style-type: square; margin-left: 1em">']
for item in u_list_items:
u_list.append(item)
u_list.append("</ul>")
list_html = ""
for item in u_list:
list_html += item
return(list_html)
def htmlfy_table(rowslist):
"""Returns html table.
"""
table = ['<table style="margin-left: 1em">']
for rows in rowslist:
table.append(rows)
table.append('</table>')
return(table)
def format_to_html():
"""Formats text files for html output.
"""
hr_line = '<p> </p><hr width="5%"><p> </p>'
# format summary files
folder = os.listdir()
summary_files = [f for f in folder if f.startswith("s_")]
for f in summary_files:
with open(f, mode="r", encoding="utf8") as text_f:
with open("email_{}".format(f), mode="w", encoding="utf8") as email_f:
lines = text_f.read().splitlines()
lines = [line for line in lines if line != ""]
category = lines.pop(0)
headlines = [line[line.find(" ")+1:] for line in lines if line.startswith("#")]
urls = [line for line in lines if line.startswith("http")]
print(htmlfy_h3(category), file=email_f)
print(htmlfy_li(headlines), file=email_f)
print(htmlfy_br(""), file=email_f)
for line in lines:
if line.startswith("#"):
line = line[line.find(" ")+1:]
line = htmlfy_h4(htmlfy_a(line.upper(), urls.pop(0)))
print(line, file=email_f)
elif line.startswith("http") or line.startswith("=="):
continue
elif line == "***":
print(hr_line, file=email_f)
else:
line = htmlfy_p(line)
print(line, file=email_f)
# format report file
f = "ST_News-Headlines.txt"
with open(f, mode="r", encoding="utf8") as text_f:
with open("email_{}".format(f), mode="w", encoding="utf8") as email_f:
lines = text_f.read().splitlines()
lines = [line for line in lines if line != ""]
urls = [line for line in lines if line.startswith("http")]
date = lines.pop(0)
print(htmlfy_h3(date), file=email_f)
table_data = []
for line in lines:
if line.isupper():
line = htmlfy_b(line)
line = '<th align="left">{}</th>'.format(line)
table_data.append(line)
elif line.startswith("["):
line = re.search("\[(.*)\].[#]\d*.(.*)", line)
if line:
url = urls.pop(0)
date = line.group(1)
headline = htmlfy_a(htmlfy_i(line.group(2)), url)
line = '<td valign="top">{}</td><td valign="top" style="margin-left: 3em">{}</td>'.format(date, headline)
table_data.append(line)
elif line == "***":
table_data.append("<td> </td><td> </td>")
table_rows = ["<tr>{}</tr>".format(td) for td in table_data]
table = htmlfy_table(table_rows)
for t in table:
print(t, file=email_f)
def email(gmail_user, gmail_pwd, to, subject, text, attach):
"""Build email.
"""
msg = MIMEMultipart()
msg["From"] = gmail_user
msg["To"] = to
msg["Subject"] = subject
# email text
textfile = open(text, mode="r", encoding="utf8")
body = textfile.read()
msg.attach(MIMEText(body, "html")) # text is in html format
# email attachment
part = MIMEBase("application", "octet-stream")
part.set_payload(open(attach, mode="rb").read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
"attachment; filename='%s'" % os.path.basename(attach))
msg.attach(part)
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(gmail_user, gmail_pwd)
server.sendmail(gmail_user, to, msg.as_string())
server.quit()
def send_email(gmail_user, gmail_pwd, send_to):
"""Sends email.
"""
# format relevant file's content to html
print(" Formatting e-mails...")
format_to_html()
print(" E-mails formatted.")
# date today
date = time.strftime("%d %b %Y")
email_content = {
"0_headlines": (
"The Straits Times, {}".format(date),
"email_ST_News-Headlines.txt",
"email_ST_News-Headlines.txt")
}
folder = os.listdir()
email_count = 1
for f in folder:
if f.startswith("email_s"):
summary_file = f
original_file = summary_file.replace("email_s", "o")
cat = summary_file[11:summary_file.find(".")].replace("-", " ")
email_content.update({
"{}_{}".format(email_count, cat): (
"ST: {} Top Stories, {}".format(cat, date),
summary_file,
original_file)}
)
email_count += 1
print(" Sending e-mails.\n")
print(" E-mail recipients:")
for to in send_to:
print(f"\t{to}")
print("")
for to in send_to:
print(f" Sending e-mails to <{to}>...")
for (cat, content) in sorted(email_content.items())[::-1]:
(subject, text, attach) = content
email(gmail_user, gmail_pwd, to, subject, text, attach)
print(f"\t'{subject}' sent to <{to}>.")
print("")
print(" All e-mails sent.")