forked from dholendar-27/aw-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_report.py
97 lines (76 loc) · 2.64 KB
/
email_report.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
"""
Sends an email with a summary report (WIP)
For now, it just sends stdin.
In the future, it will generate a pretty email and send that.
Requires that an SMTP server is configured through environment variables.
Example usage:
$ env OUTPUT_HTML=true python3 examples/working_hours.py | python3 examples/email_report.py
"""
import smtplib
import os
import sys
from dataclasses import dataclass
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
@dataclass
class Recipient:
name: str
email: str
def create_msg(
sender: Recipient,
receiver: Recipient,
subject: str,
text: str,
html=None,
) -> MIMEMultipart:
"""Based on https://stackoverflow.com/a/882770/965332"""
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
msg["From"] = sender.email
msg["To"] = receiver.email
# Record the MIME types of both parts - text/plain and text/html.
# Also attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(MIMEText(text, "plain"))
if html:
msg.attach(MIMEText(html, "html"))
return msg
def main(read_stdin=True) -> None:
smtp_server = os.environ["SMTP_SERVER"].strip()
smtp_username = os.environ["SMTP_USERNAME"].strip()
smtp_password = os.environ["SMTP_PASSWORD"].strip()
assert smtp_server, "Environment variable SMTP_SERVER not set"
assert smtp_username, "Environment variable SMTP_USERNAME not set"
assert smtp_password, "Environment variable SMTP_PASSWORD not set"
sender = Recipient("ActivityWatch (automated script)", "[email protected]")
receiver = Recipient("Erik Bjäreholt", "[email protected]")
if read_stdin:
# Accepts input from stdin
text = sys.stdin.read()
else:
text = "Just a test. ActivityWatch stats will go here."
text = text.replace("\n\n", "<hr>")
# text = text.replace("\n\n", "<br>")
# Create the body of the message (a plain-text and an HTML version).
html = f"""\
<html>
<head></head>
<body>
<p>{text}</p>
</body>
</html>
"""
subject = "Example report from aw-client"
msg = create_msg(sender, receiver, subject, text, html)
try:
with smtplib.SMTP_SSL(smtp_server, 465) as smtp:
smtp.login(smtp_username, smtp_password)
smtp.send_message(msg)
smtp.quit()
print("Successfully sent email")
except smtplib.SMTPException as e:
print("Error: unable to send email")
print(e)
if __name__ == "__main__":
main()