import json import sys import yaml import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import os sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) from libs.utils import utils utils.move_files_back_from_old_files() STATIC_PATH = "../../groupsData/shx-order-voucher-code-activated/" json_object = json.loads(sys.argv[1]) customer_email = json_object["customer_email"] voucher_code = json_object["voucher_code"] voucher_type = json_object["voucher_type"] if (customer_email is None or voucher_code is None or voucher_type is None): print("Missing required parameters") sys.exit(1) customer_email = customer_email["value"] voucher_code = voucher_code["value"] voucher_type = voucher_type["value"] def load_email_config(filename): with open(filename, "r") as f: config = yaml.safe_load(f) return config["email"], config["smtp"] email_config, smtp_config = load_email_config(f"{STATIC_PATH}config.yml") # extract the email parameters auth_email = email_config["auth_email"] from_email_name = email_config["from_email_name"] from_email = email_config["from_email"] password = email_config["password"] # extract the smtp parameters smtp_server = smtp_config["server"] smtp_port = smtp_config["port"] receiver_email = customer_email # create the email multipart message msg = MIMEMultipart("alternative") msg["From"] = f"{from_email_name} <{from_email}>" msg["To"] = receiver_email msg["Subject"] = f"{voucher_type} Gutschein als Dankeschön" # load html content from file with open(f"{STATIC_PATH}email.txt", "r") as file: text = file.read() with open(f"{STATIC_PATH}email.html", "r") as file: html = file.read() # replace placeholders with actual values if voucher_type == "5 €": message = "Wir möchten uns herzlich bei dir bedanken, dass du dir die Zeit genommen hast, uns zu bewerten. Deine Meinung ist uns sehr wichtig und hilft uns, unseren Service stetig zu verbessern." price = "5" else: message = "Wir möchten uns herzlich bei dir bedanken, dass du dir die Zeit genommen hast, einen Beitrag über uns auf Social Media zu erstellen." price = "10" html = html.replace("{{PRICE}}", price) html = html.replace("{{MESSAGE}}", message) html = html.replace("{{VOUCHER_CODE}}", voucher_code) text = text.replace("{{MESSAGE}}", message) text = text.replace("{{VOUCHER_CODE}}", voucher_code) # add text and html part to the email part1 = MIMEText(text, "plain", "utf-8") part2 = MIMEText(html, "html", "utf-8") # Attach the text and html part to the email msg.attach(part1) msg.attach(part2) # connect to the smtp server and send the email try: with smtplib.SMTP_SSL(smtp_server, smtp_port) as server: server.login(auth_email, password) server.sendmail(from_email, receiver_email, msg.as_string()) print(f"Email sent to {receiver_email} with voucher code {voucher_code} of type {voucher_type}") except Exception as e: print(f"Error sending email: {e}") sys.exit(1)