Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions mailer/bilf-mail-council.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Ny kollegie-bilbokning {{ booking.date }}</title>
</head>
<body style="font-family: sans-serif; color: #333; line-height: 1.5; margin:0; padding:0;">
<div style="text-align: center; padding: 10px; background-color: #f5f5f5; font-size: 14px; color: #666;">
This email is available in Swedish above and English below / Detta mail finns på svenska ovan och engelska nedan
</div>

<!-- Swedish Version -->
<!-- Header -->
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#FF6600;padding:20px 0;">
<tr>
<td align="center">
<img src="https://i.imgur.com/vajEXHm.png" alt="F-sektionen ikon" width="80"
style="display:block; height:auto; border:none; outline:none; text-decoration:none; -ms-interpolation-mode:bicubic;">
</td>
</tr>
</table>
<table width="100%" cellpadding="20" cellspacing="0">
<tr>
<td align="center">
<p>Hej Bilf!,</p>
<p>En ny kollegie-bilbokning har gjorts!</p>
<p>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>
<p>Med Vänliga hälsningar,<br>
De små tomtarna som sitter i sektionens SMTP-server och matar brevduvorna</p>
</td>
</tr>
</table>

<!-- Separator -->
<div style="height: 40px; background-color: #f5f5f5; border-top: 2px solid #ddd; border-bottom: 2px solid #ddd;"></div>

<!-- English Version -->
<!-- Header -->
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#FF6600;padding:20px 0;">
<tr>
<td align="center">
<img src="https://i.imgur.com/vajEXHm.png" alt="F-sektionen icon" width="80"
style="display:block; height:auto; border:none; outline:none; text-decoration:none; -ms-interpolation-mode:bicubic;">
</td>
</tr>
</table>
<table width="100%" cellpadding="20" cellspacing="0">
<tr>
<td align="center">
<p>Hello BilF!,</p>
<p>A new council booking has been made!</p>
<p>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.</p>
<p>Best regards,<br>
The little gnomes that inhabit the guilds SMTP server and feed the carrier pidgeons</p>
</td>
</tr>
</table>
</body>
</html>
59 changes: 59 additions & 0 deletions mailer/bilf-mail-private.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Ny privat bilbokning {{ booking.date }}</title>
</head>
<body style="font-family: sans-serif; color: #333; line-height: 1.5; margin:0; padding:0;">
<div style="text-align: center; padding: 10px; background-color: #f5f5f5; font-size: 14px; color: #666;">
This email is available in Swedish above and English below / Detta mail finns på svenska ovan och engelska nedan
</div>

<!-- Swedish Version -->
<!-- Header -->
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#FF6600;padding:20px 0;">
<tr>
<td align="center">
<img src="https://i.imgur.com/vajEXHm.png" alt="F-sektionen ikon" width="80"
style="display:block; height:auto; border:none; outline:none; text-decoration:none; -ms-interpolation-mode:bicubic;">
</td>
</tr>
</table>
<table width="100%" cellpadding="20" cellspacing="0">
<tr>
<td align="center">
<p>Hej Bilf!,</p>
<p>En ny <strong>PRIVAT</strong> bilbokning har gjorts!</p>
<p>F-älgen är bokad av {{ booking.name }} den {{ booking.date }} klockan {{ booking.time }}. HANTERA!</p>
<p>Med Vänliga hälsningar,<br>
De små tomtarna som sitter i sektionens SMTP-server och matar brevduvorna</p>
</td>
</tr>
</table>

<!-- Separator -->
<div style="height: 40px; background-color: #f5f5f5; border-top: 2px solid #ddd; border-bottom: 2px solid #ddd;"></div>

<!-- English Version -->
<!-- Header -->
<table width="100%" cellpadding="0" cellspacing="0" style="background-color:#FF6600;padding:20px 0;">
<tr>
<td align="center">
<img src="https://i.imgur.com/vajEXHm.png" alt="F-sektionen icon" width="80"
style="display:block; height:auto; border:none; outline:none; text-decoration:none; -ms-interpolation-mode:bicubic;">
</td>
</tr>
</table>
<table width="100%" cellpadding="20" cellspacing="0">
<tr>
<td align="center">
<p>Hello BilF!,</p>
<p>A new <strong>PRIVATE</strong> booking has been made!</p>
<p>The car is booked by {{ booking.name }} on {{ booking.date }} at {{ booking.time}}. TAKE CARE OF IT!</p>
<p>Best regards,<br>
The little gnomes that inhabit the guilds SMTP server and feed the carrier pidgeons</p>
</td>
</tr>
</table>
</body>
</html>
50 changes: 50 additions & 0 deletions mailer/bilf_mailer.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions mailer/mail_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}")
3 changes: 3 additions & 0 deletions services/car_renting_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -150,6 +151,8 @@ def create_new_booking(
db.add(db_booking)
db.commit()

bilf_mailer.bilf_mailer(db_booking)

return db_booking


Expand Down