-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.py
98 lines (80 loc) · 2.85 KB
/
emails.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from database.models import User
from fastapi import FastAPI, BackgroundTasks, UploadFile, File, Form,HTTPException
from starlette.responses import JSONResponse
from starlette.requests import Request
from fastapi_mail import FastMail, MessageSchema,ConnectionConfig
from pydantic import BaseModel, EmailStr
from typing import List
from decouple import config
import jwt
EMAIL_SECRET = config("EMAIL_SECRET")
JWT_ALGORITHM = config("algorithm")
class EmailSchema(BaseModel):
email: List[EmailStr]
conf = ConnectionConfig(
MAIL_USERNAME = config("EMAIL"),
MAIL_PASSWORD = config("PASS"),
MAIL_FROM = config("EMAIL"),
MAIL_PORT = 587,
MAIL_SERVER = "smtp.dreamhost.com",
MAIL_TLS = True,
MAIL_SSL = False,
USE_CREDENTIALS = True
)
def effify(non_f_str: str):
return eval(f'f"""{non_f_str}"""')
async def decode_verification_token(token:str):
payload = jwt.decode(token,EMAIL_SECRET,algorithms=[JWT_ALGORITHM])
'''try:
payload = jwt.decode(token,EMAIL_SECRET,algorithms=[JWT_ALGORITHM])
except Exception as e:
print(e)
#raise HTTPException(status_code =401, email="Invalid token",headers={"WWW.Authenticate":"Bearer"} )'''
return payload
'''
email for user confirmation
'''
async def send_confirmation_mail(email: List,user:User) -> JSONResponse:
token_data = {
"id":user.id,
"username":user.username,
}
token = jwt.encode(token_data,EMAIL_SECRET, algorithm=JWT_ALGORITHM).decode('utf-8')
with open('templates/user_verification.html', 'r') as file:
data = file.read()
#html = effify(data)
#transform content to f string
html = eval(f'f"""{data}"""')
message = MessageSchema(
subject="fafanlp verification email",
recipients=email, # List of recipients, as many as you can pass
body=html,
subtype="html"
)
fm = FastMail(conf)
await fm.send_message(message)
return JSONResponse(status_code=200, content={"message": "email has been sent"})
'''
email for password change
'''
async def send_recovery_mail(email: List,user:User) -> JSONResponse:
token_data = {
"id":user.id,
"username":user.username,
"verified":user.verified
}
token = jwt.encode(token_data,EMAIL_SECRET, algorithm=JWT_ALGORITHM).decode('utf-8')
with open('templates/password_recovery.html', 'r') as file:
data = file.read()
#html = effify(data)
#transform content to f string
html = eval(f'f"""{data}"""')
message = MessageSchema(
subject="fafanlp password recovery",
recipients=email, # List of recipients, as many as you can pass
body=html,
subtype="html"
)
fm = FastMail(conf)
await fm.send_message(message)
return JSONResponse(status_code=200, content={"message": "email has been sent"})