eb4ef2339e
change ruff configurations in pyproject.toml in order to improve docstring format in project
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
import logging
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from exchangelib import Account, FileAttachment, Message
|
|
|
|
from logwatcher.utils import get_or_create_folder
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def send_n2_report(account: Account, summary: str, n2_log_file: Path) -> None:
|
|
"""Send the N2 report by e-mail through EWS.
|
|
|
|
Args:
|
|
account: the EWS account used to send email report from
|
|
summary: text body of the email report
|
|
n2_log_file: path to the n2 report file
|
|
|
|
"""
|
|
try:
|
|
to = os.environ["N2_REPORT_RECIPIENTS"].split(",")
|
|
subject = os.environ["N2_REPORT_SUBJECT"]
|
|
except KeyError as error:
|
|
logger.error(f"{error.args[0]} was not found in environment variable.")
|
|
raise
|
|
|
|
# build file attachment
|
|
logger.info(f"Create attachment based on '{n2_log_file}' file.")
|
|
n2_log_attachment = FileAttachment(name=n2_log_file.name, content=n2_log_file.read_bytes(), is_inline=False)
|
|
logger.info(f"Attachment '{n2_log_attachment.name}' was successfully created.")
|
|
|
|
# get sent folder
|
|
sent_folder = get_or_create_folder(account, "Sent")
|
|
|
|
# create mail
|
|
logger.info(f"Create mail with account='{account.fullname}', recipients='{to}', subject='{subject}'.")
|
|
mail = Message(account=account, to_recipients=to, subject=subject, body=summary)
|
|
mail.attach(n2_log_attachment)
|
|
logger.info(f"Mail '{mail.subject}' was successfully created with attachment '{n2_log_attachment.name}'.")
|
|
|
|
# send and keep a copy to the Logs/Sent folder
|
|
logger.info(f"Attempt to send mail '{mail.subject}'...")
|
|
mail.send(copy_to_folder=sent_folder)
|
|
logger.info(f"Mail '{subject}' was successfully sent to '{to}'.")
|
|
logger.info(f"Mail is saved in '{sent_folder}' folder.") |