Factor out the sending of a templated mail to a function, we're sure

to want to use this in the future.
This commit is contained in:
Magnus Hagander
2010-01-12 19:50:50 +01:00
parent cad9eddd92
commit 6c498a47a3
2 changed files with 21 additions and 10 deletions

View File

@ -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,

View File

@ -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)