import logging from typing import Any, Dict, Optional # Try to import emails, but provide a fallback if it fails try: import emails from emails.template import JinjaTemplate EMAILS_AVAILABLE = True except ImportError: EMAILS_AVAILABLE = False logging.warning("Email functionality is disabled due to missing 'emails' package") from app.core.config import settings def send_email( email_to: str, subject_template: str = "", html_template: str = "", environment: Optional[Dict[str, Any]] = None, ) -> None: if not settings.EMAILS_ENABLED: logging.warning("Email feature is disabled. Would have sent email to: %s", email_to) return if environment is None: environment = {} message = emails.Message( subject=JinjaTemplate(subject_template), html=JinjaTemplate(html_template), mail_from=(settings.EMAILS_FROM_NAME, settings.EMAILS_FROM_EMAIL), ) smtp_options = {"host": settings.SMTP_HOST, "port": settings.SMTP_PORT} if settings.SMTP_TLS: smtp_options["tls"] = True if settings.SMTP_USER: smtp_options["user"] = settings.SMTP_USER if settings.SMTP_PASSWORD: smtp_options["password"] = settings.SMTP_PASSWORD response = message.send(to=email_to, render=environment, smtp=smtp_options) logging.info("Email sent to %s, response: %s", email_to, response) def send_test_email(email_to: str) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - Test email" html_template = """
Hi,
This is a test email from {project_name}.
""" send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={"project_name": project_name}, ) def send_email_verification(email_to: str, token: str) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - Verify your email" verification_url = f"http://localhost:8000/api/v1/auth/verify-email?token={token}" html_template = """Hi,
Thanks for signing up for {project_name}.
Please verify your email address by clicking on the link below:
The link is valid for 24 hours.
""" send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={ "project_name": project_name, "verification_url": verification_url, }, ) def send_password_reset(email_to: str, token: str) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - Password Reset" reset_url = f"http://localhost:8000/api/v1/auth/reset-password?token={token}" html_template = """Hi,
You have requested to reset your password for {project_name}.
Please click the link below to reset your password:
The link is valid for 24 hours.
If you didn't request a password reset, please ignore this email.
""" send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={ "project_name": project_name, "reset_url": reset_url, }, ) def send_deposit_confirmation(email_to: str, amount: float, transaction_id: str) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - Deposit Confirmed" html_template = """Hi,
Your deposit of {amount} USDT has been confirmed and added to your account.
Transaction ID: {transaction_id}
Thank you for using {project_name}!
""" send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={ "project_name": project_name, "amount": amount, "transaction_id": transaction_id, }, ) def send_withdrawal_confirmation(email_to: str, amount: float, transaction_id: str) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - Withdrawal Processed" html_template = """Hi,
Your withdrawal of {amount} USDT has been processed.
Transaction ID: {transaction_id}
Thank you for using {project_name}!
""" send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={ "project_name": project_name, "amount": amount, "transaction_id": transaction_id, }, ) def send_bot_purchase_confirmation(email_to: str, bot_name: str, amount: float, expected_roi: float, end_date: str) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - Bot Purchase Confirmation" html_template = """Hi,
Your purchase of the {bot_name} bot for {amount} USDT has been confirmed.
Expected ROI: {expected_roi} USDT
End Date: {end_date}
Thank you for using {project_name}!
""" send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={ "project_name": project_name, "bot_name": bot_name, "amount": amount, "expected_roi": expected_roi, "end_date": end_date, }, ) def send_bot_completion_notification(email_to: str, bot_name: str, amount: float, roi: float) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - Bot Trading Completed" html_template = """Hi,
Your {bot_name} bot has completed its trading cycle.
Principal: {amount} USDT
ROI: {roi} USDT
Total: {total} USDT
The funds have been credited to your Trading Wallet.
Thank you for using {project_name}!
""" total = amount + roi send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={ "project_name": project_name, "bot_name": bot_name, "amount": amount, "roi": roi, "total": total, }, ) def send_kyc_status_update(email_to: str, status: str, reason: Optional[str] = None) -> None: project_name = settings.PROJECT_NAME subject = f"{project_name} - KYC Verification {status.capitalize()}" html_template = """Hi,
Your KYC verification has been {status}.
{reason_html}Thank you for using {project_name}!
""" reason_html = f"Reason: {reason}
" if reason else "" send_email( email_to=email_to, subject_template=subject, html_template=html_template, environment={ "project_name": project_name, "status": status, "reason_html": reason_html, }, )