From a90cbd217e1e75c520c406e7f9fd9ab1e26cd201 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Mon, 20 Apr 2020 10:47:56 +0200 Subject: [PATCH] Set headers for no auto response on most emails Most of our auto-generated emails should not ask for auto replies (like out of office messages or in particular, "held for moderation" notices from our own list server), so set this header by default, and also the header indicating if it's an auto submitted/auto replied message. Specifically allow auto replies on moderation notices, since that's a case where it might be really interesting for the moderator to see for example an out of office message. At least for now that seems like a good idea. --- pgweb/mailqueue/util.py | 12 +++++++++++- pgweb/util/admin.py | 3 ++- pgweb/util/misc.py | 16 ++++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pgweb/mailqueue/util.py b/pgweb/mailqueue/util.py index cd5e3eec..e2f3f7c6 100644 --- a/pgweb/mailqueue/util.py +++ b/pgweb/mailqueue/util.py @@ -15,7 +15,7 @@ def _encoded_email_header(name, email): return email -def send_simple_mail(sender, receiver, subject, msgtxt, attachments=None, usergenerated=False, cc=None, replyto=None, sendername=None, receivername=None, messageid=None): +def send_simple_mail(sender, receiver, subject, msgtxt, attachments=None, usergenerated=False, cc=None, replyto=None, sendername=None, receivername=None, messageid=None, suppress_auto_replies=True, is_auto_reply=False): # attachment format, each is a tuple of (name, mimetype,contents) # content should be *binary* and not base64 encoded, since we need to # use the base64 routines from the email library to get a properly @@ -33,6 +33,16 @@ def send_simple_mail(sender, receiver, subject, msgtxt, attachments=None, userge msg['Message-ID'] = messageid else: msg['Message-ID'] = make_msgid() + if suppress_auto_replies: + # Do our best to set some headers to indicate that auto-replies like out of office + # messages should not be sent to this email. + msg['X-Auto-Response-Suppress'] = 'All' + + # Is this email auto-generated or auto-replied? + if is_auto_reply: + msg['Auto-Submitted'] = 'auto-replied' + elif not usergenerated: + msg['Auto-Submitted'] = 'auto-generated' msg.attach(MIMEText(msgtxt, _charset='utf-8')) diff --git a/pgweb/util/admin.py b/pgweb/util/admin.py index f86a692d..9e02eddb 100644 --- a/pgweb/util/admin.py +++ b/pgweb/util/admin.py @@ -78,7 +78,8 @@ class PgwebAdmin(admin.ModelAdmin): send_simple_mail(settings.NOTIFICATION_FROM, obj.org.email, "postgresql.org moderation notification", - msgstr) + msgstr, + suppress_auto_replies=False) # Also generate a mail to the moderators send_simple_mail( diff --git a/pgweb/util/misc.py b/pgweb/util/misc.py index 5d45b7a0..2698031e 100644 --- a/pgweb/util/misc.py +++ b/pgweb/util/misc.py @@ -9,16 +9,20 @@ from pgweb.util.helpers import template_to_string import re -def send_template_mail(sender, receiver, subject, templatename, templateattr={}, usergenerated=False, cc=None, replyto=None, receivername=None, sendername=None, messageid=None): +def send_template_mail(sender, receiver, subject, templatename, templateattr={}, usergenerated=False, cc=None, replyto=None, receivername=None, sendername=None, messageid=None, suppress_auto_replies=True, is_auto_reply=False): d = { 'link_root': settings.SITE_ROOT, } d.update(templateattr) - send_simple_mail(sender, receiver, subject, - template_to_string(templatename, d), - usergenerated=usergenerated, cc=cc, replyto=replyto, - receivername=receivername, sendername=sendername, - messageid=messageid) + send_simple_mail( + sender, receiver, subject, + template_to_string(templatename, d), + usergenerated=usergenerated, cc=cc, replyto=replyto, + receivername=receivername, sendername=sendername, + messageid=messageid, + suppress_auto_replies=suppress_auto_replies, + is_auto_reply=is_auto_reply, + ) def get_client_ip(request):