Improve code
This commit is contained in:
parent
83aa8b1ba2
commit
885593db5b
2 changed files with 32 additions and 36 deletions
|
@ -37,9 +37,9 @@ def stacosys_server(config_pathname):
|
||||||
|
|
||||||
# load and check config
|
# load and check config
|
||||||
config.load(config_pathname)
|
config.load(config_pathname)
|
||||||
is_config_ok, erreur_config = config.check()
|
is_config_ok, config_error = config.check()
|
||||||
if not is_config_ok:
|
if not is_config_ok:
|
||||||
logger.error("Configuration incorrecte '%s'", erreur_config)
|
logger.error("Invalid configuration '%s'", config_error)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
logger.info(config)
|
logger.info(config)
|
||||||
|
|
||||||
|
@ -65,7 +65,9 @@ def stacosys_server(config_pathname):
|
||||||
config.get(ConfigParameter.SMTP_PASSWORD),
|
config.get(ConfigParameter.SMTP_PASSWORD),
|
||||||
)
|
)
|
||||||
mailer.configure_destination(config.get(ConfigParameter.SITE_ADMIN_EMAIL))
|
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)
|
logger.info("start interfaces %s %s %s", api, form, admin)
|
||||||
|
|
||||||
|
|
|
@ -2,44 +2,39 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import smtplib
|
|
||||||
import ssl
|
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from smtplib import SMTP_SSL, SMTPAuthenticationError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Mailer:
|
class Mailer:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._smtp_host: str = ""
|
self._smtp_host = ""
|
||||||
self._smtp_port: int = 0
|
self._smtp_port = 0
|
||||||
self._smtp_login: str = ""
|
self._smtp_login = ""
|
||||||
self._smtp_password: str = ""
|
self._smtp_password = ""
|
||||||
self._site_admin_email: str = ""
|
self._site_admin_email = ""
|
||||||
|
|
||||||
def configure_smtp(
|
def configure_smtp(self, smtp_host: str, smtp_port: int, smtp_login: str, smtp_password: str) -> None:
|
||||||
self,
|
|
||||||
smtp_host,
|
|
||||||
smtp_port,
|
|
||||||
smtp_login,
|
|
||||||
smtp_password,
|
|
||||||
) -> None:
|
|
||||||
self._smtp_host = smtp_host
|
self._smtp_host = smtp_host
|
||||||
self._smtp_port = smtp_port
|
self._smtp_port = smtp_port
|
||||||
self._smtp_login = smtp_login
|
self._smtp_login = smtp_login
|
||||||
self._smtp_password = smtp_password
|
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
|
self._site_admin_email = site_admin_email
|
||||||
|
|
||||||
def check(self):
|
def check(self) -> bool:
|
||||||
server = smtplib.SMTP_SSL(
|
try:
|
||||||
self._smtp_host, self._smtp_port, context=ssl.create_default_context()
|
with SMTP_SSL(self._smtp_host, self._smtp_port) as server:
|
||||||
)
|
server.login(self._smtp_login, self._smtp_password)
|
||||||
server.login(self._smtp_login, self._smtp_password)
|
return True
|
||||||
server.close()
|
except SMTPAuthenticationError:
|
||||||
|
logger.exception("Invalid credentials")
|
||||||
|
return False
|
||||||
|
|
||||||
def send(self, subject, message) -> bool:
|
def send(self, subject: str, message: str) -> bool:
|
||||||
sender = self._smtp_login
|
sender = self._smtp_login
|
||||||
receivers = [self._site_admin_email]
|
receivers = [self._site_admin_email]
|
||||||
|
|
||||||
|
@ -48,15 +43,14 @@ class Mailer:
|
||||||
msg["To"] = self._site_admin_email
|
msg["To"] = self._site_admin_email
|
||||||
msg["From"] = sender
|
msg["From"] = sender
|
||||||
|
|
||||||
# pylint: disable=bare-except
|
|
||||||
try:
|
try:
|
||||||
server = smtplib.SMTP_SSL(
|
with SMTP_SSL(self._smtp_host, self._smtp_port) as server:
|
||||||
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.login(self._smtp_login, self._smtp_password)
|
return True
|
||||||
server.send_message(msg, sender, receivers)
|
except SMTPAuthenticationError:
|
||||||
server.close()
|
logger.exception("Invalid credentials")
|
||||||
success = True
|
return False
|
||||||
except:
|
except Exception as e:
|
||||||
success = False
|
logger.exception(f"Error sending email: {e}")
|
||||||
return success
|
return False
|
||||||
|
|
Loading…
Add table
Reference in a new issue