Spaces:
Runtime error
Runtime error
File size: 6,789 Bytes
a6fd1a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
import resend
from fastapi import HTTPException, status
import os
import logging
from dotenv import load_dotenv
load_dotenv()
logger = logging.getLogger(__name__)
async def send_reset_password_email(email: str, code: str, expiry: int) -> bool:
"""
Gửi email đặt lại mật khẩu sử dụng dịch vụ Brevo.
Args:
email (str): Địa chỉ email của người dùng.
code (str): Token đặt lại mật khẩu.
expiry_hours (int): Thời gian hết hạn của link (giờ).
Returns:
bool: True nếu gửi email thành công.
Raises:
HTTPException: Nếu có lỗi khi gửi email.
"""
try:
resend.api_key = os.environ.get('RESEND_API_KEY')
# Email content
subject = "Mã xác minh đăng nhập"
sender = "[email protected]"
reciver = ["[email protected]"]
# Create reset link
# reset_link = f"https://yourapp.com/reset-password?token={reset_token}"
# Email content
subject = "Yêu cầu đặt lại mật khẩu cho tài khoản của bạn"
html_content = f"""
<html>
<body>
<h2>Xin chào {reciver[0].split('@')[0]},</h2>
<p>Chúng tôi nhận được yêu cầu đặt lại mật khẩu cho tài khoản của bạn.</p>
<p>Mã xác minh để đặt lại mật khẩu. Mã này sẽ hết hạn sau {expiry} phut: <strong>{code}</strong></p>
<p>Nếu bạn không yêu cầu đặt lại mật khẩu, vui lòng bỏ qua email này hoặc liên hệ với chúng tôi qua <a href="mailto:[email protected]">[email protected]</a>.</p>
<p>Trân trọng,<br>Đội ngũ Angle Lawer</p>
</body>
</html>
"""
# Create email payload
params: resend.Emails.SendParams = {
"from": sender,
"to": reciver,
"subject": subject,
"html": html_content,
}
# Create and send email
email: resend.Email = resend.Emails.send(params)
return True
except resend.errors.ApiError as e:
logger.error(f"Lỗi khi gửi email: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Lỗi hệ thống. Vui lòng thử lại sau."
)
except Exception as e:
logger.error(f"Lỗi hệ thống khi gửi email: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Lỗi hệ thống. Vui lòng thử lại sau."
)
async def send_verification_code_email(email: str, code: str, expiry_minutes: int = 10) -> bool:
"""
Gửi email chứa mã xác minh đăng nhập sử dụng dịch vụ Brevo.
Args:
email (str): Địa chỉ email của người dùng.
code (str): Mã xác minh 6 chữ số.
expiry_minutes (int): Thời gian hết hạn của mã (phút).
Returns:
bool: True nếu gửi email thành công.
Raises:
HTTPException: Nếu có lỗi khi gửi email.
"""
try:
resend.api_key = os.environ.get('RESEND_API_KEY')
# Email content
subject = "Mã xác minh đăng nhập"
sender = "[email protected]"
reciver = [email]
html_content = f"""
<html>
<body>
<h2>Xin chào {reciver[0].split('@')[0]},</h2>
<p>Mã xác minh đăng nhập của bạn là: <strong>{code}</strong></p>
<p>Mã này sẽ hết hạn sau {expiry_minutes} phút.</p>
<p>Nếu bạn không yêu cầu đăng nhập, vui lòng bỏ qua email này hoặc liên hệ với chúng tôi qua <a href="mailto:[email protected]">[email protected]</a>.</p>
<p>Trân trọng,<br>Đội ngũ Angel Lawer</p>
</body>
</html>
"""
params: resend.Emails.SendParams = {
"from": sender,
"to": reciver,
"subject": subject,
"html": html_content,
}
# Create and send email
email: resend.Email = resend.Emails.send(params)
return True
except resend.errors.ApiError as e:
logger.error(f"Lỗi khi gửi email: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Lỗi hệ thống. Vui lòng thử lại sau."
)
except Exception as e:
logger.error(f"Lỗi hệ thống khi gửi email: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Lỗi hệ thống. Vui lòng thử lại sau."
)
async def send_suspicious_activity_email(email: str) -> bool:
"""
Gửi email thông báo hoạt động đáng ngờ (ví dụ: refresh token không hợp lệ).
Args:
email (str): Địa chỉ email của người dùng.
Returns:
bool: True nếu gửi email thành công.
Raises:
HTTPException: Nếu có lỗi khi gửi email.
"""
try:
resend.api_key = os.environ.get('RESEND_API_KEY')
subject = "Cảnh báo hoạt động đáng ngờ"
# Email content
subject = "Mã xác minh đăng nhập"
sender = "[email protected]"
reciver = [email]
html_content = f"""
<html>
<body>
<h2>Xin chào {reciver[0].split('@')[0]},</h2>
<p>Chúng tôi đã phát hiện một nỗ lực sử dụng refresh token không hợp lệ để truy cập tài khoản của bạn.</p>
<p>Nếu đây không phải là bạn, vui lòng bảo mật tài khoản ngay lập tức và liên hệ với chúng tôi qua <a href="mailto:[email protected]">[email protected]</a>.</p>
<p>Trân trọng,<br>Đội ngũ Angle Lawer</p>
</body>
</html>
"""
params: resend.Emails.SendParams = {
"from": sender,
"to": reciver,
"subject": subject,
"html": html_content,
}
# Create and send email
email: resend.Email = resend.Emails.send(params)
return True
except resend.errors.ApiError as e:
logger.error(f"Lỗi khi gửi email: {str(e)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Lỗi hệ thống. Vui lòng thử lại sau."
)
except Exception as e:
logger.error(f"Lỗi hệ thống khi gửi email cảnh báo: {str(e)}")
return False |