"""Generic contact-form endpoint.""" from email.utils import parseaddr from flask import request from email_service import EmailDeliveryError, send_contact_email from errors import error_response from settings import app, limiter def valid_email(value): return bool( value and "\r" not in value and "\n" not in value and parseaddr(value)[1] == value and "@" in value ) @app.post("/api/contact") @limiter.limit("5 per hour") def contact(): req = request.get_json(silent=True) or {} # Quietly accept bot submissions that fill the hidden honeypot field. if req.get("website"): return {"success": True} name = req.get("name", "").strip() email = req.get("email", "").strip() subject = req.get("subject", "").strip() message = req.get("message", "").strip() if not (1 <= len(name) <= 100 and valid_email(email)): return error_response(400, "A valid name and email are required") if not ( 1 <= len(subject) <= 150 and "\r" not in subject and "\n" not in subject and 10 <= len(message) <= 5000 ): return error_response(400, "A valid subject and message are required") try: send_contact_email( name=name, email=email, subject=subject, message=message, ) except EmailDeliveryError: app.logger.error("Contact email delivery failed") return error_response(503, "Unable to send message right now") return {"success": True}