from app.worker import celery_app from app.services.email import email_service from app.models.lead import Lead # If needed, but usually we pass primitive types (emails, subject, body) to tasks import asyncio from typing import List, Optional @celery_app.task def send_email_task(to_email: str, subject: str, html_content: str): """ Background task to send email using async EmailService. """ try: # Create a new event loop for this thread/task loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # Run the async service method loop.run_until_complete(email_service.send_email( to=[to_email], subject=subject, html_body=html_content )) loop.close() return f"Email sent to {to_email}" except Exception as e: return f"Failed to send email: {e}"