Skip to content

♻️ Send account creation email asynchronously via BackgroundTasks #1505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions backend/app/api/routes/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from typing import Any

from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
from sqlmodel import col, delete, func, select

from app import crud
Expand Down Expand Up @@ -51,7 +51,9 @@ def read_users(session: SessionDep, skip: int = 0, limit: int = 100) -> Any:
@router.post(
"/", dependencies=[Depends(get_current_active_superuser)], response_model=UserPublic
)
def create_user(*, session: SessionDep, user_in: UserCreate) -> Any:
def create_user(
*, session: SessionDep, user_in: UserCreate, background_tasks: BackgroundTasks
) -> Any:
"""
Create new user.
"""
Expand All @@ -67,7 +69,8 @@ def create_user(*, session: SessionDep, user_in: UserCreate) -> Any:
email_data = generate_new_account_email(
email_to=user_in.email, username=user_in.email, password=user_in.password
)
send_email(
background_tasks.add_task(
send_email,
email_to=user_in.email,
subject=email_data.subject,
html_content=email_data.html_content,
Expand Down
Loading