| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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<p><strong>Attachment:</strong> {file.filename} ({file.size} bytes)</p>"
- # 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:
- import traceback
- print(f"CRITICAL Contact form error: {e}")
- traceback.print_exc()
- raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
|