Fix: pass send_email directly to background tasks instead of inner async closures

This commit is contained in:
Atharv1705
2026-07-15 15:43:34 +05:30
parent 40bf55267b
commit 6e3d1a9203
+48 -74
View File
@@ -98,35 +98,22 @@ async def forgot_password(
frontend_url = os.getenv("FRONTEND_URL", "https://bookkeepro.net") frontend_url = os.getenv("FRONTEND_URL", "https://bookkeepro.net")
reset_link = f"{frontend_url}/reset-password?token={reset_token}" reset_link = f"{frontend_url}/reset-password?token={reset_token}"
# Add email task with error handling reset_body = (
async def send_reset_email_safely(): f"Hi {user.name},<br><br>"
try: f"Click the button below to reset your password:<br><br>"
await send_email( f'<a href="{reset_link}" '
to=user.email, f'style="padding:12px 18px;background:#FF7F11;color:#fff;'
subject="Reset your BookKeepro password", f'text-decoration:none;border-radius:6px;">'
body=f""" f"Reset Password</a><br><br>"
Hi {user.name},<br><br> f"This link expires in 15 minutes.<br><br>"
Click the button below to reset your password:<br><br> f"BookKeepro Team"
)
<a href="{reset_link}" background.add_task(
style="padding:12px 18px; send_email,
background:#FF7F11; to=user.email,
color:#fff; subject="Reset your BookKeepro password",
text-decoration:none; body=reset_body,
border-radius:6px;"> )
Reset Password
</a>
<br><br>
This link expires in 15 minutes.<br><br>
BookKeepro Team
"""
)
except Exception as e:
import logging
logger = logging.getLogger(__name__)
logger.error(f"Failed to send reset email to {user.email}: {e}")
background.add_task(send_reset_email_safely)
return {"message": "If the email exists, a reset link has been sent"} return {"message": "If the email exists, a reset link has been sent"}
@@ -255,29 +242,22 @@ async def signup(request: Request, payload: SignupRequest, background: Backgroun
verify_token = create_email_verification_token(user.email) verify_token = create_email_verification_token(user.email)
frontend_url = os.getenv("FRONTEND_URL", "https://bookkeepro.net") frontend_url = os.getenv("FRONTEND_URL", "https://bookkeepro.net")
verify_link = f"{frontend_url}/verify-email?token={verify_token}" verify_link = f"{frontend_url}/verify-email?token={verify_token}"
verify_body = (
async def send_verify_email(): f"Hi {user.name},<br><br>"
try: f"Welcome to BookKeepro! Please verify your email address to activate your account.<br><br>"
await send_email( f'<a href="{verify_link}" '
to=user.email, f'style="padding:12px 24px;background:#FF7F11;color:#fff;'
subject="Verify your BookKeepro email", f'text-decoration:none;border-radius:6px;font-weight:600;">'
body=f""" f"Verify My Email</a><br><br>"
Hi {user.name},<br><br> f"This link expires in 24 hours. If you did not sign up, ignore this email.<br><br>"
Welcome to BookKeepro! Please verify your email address to activate your account.<br><br> f"BookKeepro Team"
<a href="{verify_link}" )
style="padding:12px 24px;background:#FF7F11;color:#fff; background.add_task(
text-decoration:none;border-radius:6px;font-weight:600;"> send_email,
Verify My Email to=user.email,
</a> subject="Verify your BookKeepro email",
<br><br> body=verify_body,
This link expires in 24 hours. If you did not sign up, ignore this email.<br><br> )
BookKeepro Team
"""
)
except Exception as e:
logger.error(f"Verification email failed for {user.email}: {e}")
background.add_task(send_verify_email)
return { return {
"access_token": "", # no token until verified "access_token": "", # no token until verified
@@ -370,28 +350,22 @@ async def resend_verification(
frontend_url = os.getenv("FRONTEND_URL", "https://bookkeepro.net") frontend_url = os.getenv("FRONTEND_URL", "https://bookkeepro.net")
verify_link = f"{frontend_url}/verify-email?token={verify_token}" verify_link = f"{frontend_url}/verify-email?token={verify_token}"
async def send_resend_email(): resend_body = (
try: f"Hi {user.name},<br><br>"
await send_email( f"Here is your new verification link:<br><br>"
to=user.email, f'<a href="{verify_link}" '
subject="Verify your BookKeepro email", f'style="padding:12px 24px;background:#FF7F11;color:#fff;'
body=f""" f'text-decoration:none;border-radius:6px;font-weight:600;">'
Hi {user.name},<br><br> f"Verify My Email</a><br><br>"
Here is your new verification link:<br><br> f"This link expires in 24 hours.<br><br>"
<a href="{verify_link}" f"BookKeepro Team"
style="padding:12px 24px;background:#FF7F11;color:#fff; )
text-decoration:none;border-radius:6px;font-weight:600;"> background.add_task(
Verify My Email send_email,
</a> to=user.email,
<br><br> subject="Verify your BookKeepro email",
This link expires in 24 hours.<br><br> body=resend_body,
BookKeepro Team )
"""
)
except Exception as e:
logger.error(f"Resend verification email failed for {user.email}: {e}")
background.add_task(send_resend_email)
return {"message": "If the email exists and is unverified, a new link has been sent"} return {"message": "If the email exists and is unverified, a new link has been sent"}