diff --git a/src/accounts/client_account/services.py b/src/accounts/client_account/services.py index 540a01a..93bfe80 100644 --- a/src/accounts/client_account/services.py +++ b/src/accounts/client_account/services.py @@ -15,7 +15,7 @@ from ...users.logic import all_users, get_or_404 from ...users.models import users from ...users.schemas import UserCreate, OwnerCreate, UserUpdate -from ...service import check_dict +from ...service import check_dict, send_mail async def get_clients(): @@ -130,5 +130,8 @@ async def add_owner(client_id: int, owner: OwnerCreate): status_code=status.HTTP_400_BAD_REQUEST, detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS, ) + + message = f"Добро пожаловать в UDV Service Desk!\n\nВаш логин в системе: {owner.email}\nВаш пароль: {owner.password}" + await send_mail(owner.email, "Вы зарегистрированы в системе", message) updated_owner = await update_owner(client_id, created_owner.id) return updated_owner diff --git a/src/accounts/developer_account/services.py b/src/accounts/developer_account/services.py index 97a1aa1..2b4714b 100644 --- a/src/accounts/developer_account/services.py +++ b/src/accounts/developer_account/services.py @@ -5,6 +5,7 @@ from pydantic.types import UUID4 from src.db.db import database +from src.service import send_mail from src.users.logic import all_users, delete_user, update_user from src.users.models import users from src.users.schemas import UserCreate, DeveloperCreate, UserUpdate @@ -28,7 +29,8 @@ async def add_developer(user: UserCreate): status_code=status.HTTP_400_BAD_REQUEST, detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS, ) - + message = f"Добро пожаловать в UDV Service Desk!\n\nВаш логин в системе: {user.email}\nВаш пароль: {user.password}" + await send_mail(user.email, "Вы зарегистрированы в системе", message) return created_developer diff --git a/src/accounts/employee_account/services.py b/src/accounts/employee_account/services.py index 2fbfd42..e45fa04 100644 --- a/src/accounts/employee_account/services.py +++ b/src/accounts/employee_account/services.py @@ -6,6 +6,7 @@ from src.db.db import database from src.reference_book.models import licences +from src.service import send_mail from src.users.logic import all_users, update_user, delete_user from src.users.models import users from src.users.schemas import UserCreate, EmployeeCreate, UserUpdate @@ -39,11 +40,9 @@ async def count_allowed_employees(client_id: int): async def add_employee(id: int, user: UserCreate): - employee = EmployeeCreate( - **user.dict(), - client_id=id, - is_owner=False, - date_reg=datetime.utcnow()) + user.client_id = id + user.is_owner = False + employee = EmployeeCreate(**user.dict()) try: created_user = await all_users.create_user(employee, safe=True) except Exception: @@ -51,7 +50,8 @@ async def add_employee(id: int, user: UserCreate): status_code=status.HTTP_400_BAD_REQUEST, detail=ErrorCode.REGISTER_USER_ALREADY_EXISTS, ) - + message = f"Добро пожаловать в UDV Service Desk!\n\nВаш логин в системе: {user.email}\nВаш пароль: {user.password}" + await send_mail(user.email, "Вы зарегистрированы в системе", message) return created_user diff --git a/src/service.py b/src/service.py index f98bfba..371ca3c 100644 --- a/src/service.py +++ b/src/service.py @@ -1,6 +1,31 @@ +import asyncio +import smtplib +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart + +from src.config import MAIL_LOGIN, MAIL_PWD async def check_dict(result): if result: return dict(result) return None + + +async def send_mail(to_addr: str, subject: str, msg: str): + multipart_msg = MIMEMultipart() + multipart_msg['From'] = MAIL_LOGIN + multipart_msg['To'] = to_addr + multipart_msg['Subject'] = subject + multipart_msg.attach(MIMEText(msg, 'plain')) + + smtp_obj = smtplib.SMTP('smtp.gmail.com', 587) + smtp_obj.starttls() + smtp_obj.login(MAIL_LOGIN, MAIL_PWD) + smtp_obj.send_message(multipart_msg) + smtp_obj.quit() + print("письмо отправлено") + +if __name__ == '__main__': + print("Hello") + asyncio.run(send_mail("puzanovim@yandex.ru", "HELLO", "Hello Hello"))