from fastapi import APIRouter, HTTPException, Depends, Form, UploadFile, File from typing import Optional import config from services.email_service import send_contact_form_email import os import uuid router = APIRouter(prefix="/contact", tags=["contact"]) @router.post("") async def submit_contact_form( name: str = Form(...), email: str = Form(...), subject: str = Form(...), message: str = Form(...), file: Optional[UploadFile] = File(None) ): """ Handle contact form submission with optional file attachment. Sends an email notification to the administrator. """ try: # For now, we don't save to DB, just send email. # If we had a contact_messages table, we would INSERT here. # We could handle the file by saving it or just noting its presence. # Since send_email currently only supports HTML body without attachments, # we'll mention the file in the email. # In a real-world scenario, we'd attach the file to the email. file_info = "" if file: file_info = f"\n
Attachment: {file.filename} ({file.size} bytes)
" # Optional: Save file to a temporary location if needed for manual review # but for simplified contact form, mentioning it is common. success = send_contact_form_email( admin_email=config.SMTP_FROM, name=name, from_email=email, subject=subject, message=message + file_info ) if not success: raise HTTPException(status_code=500, detail="Failed to send message. Please try again later.") return {"message": "Your message has been sent successfully."} except Exception as e: print(f"Contact form error: {e}") raise HTTPException(status_code=500, detail="Internal server error")