send email ok
This commit is contained in:
parent
228c9bebb8
commit
f32fa3e5b9
7
app/models/email.py
Normal file
7
app/models/email.py
Normal 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]
|
@ -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"})
|
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user