Coverage for src/services/email.py: 57%
21 statements
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-01 22:29 +0200
« prev ^ index » next coverage.py v7.5.4, created at 2024-07-01 22:29 +0200
1from fastapi_mail import FastMail, MessageSchema, MessageType
2from fastapi_mail.errors import ConnectionErrors
4from pydantic import EmailStr
6from src.services.auth import auth_serviсe
8from src.conf.config import conf
11async def send_email(email: EmailStr, username: str, host: str):
12 """
13 Get a list of contacts.
15 Parameters:
16 - limit (int): The maximum number of contacts to retrieve. Default is 10, minimum is 1, and maximum is 500.
17 - offset (int): The index of the first contact to retrieve. Default is 0.
18 - db (AsyncSession): The database session to use for the operation.
19 - user (User): The authenticated user making the request.
21 Returns:
22 - list[ContactResponseSchema]: A list of ContactResponseSchema objects representing the contacts.
23 """
24 try:
25 token_verification = auth_serviсe.create_email_token({"sub": email})
26 message = MessageSchema(
27 subject="Confirm your email ",
28 recipients=[email],
29 template_body={
30 "host": host,
31 "username": username,
32 "token": token_verification,
33 },
34 subtype=MessageType.html,
35 )
36 fm = FastMail(conf)
37 await fm.send_message(message, template_name="email_verification.html")
38 except ConnectionErrors as err:
39 print(err)
42async def send_password_reset_email(email: EmailStr, username: str, host: str):
43 """
44 Sends a password reset email to the specified user.
46 Parameters:
47 - email (EmailStr): The email address of the user to send the password reset email to.
48 - username (str): The username of the user to send the password reset email to.
49 - host (str): The hostname of the website or application.
51 Returns:
52 - None: This function does not return any value. It sends an email asynchronously.
54 Raises:
55 - ConnectionErrors: If there is an error connecting to the email service.
56 """
57 try:
58 token_verification = auth_serviсe.create_email_token({"sub": email})
59 message = MessageSchema(
60 subject="Password reset ",
61 recipients=[email],
62 template_body={
63 "host": host,
64 "username": username,
65 "token": token_verification,
66 },
67 subtype=MessageType.html,
68 )
69 fm = FastMail(conf)
70 await fm.send_message(message, template_name="password_reset_mail.html")
71 except ConnectionErrors as err:
72 print(err)