admin-dashboard-backend/groupTasks/groups/shx-order-voucher-code-acti.../script.py

96 lines
2.9 KiB
Python

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_type = json_object["voucher_type"]
if customer_email is None or voucher_type is None:
print("Missing required parameters")
sys.exit(1)
customer_email = customer_email["value"]
voucher_type = voucher_type["value"]
secrets = utils.get_secrets()["email"]
# extract the email parameters
auth_email = secrets["auth_email"]
from_email_name = secrets["from_email_name"]
from_email = secrets["from_email"]
password = secrets["password"]
# extract the smtp parameters
smtp_server = secrets["smtp_server"]
smtp_port = secrets["smtp_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"
discount_code = utils.create_shopify_discount_code("order_voucher_5_euro")
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"
discount_code = utils.create_shopify_discount_code("order_voucher_10_euro")
if discount_code == "":
sys.exit("Discount code is empty")
html = html.replace("{{PRICE}}", price)
html = html.replace("{{MESSAGE}}", message)
html = html.replace("{{DISCOUNT_CODE}}", discount_code)
text = text.replace("{{PRICE}}", price)
text = text.replace("{{MESSAGE}}", message)
text = text.replace("{{DISCOUNT_CODE}}", discount_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 {discount_code} of type {voucher_type}")
except Exception as e:
print(f"Error sending email: {e}")
sys.exit(1)