summaryrefslogtreecommitdiff
path: root/postgresqleu/mailqueue/util.py
diff options
context:
space:
mode:
authorStephen Frost2017-03-21 19:41:50 +0000
committerMagnus Hagander2017-04-17 19:04:45 +0000
commitbc8e3fd31c428c9b9413caef34081cc73195e10b (patch)
tree388a4b9aa5701d4be0da47909a40ece5c1faa3ea /postgresqleu/mailqueue/util.py
parent474ca6bd009f7b33f8ee299fb91daafceef6d50a (diff)
Introduce send_template_mail
This introduces the function of the same name from the pgweb code base (though it isn't identical, to be clear). This function is to be used when rendering a text file into an email. There is a helper function template_to_string() which is also added and can be used if a string result is needed. The primary different here is that the variables set in util/context_processor:settings_context are included in the hash automatically, so those settings do not need to be explicitly set by the callers and the templates can expect them to be available. Using that, this change also includes changes to move away from hard-coded values like 'PostgreSQL Europe' in favor of using the variables set in settings.py/local_settings.py in the text templates. This will allow others to re-use the exisitng templates and not have to develop and maintain their own going forward.
Diffstat (limited to 'postgresqleu/mailqueue/util.py')
-rw-r--r--postgresqleu/mailqueue/util.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/postgresqleu/mailqueue/util.py b/postgresqleu/mailqueue/util.py
index 2f616b66..19952ff9 100644
--- a/postgresqleu/mailqueue/util.py
+++ b/postgresqleu/mailqueue/util.py
@@ -4,8 +4,23 @@ from email.mime.nonmultipart import MIMENonMultipart
from email.Utils import formatdate
from email import encoders
+from postgresqleu.util.context_processors import settings_context
+
+from django.template import Context
+from django.template.loader import get_template
+
from models import QueuedMail
+def template_to_string(templatename, attrs = {}):
+ context = Context(attrs)
+ context.update(settings_context())
+ return get_template(templatename).render(context)
+
+def send_template_mail(sender, receiver, subject, templatename, templateattr={}, attachments=None, bcc=None, sendername=None, receivername=None):
+ send_simple_mail(sender, receiver, subject,
+ template_to_string(templatename, templateattr),
+ attachments, bcc, sendername, receivername)
+
def send_simple_mail(sender, receiver, subject, msgtxt, attachments=None, bcc=None, sendername=None, receivername=None):
# attachment format, each is a tuple of (name, mimetype,contents)
# content should be *binary* and not base64 encoded, since we need to