import smtplib import ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from JsonHelper import ItemElement from typing import List sender_email = "youremail@gmail.com" # receiver_email = "test@protonmail.com" password = "yourpassword" Profiles = { "tset@protonmail.com": {"9697s","61571s", "61570s", "51784s"}, "test2@gmail.com": {"39517s", "9047s", "10566s", "73681s", "9037s", "9090s", "9038s", "75096s", "63149s", "42496s", "9084s"}, "test3@outlook.com": {"9626s", "61571s", "61570s", "48073s", "51784s", "52612s"} } ''' 9697s = "Bakkerij 't Bakkertje - Steenweg" 39517s = "IJssalon Kees - Eindhoven" 9084s = "Banketbakkerij Renders" 9047s = "backWERK - Eindhoven" 10566s = "La Place - Eindhoven Piazza (144421)" 73681s = "Jamin - Eindhoven centrum" 9037s = "Brood2day" 9090s = "La Semolina" 9038s = "SPAR City - Eindhoven" 75096s = "Houben Worstenbrood" 9626s = "Bakkerij 't Bakkertje - Geysendorfferstraat" 51784s = "HEMA - Helmond" 52612s = "Sugar Sweet Shop - Helmond" 61571s = "Jan Linders - Helmond (Mierlo-Hout)" 61570s = "Jan Linders - Helmond" 48073s = "Groente & Fruit Hans Deelen" 63149s = "The Student Hotel Eindhoven" 42496s = "La Place - Eindhoven Heuvel (144412)" 10145s = "Monkey Coffee - Station Eindhoven" 9030s = "Seasons" "chrisdebont@protonmail.com": {"9697s", "61570s", "52612s", "51784s", "48073s", "10145s", "9030s", "61571s", "63149s", "63149s"}, ''' def item_is_valid(item: ItemElement, valid_stores: List[str]): return item.store.store_id in valid_stores def filter_items(items: List[ItemElement], valid_stores: List[str]): output = [] for item in items: if item_is_valid(item, valid_stores): output.append(item) return output def send_email_alerts(items: List[ItemElement]): for email, validStores in Profiles.items(): # any store, == {} if len(validStores) == 0: send_email(email, items) continue filtered = filter_items(items, validStores) if len(filtered) > 0: send_email(email, filtered) def items_to_string(items: List[ItemElement]): output = "" for item in items: output += f"{item.items_available}x{item.display_name}\n" # ({item.store.store_name}) [{item.store.store_id}]\n" return output def send_email(email: str, items: List[ItemElement]): message = MIMEMultipart("alternative") message["Subject"] = "TooGoodToGo Alert!" message["From"] = sender_email message["To"] = email # Turn these into plain/html MIMEText objects text = items_to_string(items) part1 = MIMEText(text, "plain") message.attach(part1) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail( sender_email, email, message.as_string() )