Optionally add Cc to the email

This commit is contained in:
sfigato 2022-09-20 22:29:59 +02:00
parent 365105f4b4
commit f3ed2b0cc9
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
3 changed files with 12 additions and 4 deletions

View File

@ -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 [

View File

@ -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():

View File

@ -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"))