262 lines
7.8 KiB
Python
262 lines
7.8 KiB
Python
import logging
|
|
from typing import Any, Dict, Optional
|
|
|
|
import emails
|
|
from emails.template import JinjaTemplate
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>This is a test email from {project_name}.</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>Thanks for signing up for {project_name}.</p>
|
|
<p>Please verify your email address by clicking on the link below:</p>
|
|
<p><a href="{verification_url}">{verification_url}</a></p>
|
|
<p>The link is valid for 24 hours.</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>You have requested to reset your password for {project_name}.</p>
|
|
<p>Please click the link below to reset your password:</p>
|
|
<p><a href="{reset_url}">{reset_url}</a></p>
|
|
<p>The link is valid for 24 hours.</p>
|
|
<p>If you didn't request a password reset, please ignore this email.</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>Your deposit of {amount} USDT has been confirmed and added to your account.</p>
|
|
<p>Transaction ID: {transaction_id}</p>
|
|
<p>Thank you for using {project_name}!</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>Your withdrawal of {amount} USDT has been processed.</p>
|
|
<p>Transaction ID: {transaction_id}</p>
|
|
<p>Thank you for using {project_name}!</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>Your purchase of the {bot_name} bot for {amount} USDT has been confirmed.</p>
|
|
<p>Expected ROI: {expected_roi} USDT</p>
|
|
<p>End Date: {end_date}</p>
|
|
<p>Thank you for using {project_name}!</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>Your {bot_name} bot has completed its trading cycle.</p>
|
|
<p>Principal: {amount} USDT</p>
|
|
<p>ROI: {roi} USDT</p>
|
|
<p>Total: {total} USDT</p>
|
|
<p>The funds have been credited to your Trading Wallet.</p>
|
|
<p>Thank you for using {project_name}!</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
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 = """
|
|
<html>
|
|
<body>
|
|
<p>Hi,</p>
|
|
<p>Your KYC verification has been {status}.</p>
|
|
{reason_html}
|
|
<p>Thank you for using {project_name}!</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
reason_html = f"<p>Reason: {reason}</p>" 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,
|
|
},
|
|
) |