From b20bf996ec10f3b5b6616b25558bcd9d4b083a64 Mon Sep 17 00:00:00 2001 From: Florent Daigniere Date: Sat, 19 Nov 2022 18:44:30 +0100 Subject: [PATCH] Fix #2231: make public announcements work --- core/admin/mailu/models.py | 22 +++++++++++++--------- core/admin/mailu/ui/views/base.py | 5 +++-- towncrier/newsfragments/2231.bugfix | 1 + 3 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 towncrier/newsfragments/2231.bugfix diff --git a/core/admin/mailu/models.py b/core/admin/mailu/models.py index 48ce8b33..90dbbbf9 100644 --- a/core/admin/mailu/models.py +++ b/core/admin/mailu/models.py @@ -2,7 +2,6 @@ """ import os -import smtplib import json from datetime import date @@ -420,14 +419,19 @@ class Email(object): def sendmail(self, subject, body): """ send an email to the address """ - f_addr = f'{app.config["POSTMASTER"]}@{idna.encode(app.config["DOMAIN"]).decode("ascii")}' - with smtplib.SMTP(app.config['HOST_AUTHSMTP'], port=10025) as smtp: - to_address = f'{self.localpart}@{idna.encode(self.domain_name).decode("ascii")}' - msg = text.MIMEText(body) - msg['Subject'] = subject - msg['From'] = f_addr - msg['To'] = to_address - smtp.sendmail(f_addr, [to_address], msg.as_string()) + try: + f_addr = f'{app.config["POSTMASTER"]}@{idna.encode(app.config["DOMAIN"]).decode("ascii")}' + ip, port = app.config['HOST_LMTP'].rsplit(':') + with smtplib.LMTP(ip, port=port) as lmtp: + to_address = f'{self.localpart}@{idna.encode(self.domain_name).decode("ascii")}' + msg = text.MIMEText(body) + msg['Subject'] = subject + msg['From'] = f_addr + msg['To'] = to_address + lmtp.sendmail(f_addr, [to_address], msg.as_string()) + return True + except smtplib.SMTPException: + return False @classmethod def resolve_domain(cls, email): diff --git a/core/admin/mailu/ui/views/base.py b/core/admin/mailu/ui/views/base.py index 9b7614e1..1d06464a 100644 --- a/core/admin/mailu/ui/views/base.py +++ b/core/admin/mailu/ui/views/base.py @@ -21,8 +21,9 @@ def announcement(): form = forms.AnnouncementForm() if form.validate_on_submit(): for user in models.User.query.all(): - user.sendmail(form.announcement_subject.data, - form.announcement_body.data) + if not user.sendmail(form.announcement_subject.data, + form.announcement_body.data): + flask.flash('Failed to send to %s' % user.email, 'error') # Force-empty the form form.announcement_subject.data = '' form.announcement_body.data = '' diff --git a/towncrier/newsfragments/2231.bugfix b/towncrier/newsfragments/2231.bugfix new file mode 100644 index 00000000..e710ea6d --- /dev/null +++ b/towncrier/newsfragments/2231.bugfix @@ -0,0 +1 @@ +Make public announcement bypass the filters. They may still time-out before being sent if there is a large number of users.