104 lines
3.8 KiB
Python
Executable File
104 lines
3.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""mail_wave — grant applications straight to the intake mailbox.
|
|
|
|
Use it when the site form is broken (it answers 503 "your message could not be
|
|
sent" while its transactional mail provider is down). The message body reuses
|
|
the persona generator from form_intake.py.
|
|
|
|
SMTP credentials are yours: put them in config.json under "smtp" or in the
|
|
environment (SMTP_HOST / SMTP_PORT / SMTP_USER / SMTP_PASS). Nothing is stored
|
|
by this script.
|
|
|
|
python mail_wave.py --n 20 --pause 25 60
|
|
"""
|
|
import argparse
|
|
import email.utils
|
|
import json
|
|
import os
|
|
import random
|
|
import smtplib
|
|
import ssl
|
|
import sys
|
|
import time
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from form_intake import load_config, persona # noqa: E402
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
SUBJECTS = [
|
|
"Grant application - {org} - {chain} hackathon",
|
|
"Grant application - {org} - {chain} security build",
|
|
"Grant request: {org} ({chain})",
|
|
"Grant application, {org}, {chain}",
|
|
]
|
|
|
|
|
|
def smtp_settings(cfg):
|
|
s = cfg.get("smtp") or {}
|
|
return {
|
|
"host": s.get("host") or os.environ.get("SMTP_HOST", "smtp.gmail.com"),
|
|
"port": int(s.get("port") or os.environ.get("SMTP_PORT", 587)),
|
|
"user": s.get("user") or os.environ.get("SMTP_USER", ""),
|
|
"pass": s.get("pass") or os.environ.get("SMTP_PASS", ""),
|
|
}
|
|
|
|
|
|
def build(per):
|
|
subj = random.choice(SUBJECTS).format(org=per["organization"], chain=per["chain"])
|
|
body = ("Hello,\n\n"
|
|
"The grant form answers \"Your message could not be sent\" right now, "
|
|
"so sending the application by mail as that error suggests.\n\n"
|
|
+ per["message"] + "\n\n"
|
|
"We can share the test plan, target ownership proof and reporting format on request.\n\n"
|
|
"Regards,\n" + per["name"] + "\n" + per["organization"] + "\n")
|
|
return subj, body
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--config", default="config.json")
|
|
ap.add_argument("--n", type=int, default=20)
|
|
ap.add_argument("--to", default="")
|
|
ap.add_argument("--pause", type=float, nargs=2, default=[25, 60])
|
|
ap.add_argument("--out", default="mail.jsonl")
|
|
a = ap.parse_args()
|
|
cfg = load_config(a.config)
|
|
s = smtp_settings(cfg)
|
|
to = a.to or cfg.get("mail_to") or ""
|
|
if not (s["user"] and s["pass"] and to):
|
|
print("missing smtp credentials or recipient; set them in config.json or env")
|
|
return 2
|
|
sent = 0
|
|
for i in range(a.n):
|
|
per = persona(cfg)
|
|
subj, body = build(per)
|
|
msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n"
|
|
"MIME-Version: 1.0\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n%s") % (
|
|
s["user"], to, subj, email.utils.formatdate(localtime=True), body)
|
|
rec = {"ts": int(time.time()), "to": to, "org": per["organization"],
|
|
"tier": per["tier"], "chain": per["chain"], "subject": subj}
|
|
try:
|
|
c = smtplib.SMTP(s["host"], s["port"], timeout=30)
|
|
c.ehlo()
|
|
c.starttls(context=ssl.create_default_context())
|
|
c.ehlo()
|
|
c.login(s["user"], s["pass"])
|
|
c.sendmail(s["user"], [to], msg.encode("utf-8"))
|
|
c.quit()
|
|
sent += 1
|
|
rec["ok"] = True
|
|
print("[%d] sent %s $%s" % (i + 1, per["organization"], per["tier"]), flush=True)
|
|
except Exception as e:
|
|
rec["ok"] = False
|
|
rec["err"] = str(e)[:160]
|
|
print("[%d] fail %s" % (i + 1, str(e)[:90]), flush=True)
|
|
with open(a.out, "a", encoding="utf-8") as fh:
|
|
fh.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
|
time.sleep(random.uniform(*a.pause))
|
|
print(json.dumps({"sent": sent, "of": a.n}))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |