-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.py
63 lines (56 loc) · 2.16 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
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from dotenv import dotenv_values
from typing import List
from models import User
import jwt
import os
credentials = dotenv_values(".env")
#credentials = {
#"EMAIL": os.getenv("EMAIL"),
#"PASSWORD": os.getenv("PASSWORD"),
#"SECRET": os.getenv("SECRET"),
#"SERVER_URL": os.getenv("SERVER_URL"),
#}
config = ConnectionConfig(
MAIL_USERNAME=credentials["EMAIL"],
MAIL_PASSWORD=credentials["PASSWORD"],
MAIL_FROM=credentials["EMAIL"],
MAIL_PORT=465,
MAIL_SERVER="smtp.gmail.com",
MAIL_STARTTLS=False,
MAIL_SSL_TLS=True,
USE_CREDENTIALS=True,
VALIDATE_CERTS=True
)
async def send_email(email: List, instance: User):
token_data = {"id": instance.id, "username": instance.username}
token = jwt.encode(token_data, credentials["SECRET"], algorithm="HS256")
template = f"""
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="display: flex; align-items: center; justify-content: center; flex-direction: column">
<h3>Account Verification</h3>
</br>
<p>Thanks for choosing our shop.
Please click the link 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="{credentials["SERVER_URL"]}/verification/?token={token}">
Verify your account
</a>
<p>Please ignore this email if you did not register in our Shop. Thanks</p>
</div>
</body>
</html>
"""
message = MessageSchema(
subject="Shop Account Verification Email",
recipients=email, # List of recipients
body=template,
subtype="html"
)
fm = FastMail(config)
await fm.send_message(message=message)