add Mailing Service
This commit is contained in:
parent
c20d437b55
commit
364f11f633
@ -4,3 +4,10 @@
|
||||
db_host="127.0.0.1"
|
||||
db_port=3306
|
||||
db_name="ez_lan_manager"
|
||||
|
||||
[mailing]
|
||||
smtp_server=""
|
||||
smtp_port=587
|
||||
sender=""
|
||||
username=""
|
||||
password=""
|
||||
|
||||
@ -9,6 +9,7 @@ from src.ez_lan_manager.services.DatabaseService import DatabaseService
|
||||
|
||||
from random import randint
|
||||
|
||||
from src.ez_lan_manager.services.MailingService import MailingService
|
||||
from src.ez_lan_manager.services.NewsService import NewsService
|
||||
from src.ez_lan_manager.services.UserService import UserService
|
||||
from src.ez_lan_manager.types.News import News
|
||||
@ -25,7 +26,12 @@ if __name__ == "__main__":
|
||||
user_service = UserService(db_service)
|
||||
accounting_service = AccountingService(db_service)
|
||||
news_service = NewsService(db_service)
|
||||
print(news_service.get_latest_news())
|
||||
mailing_service = MailingService(configuration_service.get_mailing_service_configuration())
|
||||
#mailing_service.send_email("Hallo von EZ LAN Mananger", "Grüße :)", "davidr.develop@gmail.com")
|
||||
|
||||
|
||||
#user_service.create_user("Alex", "alex@gmail.com", "MeinPasswort")
|
||||
#print(user_service.is_login_valid("Alex@gmail.com", "MeinPasswort"))
|
||||
|
||||
# news_service.add_news(News(
|
||||
# news_id=None,
|
||||
|
||||
@ -3,7 +3,7 @@ from pathlib import Path
|
||||
import logging
|
||||
import tomllib
|
||||
|
||||
from src.ez_lan_manager.types.ConfigurationTypes import DatabaseConfiguration
|
||||
from src.ez_lan_manager.types.ConfigurationTypes import DatabaseConfiguration, MailingServiceConfiguration
|
||||
|
||||
logger = logging.getLogger(__name__.split(".")[-1])
|
||||
|
||||
@ -29,3 +29,18 @@ class ConfigurationService:
|
||||
except KeyError:
|
||||
logger.fatal("Error loading DatabaseConfiguration, exiting...")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def get_mailing_service_configuration(self) -> MailingServiceConfiguration:
|
||||
try:
|
||||
database_configuration = self._config["mailing"]
|
||||
return MailingServiceConfiguration(
|
||||
smtp_server=database_configuration["smtp_server"],
|
||||
smtp_port=database_configuration["smtp_port"],
|
||||
sender=database_configuration["sender"],
|
||||
username=database_configuration["username"],
|
||||
password=database_configuration["password"]
|
||||
)
|
||||
except KeyError:
|
||||
logger.fatal("Error loading MailingServiceConfiguration, exiting...")
|
||||
sys.exit(1)
|
||||
|
||||
30
src/ez_lan_manager/services/MailingService.py
Normal file
30
src/ez_lan_manager/services/MailingService.py
Normal file
@ -0,0 +1,30 @@
|
||||
import logging
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from smtplib import SMTP
|
||||
|
||||
from src.ez_lan_manager.types.ConfigurationTypes import MailingServiceConfiguration
|
||||
|
||||
logger = logging.getLogger(__name__.split(".")[-1])
|
||||
|
||||
class MailingService:
|
||||
def __init__(self, configuration: MailingServiceConfiguration):
|
||||
self._config = configuration
|
||||
|
||||
def send_email(self, subject: str, body: str, receiver: str) -> None:
|
||||
# ToDo: Check with Rio/FastAPI if this needs to be ASYNC
|
||||
try:
|
||||
msg = MIMEMultipart()
|
||||
msg['From'] = self._config.sender
|
||||
msg['To'] = receiver
|
||||
msg['Subject'] = subject
|
||||
|
||||
msg.attach(MIMEText(body, 'plain'))
|
||||
|
||||
with SMTP(self._config.smtp_server, self._config.smtp_port) as server:
|
||||
server.starttls()
|
||||
server.login(self._config.username, self._config.password)
|
||||
server.sendmail(self._config.sender, receiver, msg.as_string())
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to send email: {e}")
|
||||
@ -7,3 +7,11 @@ class DatabaseConfiguration:
|
||||
db_host: str
|
||||
db_port: int
|
||||
db_name: str
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MailingServiceConfiguration:
|
||||
smtp_server: str
|
||||
smtp_port: int
|
||||
sender: str
|
||||
username: str
|
||||
password: str
|
||||
|
||||
Loading…
Reference in New Issue
Block a user