[34588f42] Feat: Personalisierte Dankes-E-Mail mit Anleitung und Signatur

- ReleaseParticipant Tabelle hinzugefügt, um Vornamen für den Webhook zwischenzuspeichern.
- Dankes-E-Mail Template mit Anleitungstext, Gutschein-Code und Anleitung-Bild aktualisiert.
- Offizielle Projektsignatur in Backend-E-Mails integriert.
- Frontend sendet nun Teilnehmer-Mapping beim Versand der Anfrage.
This commit is contained in:
2026-04-17 21:43:30 +00:00
parent 3f6b27a89f
commit ba06e6d033
3 changed files with 77 additions and 41 deletions

View File

@@ -1,26 +1,38 @@
from fastapi import APIRouter, Depends, HTTPException, Request, BackgroundTasks
from pydantic import BaseModel
from sqlalchemy.orm import Session
from database import get_db, DiscountCode
import datetime
import logging
from gmail_service import GmailService
import re
import time
import asyncio
from typing import List, Dict, Optional
from database import get_db, DiscountCode, ReleaseParticipant
router = APIRouter(prefix="/api/publish-request", tags=["publish-request"])
logger = logging.getLogger("publish-request")
# Official Project Signature
SIGNATURE_HTML = """
<br><br>
<span style="color: #888;">--</span><br>
<div dir="ltr">
<table border="0" cellspacing="0" cellpadding="0" style="border-collapse:collapse; margin-top: 5px;">
<tbody>
<tr>
<td width="220" valign="top" style="padding-right: 15px;">
<img width="200" src="https://lh3.googleusercontent.com/d/1K7RODOqKE2e1nRJ3D4dEWdjthoTMyXUq" alt="Kinderfotos Erding Logo" style="display: block;">
</td>
<td valign="bottom" style="padding-left: 15px; border-left: 1px solid #ddd; font-family: sans-serif; font-size: 13px; color: #333; line-height: 1.5;">
<p style="margin: 0;"><b>Kinderfotos Erding</b> | <a href="http://www.kinderfotos-erding.de/" target="_blank" style="color: #1155cc; text-decoration: none;">www.kinderfotos-erding.de</a></p>
<p style="margin: 0; color: #666;">Gartenstr. 10 | 85445 Oberding | 08122-8470867</p>
</td>
</tr>
</tbody>
</table>
</div>
"""
class CodesUpload(BaseModel):
codes: str # comma separated
class SendReleaseRequest(BaseModel):
emails: List[Dict[str, str]]
scheduled_time: Optional[str] = None # e.g. "10:00"
participants: Optional[List[Dict[str, str]]] = None # [{email, first_name}]
async def delayed_send(emails: List[Dict[str, str]], scheduled_time: str, db: Session):
async def delayed_send(emails: List[Dict[str, str]], scheduled_time: str, db_session_factory):
try:
# Calculate delay
now = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=2))) # Berlin Time Approx
@@ -35,21 +47,41 @@ async def delayed_send(emails: List[Dict[str, str]], scheduled_time: str, db: Se
await asyncio.sleep(delay_seconds)
service = GmailService(db)
success_count = 0
for email_data in emails:
if service.send_email(email_data["to"], email_data["subject"], email_data["body"]):
success_count += 1
await asyncio.sleep(1) # Rate limiting
logger.info(f"Scheduled send complete: {success_count}/{len(emails)} success.")
# We need a fresh DB session for the background task
db = db_session_factory()
try:
service = GmailService(db)
success_count = 0
for email_data in emails:
if service.send_email(email_data["to"], email_data["subject"], email_data["body"]):
success_count += 1
await asyncio.sleep(1) # Rate limiting
logger.info(f"Scheduled send complete: {success_count}/{len(emails)} success.")
finally:
db.close()
except Exception as e:
logger.exception("Error in delayed_send background task")
@router.post("/send")
async def send_requests(data: SendReleaseRequest, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
# Store participant names for later (webhook)
if data.participants:
for p in data.participants:
email = p.get("email", "").strip().lower()
first_name = p.get("first_name", "").strip()
if email and first_name:
existing = db.query(ReleaseParticipant).filter(ReleaseParticipant.email == email).first()
if existing:
existing.first_name = first_name
else:
db.add(ReleaseParticipant(email=email, first_name=first_name))
db.commit()
if data.scheduled_time:
background_tasks.add_task(delayed_send, data.emails, data.scheduled_time, db)
# Pass a way to get a new session to the background task
from database import SessionLocal
background_tasks.add_task(delayed_send, data.emails, data.scheduled_time, SessionLocal)
return {"status": "scheduled", "message": f"Versand für {data.scheduled_time} geplant."}
# Immediate send
@@ -113,9 +145,12 @@ async def handle_webhook(request: Request, db: Session = Depends(get_db)):
free_code = db.query(DiscountCode).filter(DiscountCode.is_used == 0).first()
if not free_code:
logger.error("NO FREE DISCOUNT CODES LEFT!")
# Fallback logic: Notify admin?
return {"status": "error", "message": "No codes available"}
# Look up participant name
participant = db.query(ReleaseParticipant).filter(ReleaseParticipant.email == email).first()
first_name = participant.first_name if participant else "Ihr Lieben"
# Mark as used
free_code.is_used = 1
free_code.assigned_to_email = email
@@ -126,23 +161,16 @@ async def handle_webhook(request: Request, db: Session = Depends(get_db)):
service = GmailService(db)
subject = "Dankeschön für Eure Freigabe & Euer Rabattcode"
# HTML Signature from frontend
SIGNATURE_HTML = """
<br><br><br>
<div style="font-family: 'Verdana', sans-serif; font-size: 11px; color: #3D3C3F;">
<p><strong>Liebe Grüße,</strong><br>Euer Team von Kinderfotos Erding</p>
<p><strong>Zierl Fotografen GmbH</strong><br>
Anton-Bruckner-Straße 5<br>85435 Erding</p>
<p><a href="https://www.kinderfotos-erding.de" style="color: #3D3C3F; text-decoration: none;">www.kinderfotos-erding.de</a></p>
</div>
"""
# Image provided by user
INSTRUCTIONS_IMAGE_URL = "https://mail.google.com/mail/u/2?ui=2&ik=719adaa3c5&attid=0.1&permmsgid=msg-a:r7482671925923393616&th=196e322c399dbc7f&view=fimg&fur=ip&permmsgid=msg-a:r7482671925923393616&sz=s0-l75-ft&attbid=ANGjdJ9_U6ayMFgwbupt4HalTKO867IHx6N70eNbPfQmTLNzRXilJxI-n8a1gjM8xVcP5HEOgaVxfp3FnJPzTYEEYhK4gSU-Il_0a6OtzFYscp55_W4iyxuxjyPvK4&disp=emb&realattid=ii_maspzxv50&zw"
body_html = f"""
<p>Hallo,</p>
<p>vielen Dank für Eure Unterstützung und das Ausfüllen der Freigabe!</p>
<p>Als kleines Dankeschön hier Euer 25 € Rabattcode für Eure Bestellung:</p>
<p><strong style="font-size: 18px; color: #4F46E5; padding: 10px; border: 1px dashed #4F46E5; display: inline-block;">{free_code.code}</strong></p>
<p>Bitte wartet mit Eurer Bestellung, falls Ihr noch nicht bestellt habt, bis Ihr diesen Code an der Kasse einlösen könnt.</p>
<p>Hallo {first_name},</p>
<p>Vielen Dank nochmal für die Freigabe zur Veröffentlichung, das ist super nett von Euch!</p>
<p>Hier ist euer Gutscheincode über 25 Euro: <strong style="font-size: 18px; color: #4F46E5;">{free_code.code}</strong></p>
<p>Um den Gutschein einzugeben, musst du auf den Preis des Warenkorbs drücken (über dem Button zur Kasse gehen):</p>
<p><img src="{INSTRUCTIONS_IMAGE_URL}" alt="Anleitung Gutschein einlösen" style="max-width: 100%; border: 1px solid #ddd; border-radius: 8px;"></p>
<p>Liebe Grüße,<br>das Team von Kinderfotos Erding</p>
{SIGNATURE_HTML}
"""