AdGenesis-App / helpers_function /helper_email.py
userIdc2024's picture
Upload 3 files
3c2b4ae verified
import logging
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from core.settings import cnf
from database.connections import get_mongo_client
logger = logging.getLogger(__name__)
def _send_email(to_email: str, subject: str, body: str) -> bool:
"""Low-level Gmail SMTP email sender."""
try:
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
EMAIL_USER = cnf.EMAIL_USER
EMAIL_PASS = cnf.EMAIL_PASS
if not all([to_email, subject, body, EMAIL_USER, EMAIL_PASS]):
logger.error("Missing required email parameters")
return False
msg = MIMEMultipart()
msg["From"] = EMAIL_USER
msg["To"] = to_email
msg["Subject"] = subject
msg.attach(MIMEText(body, "plain"))
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls()
server.login(EMAIL_USER, EMAIL_PASS)
server.sendmail(EMAIL_USER, to_email, msg.as_string())
logger.info(f"Email sent to {to_email}")
return True
except Exception as e:
logger.error(f"Failed to send email: {e}")
return False
def all_tasks_completed_notification(user_email: str, product_name: str) -> bool:
"""
Send success email if the user has completed image, script, and video tasks.
"""
db = get_mongo_client()
if db is None:
logger.error("MongoDB not available for notifications")
return False
has_image = db["results"].count_documents({"created_by": user_email, "type": {"$in": ["generation", "variation"]}}) > 0
has_script = db["script_generator"].count_documents({"created_by": user_email}) > 0
has_video = db["video_analyses"].count_documents({"created_by": user_email}) > 0
if has_image and has_script and has_video:
body = f"""Hey {user_email},
πŸŽ‰ Congratulations!
You have successfully:
- Generated Images
- Generated Scripts
- Analyzed Videos
Your work for **{product_name}** has been completed successfully.
You can now review all assets in the AI Library.
Best regards,
The Creative AdGenesis Team
"""
return _send_email(user_email, "All Tasks Completed Successfully ", body)
logger.info(f"Tasks not fully completed for {user_email}")
return False