Move send_mail() to own module

This commit is contained in:
Edward Betts 2024-07-08 10:44:39 +01:00
parent 19a9015dba
commit ff51bb9ff9
2 changed files with 36 additions and 31 deletions

28
agenda/mail.py Normal file
View file

@ -0,0 +1,28 @@
"""Send e-mail."""
import smtplib
from email.message import EmailMessage
from email.utils import formatdate, make_msgid
import flask
def send_mail(config: flask.config.Config, subject: str, body: str) -> None:
"""Send an e-mail."""
msg = EmailMessage()
msg["Subject"] = subject
msg["To"] = f"{config['NAME']} <{config['MAIL_TO']}>"
msg["From"] = f"{config['NAME']} <{config['MAIL_FROM']}>"
msg["Date"] = formatdate()
msg["Message-ID"] = make_msgid()
# Add extra mail headers
for header, value in config["MAIL_HEADERS"]:
msg[header] = value
msg.set_content(body)
s = smtplib.SMTP(config["SMTP_HOST"])
s.sendmail(config["MAIL_TO"], [config["MAIL_TO"]], msg.as_string())
s.quit()

View file

@ -4,23 +4,21 @@
import asyncio import asyncio
import json import json
import os import os
import smtplib
import sys import sys
import typing import typing
from datetime import date, datetime from datetime import date, datetime
from email.message import EmailMessage
from email.utils import formatdate, make_msgid
from time import time from time import time
import flask import flask
import requests import requests
import agenda.fx import agenda.fx
import agenda.gwr
import agenda.mail
import agenda.thespacedevs import agenda.thespacedevs
import agenda.types import agenda.types
import agenda.uk_holiday import agenda.uk_holiday
import agenda.waste_schedule import agenda.waste_schedule
from agenda import gwr
from agenda.types import StrDict from agenda.types import StrDict
from web_view import app from web_view import app
@ -50,37 +48,16 @@ async def update_bristol_bins(config: flask.config.Config) -> None:
print(f"took {time_taken:.1f} seconds") print(f"took {time_taken:.1f} seconds")
def send_mail(config: flask.config.Config, subject: str, body: str) -> None:
"""Send an e-mail."""
msg = EmailMessage()
msg["Subject"] = subject
msg["To"] = f"{config['NAME']} <{config['MAIL_TO']}>"
msg["From"] = f"{config['NAME']} <{config['MAIL_FROM']}>"
msg["Date"] = formatdate()
msg["Message-ID"] = make_msgid()
# Add extra mail headers
for header, value in config["MAIL_HEADERS"]:
msg[header] = value
msg.set_content(body)
s = smtplib.SMTP(config["SMTP_HOST"])
s.sendmail(config["MAIL_TO"], [config["MAIL_TO"]], msg.as_string())
s.quit()
def update_gwr_advance_ticket_date(config: flask.config.Config) -> None: def update_gwr_advance_ticket_date(config: flask.config.Config) -> None:
"""Update GWR advance ticket date cache.""" """Update GWR advance ticket date cache."""
filename = os.path.join(config["DATA_DIR"], "advance-tickets.html") filename = os.path.join(config["DATA_DIR"], "advance-tickets.html")
existing_html = open(filename).read() existing_html = open(filename).read()
existing_date = gwr.extract_weekday_date(existing_html) existing_date = agenda.gwr.extract_weekday_date(existing_html)
new_html = requests.get(gwr.url).text new_html = requests.get(agenda.gwr.url).text
open(filename, "w").write(new_html) open(filename, "w").write(new_html)
new_date = gwr.extract_weekday_date(new_html) new_date = agenda.gwr.extract_weekday_date(new_html)
if existing_date == new_date: if existing_date == new_date:
if sys.stdin.isatty(): if sys.stdin.isatty():
@ -91,11 +68,11 @@ def update_gwr_advance_ticket_date(config: flask.config.Config) -> None:
body = f"""Old date: {existing_date} body = f"""Old date: {existing_date}
New date: {new_date} New date: {new_date}
{gwr.url} {agenda.gwr.url}
Agenda: https://edwardbetts.com/agenda/ Agenda: https://edwardbetts.com/agenda/
""" """
send_mail(config, subject, body) agenda.mail.send_mail(config, subject, body)
def report_space_launch_change( def report_space_launch_change(
@ -121,7 +98,7 @@ New launch data
{json.dumps(cur_launch, indent=2)} {json.dumps(cur_launch, indent=2)}
""" """
send_mail(config, subject, body) agenda.mail.send_mail(config, subject, body)
def get_launch_by_slug(data: StrDict, slug: str) -> StrDict | None: def get_launch_by_slug(data: StrDict, slug: str) -> StrDict | None: