Improve code

pull/18/head
Yax 9 months ago
parent 83aa8b1ba2
commit 885593db5b

@ -37,9 +37,9 @@ def stacosys_server(config_pathname):
# load and check config
config.load(config_pathname)
is_config_ok, erreur_config = config.check()
is_config_ok, config_error = config.check()
if not is_config_ok:
logger.error("Configuration incorrecte '%s'", erreur_config)
logger.error("Invalid configuration '%s'", config_error)
sys.exit(1)
logger.info(config)
@ -65,7 +65,9 @@ def stacosys_server(config_pathname):
config.get(ConfigParameter.SMTP_PASSWORD),
)
mailer.configure_destination(config.get(ConfigParameter.SITE_ADMIN_EMAIL))
mailer.check()
if not mailer.check():
logger.error("Email configuration not working")
sys.exit(1)
logger.info("start interfaces %s %s %s", api, form, admin)

@ -2,44 +2,39 @@
# -*- coding: utf-8 -*-
import logging
import smtplib
import ssl
from email.mime.text import MIMEText
from smtplib import SMTP_SSL, SMTPAuthenticationError
logger = logging.getLogger(__name__)
class Mailer:
def __init__(self) -> None:
self._smtp_host: str = ""
self._smtp_port: int = 0
self._smtp_login: str = ""
self._smtp_password: str = ""
self._site_admin_email: str = ""
def configure_smtp(
self,
smtp_host,
smtp_port,
smtp_login,
smtp_password,
) -> None:
self._smtp_host = ""
self._smtp_port = 0
self._smtp_login = ""
self._smtp_password = ""
self._site_admin_email = ""
def configure_smtp(self, smtp_host: str, smtp_port: int, smtp_login: str, smtp_password: str) -> None:
self._smtp_host = smtp_host
self._smtp_port = smtp_port
self._smtp_login = smtp_login
self._smtp_password = smtp_password
def configure_destination(self, site_admin_email) -> None:
def configure_destination(self, site_admin_email: str) -> None:
self._site_admin_email = site_admin_email
def check(self):
server = smtplib.SMTP_SSL(
self._smtp_host, self._smtp_port, context=ssl.create_default_context()
)
server.login(self._smtp_login, self._smtp_password)
server.close()
def send(self, subject, message) -> bool:
def check(self) -> bool:
try:
with SMTP_SSL(self._smtp_host, self._smtp_port) as server:
server.login(self._smtp_login, self._smtp_password)
return True
except SMTPAuthenticationError:
logger.exception("Invalid credentials")
return False
def send(self, subject: str, message: str) -> bool:
sender = self._smtp_login
receivers = [self._site_admin_email]
@ -48,15 +43,14 @@ class Mailer:
msg["To"] = self._site_admin_email
msg["From"] = sender
# pylint: disable=bare-except
try:
server = smtplib.SMTP_SSL(
self._smtp_host, self._smtp_port, context=ssl.create_default_context()
)
server.login(self._smtp_login, self._smtp_password)
server.send_message(msg, sender, receivers)
server.close()
success = True
except:
success = False
return success
with SMTP_SSL(self._smtp_host, self._smtp_port) as server:
server.login(self._smtp_login, self._smtp_password)
server.send_message(msg, sender, receivers)
return True
except SMTPAuthenticationError:
logger.exception("Invalid credentials")
return False
except Exception as e:
logger.exception(f"Error sending email: {e}")
return False

Loading…
Cancel
Save