-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.py
71 lines (63 loc) · 2.67 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
import os
from dotenv import dotenv_values
from fastapi import (BackgroundTasks, UploadFile, File, Form, Depends, HTTPException, status)
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from pydantic import BaseModel, EmailStr
from typing import List
from models import User
import jwt
config_credentials = dotenv_values('.env')
conf = ConnectionConfig(
MAIL_USERNAME = config_credentials['MAIL_USERNAME'],
MAIL_PASSWORD = config_credentials['MAIL_PASSWORD'],
MAIL_FROM = config_credentials['MAIL_USERNAME'],
MAIL_PORT = config_credentials['MAIL_PORT'],
MAIL_SERVER = config_credentials['MAIL_SERVER'],
MAIL_STARTTLS = config_credentials['MAIL_USE_TLS'],
MAIL_SSL_TLS = config_credentials['MAIL_USE_SSL'],
USE_CREDENTIALS = True,
VALIDATE_CERTS= True
)
class EmailSchema(BaseModel):
email: List[EmailStr]
async def send_register_email(email: List, instance: User):
#print(isinstance, type(isinstance), isinstance.__dict__)
print("------------------")
token_data = {
'id': instance.id,
'username': instance.username
}
token = jwt.encode(token_data, config_credentials['SECRET'], algorithm='HS256')
template = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style= "display: flex; align-items: center; justif-content:
center; flex-direction: column">
<h3>Account Verification</h3>
<br>
<p>Thanks for choosing EasyShops,
Please click on the button below
to verify your account</p>
<a style="margin top: 1rem; padding: 1rem; border-radius: 0.5rem;
font-size: 1rem; text-decoration: none; background: #0275d8; color:
white;" href="http://localhost:8000/verification/?token={token}">
Verify your email</a>
<p>Please kindly ignore this email if you did not register for EasyShops and nothing will happend. Thanks</p>
</div>
</body>
</html>
"""
message = MessageSchema(
subject='EasyShop Account Verification Email',
recipients=email,
body = template,
subtype = 'html'
)
fm = FastMail(conf)
await fm.send_message(message=message)