Skip to content

Commit

Permalink
perf($email): only login and logout from the email server once
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Nov 10, 2021
1 parent 31edd92 commit cef9cf9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
22 changes: 17 additions & 5 deletions home_guardian/message/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from loguru import logger

from home_guardian.common.debounce_throttle import debounce
from home_guardian.configuration.application_configuration import application_conf
from home_guardian.template.html_template import render_template

Expand All @@ -18,6 +19,12 @@
)
_receivers: List[str] = application_conf.get_list("email.receivers")

# Login to the email server
_smtp: smtplib.SMTP = smtplib.SMTP(_mail_host, 25)
_smtp.connect(_mail_host, 25)
_smtp.login(_sender, _mail_password)
logger.warning(f"Logged in to the email server: {_mail_host}")


def build_message(
subject: str,
Expand Down Expand Up @@ -55,6 +62,7 @@ def build_message(
return message


@debounce(30)
def send_email(
subject: str, template_name: str, render_dict: dict, picture_path: str
) -> None:
Expand All @@ -66,19 +74,23 @@ def send_email(
:param render_dict: The render dict for the template.
:param picture_path: The path to the picture to attach.
"""
smtp: smtplib.SMTP = smtplib.SMTP(_mail_host, 25)
smtp.connect(_mail_host, 25)
smtp.login(_sender, _mail_password)
for receiver in _receivers:
message: MIMEMultipart = build_message(
subject, receiver, template_name, render_dict, picture_path
)
logger.debug(f"Sending email. receiver: {receiver}")
try:
smtp.sendmail(_sender, [receiver], message.as_string())
_smtp.sendmail(_sender, [receiver], message.as_string())
logger.debug(
f"Sent email successfully. Receiver: {receiver}, subject: {subject}, template_name: {template_name}"
)
except smtplib.SMTPException:
logger.exception("Exception occurred while sending email!")
smtp.quit()


def cleanup() -> None:
"""
Closes the connection to the email server.
"""
_smtp.quit()
logger.warning(f"Logged out from the email server: {_mail_host}")
1 change: 0 additions & 1 deletion home_guardian/opencv/face_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,3 @@ def async_save_capture(frame) -> None:
render_dict,
detected_face.picture_path,
)
logger.info("Sent `security_warning` successfully")

0 comments on commit cef9cf9

Please sign in to comment.