-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify.py
68 lines (60 loc) · 2.15 KB
/
notify.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
import os
from dotenv import load_dotenv
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
import logging
import ssl
logger = logging.getLogger("uvicorn")
ssl._create_default_https_context = ssl._create_unverified_context
load_dotenv()
SENDER_GMAIL = os.environ["SENDER_GMAIL"]
SENDER_GMAIL_PASSWORD = os.environ["SENDER_GMAIL_PASSWORD"]
dirname = os.path.dirname(__file__)
templates_folder = os.path.join(dirname, '../templates')
conf = ConnectionConfig(
MAIL_USERNAME = SENDER_GMAIL,
MAIL_PASSWORD = SENDER_GMAIL_PASSWORD,
MAIL_FROM = SENDER_GMAIL,
MAIL_PORT = 587,
MAIL_SERVER = "smtp.gmail.com",
MAIL_FROM_NAME="FastAPI forgot password example",
MAIL_STARTTLS = True,
MAIL_SSL_TLS = False,
USE_CREDENTIALS = True,
VALIDATE_CERTS = False,
TEMPLATE_FOLDER = templates_folder,
)
async def send_registration_notification(password, recipient_email):
template_body = {
"email": recipient_email,
"password": password
}
try:
message = MessageSchema(
subject="FastAPI forgot password application registration",
recipients=[recipient_email],
template_body=template_body,
subtype=MessageType.html
)
fm = FastMail(conf)
await fm.send_message(message, template_name="registration_notification.html")
except Exception as e:
logger.error(f"Something went wrong in registration email notification")
logger.error(str(e))
async def send_reset_password_mail(recipient_email, user, url, expire_in_minutes):
template_body = {
"user": user,
"url": url,
"expire_in_minutes": expire_in_minutes
}
try:
message = MessageSchema(
subject="FastAPI forgot password application reset password",
recipients=[recipient_email],
template_body=template_body,
subtype=MessageType.html
)
fm = FastMail(conf)
await fm.send_message(message, template_name="reset_password_email.html")
except Exception as e:
logger.error(f"Something went wrong in reset password email")
logger.error(str(e))