send email ok

This commit is contained in:
Valentin CZERYBA 2023-10-15 15:06:54 +02:00
parent 228c9bebb8
commit f32fa3e5b9
3 changed files with 19 additions and 7 deletions

7
app/models/email.py Normal file
View File

@ -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]

View File

@ -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"})

View File

@ -1,6 +1,6 @@
<html>
<head><title>Email</title></head>
<body><p>Voici un lien https://localhost:8080/api/mail?key={key}&username={username}
<body><p>Voici un lien https://localhost:8080/api/mail?key={{ key }}&username={{ username }}
</p></body>
</html>