Files
AI_CPA/services/api/app/routers/contact.py
T

66 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request
from app.limiter import limiter
from pydantic import BaseModel, EmailStr, field_validator
from app.utils.emailer import send_email
router = APIRouter(prefix="/api", tags=["contact"])
# Get admin email from environment, with fallback
ADMIN_EMAIL = os.getenv("ADMIN_EMAIL", "admin@bookkeepro.net")
class ContactForm(BaseModel):
first_name: str
last_name: str
email: EmailStr
phone: str
message: str = ""
@field_validator("message")
@classmethod
def cap_message(cls, v: str) -> str:
if len(v) > 2000:
raise ValueError("Message must be 2000 characters or fewer")
return v
@router.post("/contact")
@limiter.limit("5/minute")
async def contact_message(request: Request, form: ContactForm, background: BackgroundTasks):
if not ADMIN_EMAIL or "@" not in ADMIN_EMAIL:
raise HTTPException(status_code=500, detail="Admin email not configured")
background.add_task(
send_email,
to=ADMIN_EMAIL,
subject="New Contact Enquiry Received — Follow-Up Required - BookKeepro",
body=f"""
Dear Team,
<br><br>
A new contact enquiry has been received. Please find the details below:
<br><br>
<b>Name:</b> {form.first_name} {form.last_name}<br>
<b>Email:</b> {form.email}<br>
<b>Phone:</b> {form.phone}<br>
<b>Message:</b> {form.message or '(none)'}<br><br>
Kindly follow up with the user at the earliest convenience.
<br><br>
Thank you,<br>
BookKeepro Support Team
"""
)
# auto-reply to user
background.add_task(
send_email,
to=form.email,
subject="Thanks for contacting BookKeepro",
body=f"""
Hi {form.first_name},<br><br>
Thank you for reaching out to BookKeepro.<br>
We’ve received your enquiry, and our team will get back to you shortly.<br><br>
Best Regards,<br>
BookKeepro Team
"""
)
return {"status": "sent"}