web: add smtp-based email delivery
email.py stolen from ldapweb
This commit is contained in:
parent
1750d30b29
commit
0fdf969295
3 changed files with 58 additions and 13 deletions
|
@ -24,6 +24,10 @@ LDAP_USER_BASE = env.str("LDAP_USER_BASE", "ou=People,dc=hackerspace,dc=pl")
|
|||
LDAP_GROUP_FILTER = env.str("LDAP_GROUP_FILTER", "(objectClass=groupOfUniqueNames)")
|
||||
LDAP_GROUP_BASE = env.str("LDAP_GROUP_BASE", "ou=Group,dc=hackerspace,dc=pl")
|
||||
|
||||
SMTP_SERVER = env.str("SMTP_SERVER", "mail.hackerspace.pl")
|
||||
SMTP_USER = env.str("SMTP_USER", "kasownik")
|
||||
SMTP_PASSWORD = env.str("SMTP_PASSWORD", "changeme")
|
||||
|
||||
CACHE_TYPE = env.str("CACHE_TYPE", "null")
|
||||
CACHE_NO_NULL_WARNING = True
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ from flask import (
|
|||
current_app,
|
||||
)
|
||||
from flask_login import login_required
|
||||
from webapp import forms, db, models, admin_required
|
||||
from webapp import forms, db, models, admin_required, email
|
||||
|
||||
from . import directory
|
||||
from . import logic
|
||||
|
@ -315,24 +315,19 @@ def sendspam():
|
|||
if not content.strip():
|
||||
continue
|
||||
|
||||
msg = MIMEText(content, "plain", "utf-8")
|
||||
msg["From"] = "Kasownik Hackerspace'owy <kasownik@hackerspace.pl>"
|
||||
msg["Subject"] = "Stan składek na dzień %s" % now.strftime("%d/%m/%Y")
|
||||
msg["To"] = member.get_contact_email()
|
||||
spam.append(msg)
|
||||
recipient_email = member.get_contact_email()
|
||||
spam.append((recipient_email, content))
|
||||
|
||||
if form.dry_run.data:
|
||||
readable = [
|
||||
msg.as_string().split("\n\n")[0]
|
||||
+ "\n\n"
|
||||
+ msg.get_payload(decode=True).decode("utf-8")
|
||||
for msg in spam
|
||||
f"To: {recipient_email}\n\n{content}"
|
||||
for recipient_email, content in spam
|
||||
]
|
||||
return Response("\n====\n".join(readable), mimetype="text/plain")
|
||||
|
||||
for msg in spam:
|
||||
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
|
||||
p.communicate(msg.as_bytes())
|
||||
smpt_conn = email.get_connection()
|
||||
for recipient_email, content in spam:
|
||||
email.send_email(smpt_conn, "Reminder about your Warsaw Hackerspace membership fees", content, recipient_email)
|
||||
|
||||
flash("%d messages sent!" % len(spam))
|
||||
return redirect(url_for(".index"))
|
||||
|
|
46
web/webapp/email.py
Normal file
46
web/webapp/email.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
import smtplib
|
||||
from email.message import EmailMessage
|
||||
import flask
|
||||
import datetime
|
||||
from typing import Optional
|
||||
|
||||
from webapp import app
|
||||
|
||||
cached_connection: Optional[smtplib.SMTP] = None
|
||||
|
||||
|
||||
def test_connection_open(conn: smtplib.SMTP) -> bool:
|
||||
try:
|
||||
status = conn.noop()[0]
|
||||
except:
|
||||
status = -1
|
||||
return True if status == 250 else False
|
||||
|
||||
|
||||
def create_connection() -> smtplib.SMTP:
|
||||
print("Connecting to SMTP...")
|
||||
conn = smtplib.SMTP_SSL(app.config["SMTP_SERVER"])
|
||||
conn.login(app.config["SMTP_USER"], app.config["SMTP_PASSWORD"])
|
||||
return conn
|
||||
|
||||
|
||||
def get_connection() -> smtplib.SMTP:
|
||||
global cached_connection
|
||||
if cached_connection is not None and test_connection_open(cached_connection):
|
||||
return cached_connection
|
||||
|
||||
cached_connection = create_connection()
|
||||
return cached_connection
|
||||
|
||||
|
||||
def send_email(
|
||||
conn: smtplib.SMTP, subject: str, body: str, recipient_emails: str
|
||||
) -> None:
|
||||
msg = EmailMessage()
|
||||
msg.set_content(body)
|
||||
msg["Subject"] = subject
|
||||
|
||||
msg["From"] = f"Warsaw Hackerspace Kasownik <kasownik@hackerspace.pl>"
|
||||
msg["To"] = recipient_emails
|
||||
|
||||
conn.send_message(msg)
|
Loading…
Reference in a new issue