diff --git a/app/models/email.py b/app/models/email.py new file mode 100644 index 0000000..2981d3e --- /dev/null +++ b/app/models/email.py @@ -0,0 +1,7 @@ +from pydantic import BaseModel, EmailStr +from typing import List, Any, Dict + + +class EmailSchema(BaseModel): + email: List[EmailStr] + body: Dict[str, Any] \ No newline at end of file diff --git a/app/routers/users.py b/app/routers/users.py index e175d01..8f365be 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -1,11 +1,11 @@ from fastapi import APIRouter, Depends, HTTPException, status from ..dependencies import users_token, permissions_checker, database, mail -from ..models import users +from ..models import users, email from typing import Annotated from bson import ObjectId from fastapi.responses import JSONResponse -from fastapi_mail import MessageSchema, MessageType - +from fastapi_mail import MessageSchema, MessageType, FastMail +import random router = APIRouter() @@ -124,13 +124,18 @@ async def read_users_me(userSingle: users.UserCreate | None = None): ) fm = FastMail(mail.conf) + numberkey = str(random.Random()) + key_hashed = users_token.get_password_hash(numberkey) + email_body = {"key":key_hashed, "username":userSingle.username} + email_schema = email.EmailSchema(email=[userSingle.email], body=email_body) message = MessageSchema( subject="Fastapi-Mail module", - recipients=userSingle.email, - template_body=email.dict(), + recipients=email_schema.dict().get("email"), + template_body=email_schema.dict().get("body"), subtype=MessageType.html, ) + await fm.send_message(message, template_name="mailer.html") - current_user = users.User(username=userSingle.username, password=user_token.get_password_hash(userSingle.password), email=userSingle.email) + current_user = users.User(username=userSingle.username, password=users_token.get_password_hash(userSingle.password), email=userSingle.email) user_repository.save(current_user) return JSONResponse(status_code=200, content={"message": "email has been sent"}) \ No newline at end of file diff --git a/app/templates/mailer.html b/app/templates/mailer.html index e9bfbcb..011f049 100644 --- a/app/templates/mailer.html +++ b/app/templates/mailer.html @@ -1,6 +1,6 @@ Email -

Voici un lien https://localhost:8080/api/mail?key={key}&username={username} +

Voici un lien https://localhost:8080/api/mail?key={{ key }}&username={{ username }}