Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an email to SR2025 secondary contacts #193

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/.env
/*.csv
node_modules/
28 changes: 28 additions & 0 deletions SR2025/2024-09-28-secondary-contact-info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
to: Student Robotics 2025 secondary contacts, CC primary contacts
subject: "Student Robotics 2025: you are a secondary contact"
---

Hi {SecondaryGivenName},

As you're hopefully aware from {PrimaryName},
[Student Robotics][student-robotics] is an exciting competition for
16-19 year-olds which a team from {SchoolName} is taking part in.

{PrimaryGivenName} gave us your details as a secondary contact at
{SchoolName} in the unlikely case that we are unable to contact them.
Please speak to them in the first instance if you have any questions.

As [team supervisor][team-supervisor], {PrimaryGivenName} is
responsible for your competitors as well as the [kit][kit] which we loan to each
team. Our communication with your team will also all be sent to them.

If you would like to be copied into our emails with your team then feel free to
let us know.

Thanks,\
-- Student Robotics

[student-robotics]: https://studentrobotics.org
[kit]: https://studentrobotics.org/docs/kit/
[team-supervisor]: https://studentrobotics.org/docs/robots_101/team_supervisor
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
],
"dictionaries": ["en-gb", "project-words"],
"ignorePaths": [
"*.csv",
"node_modules",
"/legacy",
"/scripts/requirements.txt",
"/.spelling"
]
}
2 changes: 2 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
markdown>=3.7,<4
pyyaml>=6,<7
129 changes: 129 additions & 0 deletions scripts/send.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import argparse
import csv
import dataclasses
import functools
import html
import os
import smtplib
import time
from collections.abc import Collection
from email.headerregistry import Address
from email.mime.text import MIMEText
from pathlib import Path

import markdown
import yaml

SMTP_USERNAME = os.environ['SMTP_USERNAME']
SMTP_PASSWORD = os.environ['SMTP_PASSWORD']
SMTP_HOST = os.environ.get('SMTP_HOST', 'smtp.gmail.com')

SR_TEAMS_EMAIL = '[email protected]'
SR_TEAMS_ADDRESS = Address("Student Robotics Teams", addr_spec=SR_TEAMS_EMAIL)

FROM_EMAIL = SR_TEAMS_EMAIL
FROM_ADDRESS = SR_TEAMS_ADDRESS


@dataclasses.dataclass
class Template:
to: str
subject: str
body_raw: str

@functools.cached_property
def body(self) -> str:
return markdown.markdown(self.body_raw).replace('\\\n', '<br>')

def template(self, mapping: dict[str, str]) -> str:
# Note: this requires manual adjustment of the template and CSV file to
# have matching names, but that seems fine for now. Perhaps we should
# move to using Jinja templating or something?
return self.body.format(**{
k: html.escape(v)
for k, v in mapping.items()
})


def load_template(path: Path) -> Template:
with path.open() as f:
docs = yaml.load_all(f, Loader=yaml.CLoader)
metadata = next(docs)

f.seek(0)
content = f.read()
body = content.split('---\n')[-1]
return Template(
to=metadata['to'],
subject=metadata['subject'],
body_raw=body,
)


def send(
server: smtplib.SMTP,
*,
to: Address,
cc: Collection[Address] = (),
from_: Address = FROM_ADDRESS,
subject: str,
html_body: str,
dry_run: bool,
) -> None:
message = MIMEText(html_body, 'html')
message['Subject'] = subject
message['From'] = str(from_)
message['To'] = str(to)
message['CC'] = ', '.join(str(x) for x in cc)

print(f"Sending to {to!r}, cc {cc!r}")
if dry_run:
print('===')
print(message)
print('===')
else:
server.sendmail(str(from_), str(to), message.as_string())
print(f"Sent to {to.addr_spec!r}")


def main(args: argparse.Namespace) -> None:
template = load_template(args.template)

with smtplib.SMTP_SSL(SMTP_HOST, 465) as server:
server.login(SMTP_USERNAME, SMTP_PASSWORD)

with args.teams_csv.open() as f:
reader = csv.DictReader(f)
for row in reader:
send(
server,
to=Address(
row['SecondaryName'],
addr_spec=row['SecondaryEmailAddress'],
),
cc=[
Address(
row['PrimaryName'],
addr_spec=row['PrimaryEmailAddress'],
),
SR_TEAMS_ADDRESS,
],
subject=template.subject,
html_body=template.template(row),
dry_run=args.dry_run
)

# Rate limit for GMail
time.sleep(5)


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('template', type=Path)
parser.add_argument('teams_csv', type=Path)
parser.add_argument('--dry-run', action='store_true', default=False)
return parser.parse_args()


if __name__ == '__main__':
main(parse_args())