From 6c498a47a3a7737fe896c65bcaeb175341a2e35b Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Tue, 12 Jan 2010 19:50:50 +0100 Subject: [PATCH] Factor out the sending of a templated mail to a function, we're sure to want to use this in the future. --- pgweb/misc/views.py | 19 +++++++++---------- pgweb/util/misc.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pgweb/misc/views.py b/pgweb/misc/views.py index 3f5c3ba5..b21007b6 100644 --- a/pgweb/misc/views.py +++ b/pgweb/misc/views.py @@ -1,12 +1,11 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.http import HttpResponseRedirect, HttpResponse, Http404 from django.db import connection -from email.mime.text import MIMEText from django.conf import settings from pgweb.util.contexts import NavContext from pgweb.util.helpers import template_to_string -from pgweb.util.misc import sendmail +from pgweb.util.misc import send_template_mail from pgweb.core.models import Version @@ -20,16 +19,16 @@ def submitbug(request): c.execute("SELECT nextval('bug_id_seq')") bugid = c.fetchall()[0][0] - msg = MIMEText( - template_to_string('misc/bugmail.txt', { + send_template_mail( + form.cleaned_data['email'], + settings.BUGREPORT_EMAIL, + 'BUG #%s: %s' % (bugid, form.cleaned_data['shortdesc']), + 'misc/bugmail.txt', + { 'bugid': bugid, 'bug': form.cleaned_data, - }), - _charset='utf-8') - msg['Subject'] = 'BUG #%s: %s' % (bugid, form.cleaned_data['shortdesc']) - msg['To'] = settings.BUGREPORT_EMAIL - msg['From'] = form.cleaned_data['email'] - sendmail(msg) + } + ) return render_to_response('misc/bug_completed.html', { 'bugid': bugid, diff --git a/pgweb/util/misc.py b/pgweb/util/misc.py index e66f2948..ac839ccb 100644 --- a/pgweb/util/misc.py +++ b/pgweb/util/misc.py @@ -1,4 +1,7 @@ from subprocess import Popen, PIPE +from email.mime.text import MIMEText + +from pgweb.util.helpers import template_to_string def prettySize(size): if size < 1024: @@ -15,3 +18,12 @@ def sendmail(msg): pipe.write(msg.as_string()) pipe.close() +def send_template_mail(sender, receiver, subject, templatename, templateattr={}): + msg = MIMEText( + template_to_string(templatename, templateattr), + _charset='utf-8') + msg['Subject'] = subject + msg['To'] = receiver + msg['From'] = sender + sendmail(msg) +