-
Notifications
You must be signed in to change notification settings - Fork 0
/
emailHandler.py
33 lines (27 loc) · 1.01 KB
/
emailHandler.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
import os
import smtplib
from db import getEmail
from email.mime.text import MIMEText
from dotenv import load_dotenv
# Load environment
load_dotenv()
# Get Gmail app pass key from .env file
password = os.environ.get("gmail_pass_key")
def send_email(value, treshold, measurementTime, overOrUnder):
# Define email template
subject = "PiBrewPal Temperature Alert!"
body = f"Hello, PiBrewPal wants to inform you about temperature which went {overOrUnder} the value of {treshold}°C:\n{measurementTime} | {value}°C"
sender = "pibrewpal@gmail.com"
# Get user email from database
recipient_email = getEmail()[0]
recipients = [recipient_email]
# Set email variables
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = ', '.join(recipients)
# Connect to smtp gmail server
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server:
smtp_server.login(sender, password)
smtp_server.sendmail(sender, recipients, msg.as_string())
print("Message sent!")