From f3ed2b0cc947c601b3a42156657c84a6640aa6cc Mon Sep 17 00:00:00 2001 From: Blallo Date: Tue, 20 Sep 2022 22:29:59 +0200 Subject: [PATCH] Optionally add Cc to the email --- latecomers/config.py | 2 ++ latecomers/main.py | 4 ++-- latecomers/notifier.py | 10 ++++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/latecomers/config.py b/latecomers/config.py index f4befcd..a99eabd 100644 --- a/latecomers/config.py +++ b/latecomers/config.py @@ -20,6 +20,7 @@ def get_section(obj: T.Dict[T.Text, T.Any], key: T.Text) -> T.Any: class Config(object): smtp: T.Dict[T.Text, T.Any] = {} to: T.List[T.Text] = [] + cc: T.List[T.Text] = [] store: T.Optional[T.Text] = None def __init__(self, path: T.Text) -> None: @@ -34,6 +35,7 @@ class Config(object): content = yaml.safe_load(f) self.to = get_section(content, "to") + self.cc = get_section(content, "cc") smtp = get_section(content, "smtp") for key in [ diff --git a/latecomers/main.py b/latecomers/main.py index 1fc3acd..f113df8 100644 --- a/latecomers/main.py +++ b/latecomers/main.py @@ -32,10 +32,10 @@ def main(config: Config): data.append(get_details(row, aux_data)) if not data: - out.send_no_data(config.to) + out.send_no_data(config.to, config.cc) excel = to_excel(data, config.store) - out.send_result(config.to, excel) + out.send_result(config.to, config.cc, excel) def cli(): diff --git a/latecomers/notifier.py b/latecomers/notifier.py index 269327e..9e45517 100644 --- a/latecomers/notifier.py +++ b/latecomers/notifier.py @@ -55,13 +55,17 @@ class Notifier(object): conn.sendmail(self._from, to, email) @retry_and_log(logger, RETRIES) - def send_result(self, to: T.List[T.Text], result: bytes) -> None: + def send_result( + self, to: T.List[T.Text], cc: T.List[T.Text], result: bytes + ) -> None: date = get_date() body = f"Resoconto dei voli dal sito di AdR per l'aereoporto di Ciampino in data {date}" # noqa: E501 message = MIMEMultipart() message["From"] = self._from message["To"] = ",".join(to) + if cc: + message["Cc"] = ",".join(cc) message["Subject"] = f"[{date}] Resoconto CIA da AdR" message.attach(MIMEText(body, "plain")) @@ -81,7 +85,7 @@ class Notifier(object): self.send(to, email) @retry_and_log(logger, RETRIES) - def send_no_data(self, to: T.List[T.Text]) -> None: + def send_no_data(self, to: T.List[T.Text], cc: T.List[T.Text]) -> None: date = get_date() body = f"""Attenzione @@ -93,6 +97,8 @@ il vostro tecnico preferito. message = MIMEMultipart() message["From"] = self._from message["To"] = ",".join(to) + if cc: + message["Cc"] = ",".join(cc) message["Subject"] = f"ATTENZIONE: [{date}] Resoconto CIA da AdR" message.attach(MIMEText(body, "plain"))