|
| 1 | +""" |
| 2 | +Sends an email with a summary report (WIP) |
| 3 | +
|
| 4 | +For now, it just sends stdin. |
| 5 | +In the future, it will generate a pretty email and send that. |
| 6 | +
|
| 7 | +Requires that an SMTP server is configured through environment variables. |
| 8 | +
|
| 9 | +Example usage: |
| 10 | +
|
| 11 | + $ env OUTPUT_HTML=true python3 examples/working_hours.py | python3 examples/email_report.py |
| 12 | +""" |
| 13 | + |
| 14 | +import smtplib |
| 15 | +import os |
| 16 | +import sys |
| 17 | +from dataclasses import dataclass |
| 18 | +from email.mime.text import MIMEText |
| 19 | +from email.mime.multipart import MIMEMultipart |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class Recipient: |
| 24 | + name: str |
| 25 | + email: str |
| 26 | + |
| 27 | + |
| 28 | +def create_msg( |
| 29 | + sender: Recipient, |
| 30 | + receiver: Recipient, |
| 31 | + subject: str, |
| 32 | + text: str, |
| 33 | + html=None, |
| 34 | +) -> MIMEMultipart: |
| 35 | + """Based on https://stackoverflow.com/a/882770/965332""" |
| 36 | + msg = MIMEMultipart("alternative") |
| 37 | + msg["Subject"] = subject |
| 38 | + msg["From"] = sender.email |
| 39 | + msg["To"] = receiver.email |
| 40 | + |
| 41 | + # Record the MIME types of both parts - text/plain and text/html. |
| 42 | + # Also attach parts into message container. |
| 43 | + # According to RFC 2046, the last part of a multipart message, in this case |
| 44 | + # the HTML message, is best and preferred. |
| 45 | + msg.attach(MIMEText(text, "plain")) |
| 46 | + if html: |
| 47 | + msg.attach(MIMEText(html, "html")) |
| 48 | + |
| 49 | + return msg |
| 50 | + |
| 51 | + |
| 52 | +def main(read_stdin=True) -> None: |
| 53 | + smtp_server = os.environ["SMTP_SERVER"].strip() |
| 54 | + smtp_username = os.environ["SMTP_USERNAME"].strip() |
| 55 | + smtp_password = os.environ["SMTP_PASSWORD"].strip() |
| 56 | + |
| 57 | + assert smtp_server, "Enviroment variable SMTP_SERVER not set" |
| 58 | + assert smtp_username, "Enviroment variable SMTP_USERNAME not set" |
| 59 | + assert smtp_password, "Enviroment variable SMTP_PASSWORD not set" |
| 60 | + |
| 61 | + sender = Recipient("ActivityWatch (automated script)", "noreply@activitywatch.net") |
| 62 | + receiver = Recipient("Erik Bjäreholt", "erik.bjareholt@gmail.com") |
| 63 | + |
| 64 | + if read_stdin: |
| 65 | + # Accepts input from stdin |
| 66 | + text = sys.stdin.read() |
| 67 | + else: |
| 68 | + text = "Just a test. ActivityWatch stats will go here." |
| 69 | + |
| 70 | + text = text.replace("\n\n", "<hr>") |
| 71 | + # text = text.replace("\n\n", "<br>") |
| 72 | + # Create the body of the message (a plain-text and an HTML version). |
| 73 | + html = f"""\ |
| 74 | + <html> |
| 75 | + <head></head> |
| 76 | + <body> |
| 77 | + <p>{text}</p> |
| 78 | + </body> |
| 79 | + </html> |
| 80 | + """ |
| 81 | + |
| 82 | + subject = "Example report from aw-client" |
| 83 | + msg = create_msg(sender, receiver, subject, text, html) |
| 84 | + |
| 85 | + try: |
| 86 | + with smtplib.SMTP_SSL(smtp_server, 465) as smtp: |
| 87 | + smtp.login(smtp_username, smtp_password) |
| 88 | + smtp.send_message(msg) |
| 89 | + smtp.quit() |
| 90 | + print("Successfully sent email") |
| 91 | + except smtplib.SMTPException as e: |
| 92 | + print("Error: unable to send email") |
| 93 | + print(e) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments