diff --git a/mailer/bilf-mail-council.html b/mailer/bilf-mail-council.html new file mode 100644 index 00000000..6f39aa43 --- /dev/null +++ b/mailer/bilf-mail-council.html @@ -0,0 +1,59 @@ + + + + + Ny kollegie-bilbokning {{ booking.date }} + + +
+ This email is available in Swedish above and English below / Detta mail finns på svenska ovan och engelska nedan +
+ + + + + + + +
+ F-sektionen ikon +
+ + + + +
+

Hej Bilf!,

+

En ny kollegie-bilbokning har gjorts!

+

F-älgen är bokad av {{ booking.name }} från {{ booking.council_sv }} den {{ booking.date }} klockan {{ booking.time }}. Detta bör hantera sig självt men vi tänkte att du kanske vill veta./p> +

Med Vänliga hälsningar,
+ De små tomtarna som sitter i sektionens SMTP-server och matar brevduvorna

+
+ + +
+ + + + + + + +
+ F-sektionen icon +
+ + + + +
+

Hello BilF!,

+

A new council booking has been made!

+

The car is booked by {{ booking.name }} from {{ booking.council_en }} on {{ booking.date }} at {{ booking.time}}. It should handle itself but we though you migh want to know.

+

Best regards,
+ The little gnomes that inhabit the guilds SMTP server and feed the carrier pidgeons

+
+ + diff --git a/mailer/bilf-mail-private.html b/mailer/bilf-mail-private.html new file mode 100644 index 00000000..8bf565ae --- /dev/null +++ b/mailer/bilf-mail-private.html @@ -0,0 +1,59 @@ + + + + + Ny privat bilbokning {{ booking.date }} + + +
+ This email is available in Swedish above and English below / Detta mail finns på svenska ovan och engelska nedan +
+ + + + + + + +
+ F-sektionen ikon +
+ + + + +
+

Hej Bilf!,

+

En ny PRIVAT bilbokning har gjorts!

+

F-älgen är bokad av {{ booking.name }} den {{ booking.date }} klockan {{ booking.time }}. HANTERA!

+

Med Vänliga hälsningar,
+ De små tomtarna som sitter i sektionens SMTP-server och matar brevduvorna

+
+ + +
+ + + + + + + +
+ F-sektionen icon +
+ + + + +
+

Hello BilF!,

+

A new PRIVATE booking has been made!

+

The car is booked by {{ booking.name }} on {{ booking.date }} at {{ booking.time}}. TAKE CARE OF IT!

+

Best regards,
+ The little gnomes that inhabit the guilds SMTP server and feed the carrier pidgeons

+
+ + diff --git a/mailer/bilf_mailer.py b/mailer/bilf_mailer.py new file mode 100644 index 00000000..e70fb698 --- /dev/null +++ b/mailer/bilf_mailer.py @@ -0,0 +1,50 @@ +import os +import html as python_html +from zoneinfo import ZoneInfo + + +from email.mime.text import MIMEText +from db_models.car_booking_model import CarBooking_DB +from mailer.mail_constants import ( + STANDARD_SENDER, +) +from mailer.mail_core import send_mail_to_address + + +def bilf_mailer(booking: CarBooking_DB) -> None: + + path = os.getcwd() + + if booking.personal: + with open(f"{path}/mailer/bilf-mail-private.html", "r", encoding="utf-8") as f: + html = f.read() + else: + with open(f"{path}/mailer/bilf-mail-council.html", "r", encoding="utf-8") as f: + html = f.read() + + stockholm_tz = ZoneInfo("Europe/Stockholm") + date_string = booking.start_time.astimezone(stockholm_tz).strftime("%Y-%m-%d") + time_string = booking.start_time.astimezone(stockholm_tz).strftime("%H:%M") + + html = html.replace( + "{{ booking.name }}", python_html.escape(booking.user.first_name + " " + booking.user.last_name, quote=True) + ) + + html = html.replace("{{ booking.date }}", date_string) + + html = html.replace("{{ booking.time }}", time_string) + + if booking.council is not None: + html = html.replace("{{ booking.council_en }}", python_html.escape(booking.council.name_en, quote=True)) + html = html.replace("{{ booking.council_sv }}", python_html.escape(booking.council.name_sv, quote=True)) + + msg = MIMEText(html, "html", "utf-8") + + msg["From"] = STANDARD_SENDER + msg["To"] = "bil@fsektionen.se" + if booking.personal: + msg["Subject"] = "Ny PRIVAT bilbokning / New PRIVATE car booking" + else: + msg["Subject"] = "Ny kollegie-bilbokning / New council car booking" + + send_mail_to_address("bil@fsektionen.se", msg) diff --git a/mailer/mail_core.py b/mailer/mail_core.py index a35f5cd3..78084027 100644 --- a/mailer/mail_core.py +++ b/mailer/mail_core.py @@ -2,6 +2,8 @@ import os import smtplib +from pydantic import EmailStr + from mailer.mail_constants import SMTP_SERVER, STANDARD_SENDER from db_models.user_model import User_DB @@ -21,3 +23,20 @@ def send_mail(user: User_DB, msg: MIMEText): print(f"Email sent successfully to {user.first_name}") except Exception as e: print(f"Failed to send email: {e}") + + +def send_mail_to_address(adress: EmailStr, msg: MIMEText): + env = os.getenv("ENVIRONMENT") + + if env != "production" and env != "stage": + print("Email cannot be used on testing") + return + + try: + # Create an SMTP session + with smtplib.SMTP(SMTP_SERVER, smtplib.SMTP_PORT) as server: + server.starttls() + server.sendmail(STANDARD_SENDER, adress, msg.as_string()) + print(f"Email sent successfully to adress {adress}") + except Exception as e: + print(f"Failed to send email: {e}") diff --git a/services/car_renting_service.py b/services/car_renting_service.py index c14ae48a..ea09c4de 100644 --- a/services/car_renting_service.py +++ b/services/car_renting_service.py @@ -9,6 +9,7 @@ from db_models.car_booking_model import CarBooking_DB from datetime import UTC, datetime from db_models.car_block_model import CarBlock_DB +from mailer import bilf_mailer def is_user_blocked(user_id: int, db: DB_dependency) -> bool: @@ -150,6 +151,8 @@ def create_new_booking( db.add(db_booking) db.commit() + bilf_mailer.bilf_mailer(db_booking) + return db_booking