From a5560561a1094c95b984f5fee3f7d19808ea981f Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 18 Oct 2010 13:36:50 +0200 Subject: [PATCH] tickets: Send a confirmation email for bookings. --- tickets/templates/tickets/booked.eml | 13 +++++++++ tickets/views.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tickets/templates/tickets/booked.eml diff --git a/tickets/templates/tickets/booked.eml b/tickets/templates/tickets/booked.eml new file mode 100644 index 0000000..baa084f --- /dev/null +++ b/tickets/templates/tickets/booked.eml @@ -0,0 +1,13 @@ +{% autoescape off %} +From: Dorestad 1493 +Subject: Bevestiging reservering Dorestad 1493 + +Beste {{ booking.name }}, + +je hebt {{ booking.tickets }} kaarten gereserveerd voor de Dorestad 1493 +voorstelling op: {{ booking.get_show_display }}. + +Veel plezier bij de voorstelling! + +Evolution Events +{% endautoescape %} diff --git a/tickets/views.py b/tickets/views.py index 63cf286..3e9547f 100644 --- a/tickets/views.py +++ b/tickets/views.py @@ -9,6 +9,45 @@ class BookingForm(django.forms.ModelForm): class Meta: model=Booking +def confirm_booking(booking): + from django.core.mail import EmailMessage + + context = {'booking' : booking} + + rendered = django.template.loader.render_to_string('tickets/booked.eml', context) + (headers, body) = rendered.strip().split('\n\n', 1) + + # Turn the headers into a dict so EmailMessage can turn them into a + # string again. Bit pointless, but it works. + # Perhaps we should just use python email stuff directly. OTOH, we + # still always need to parse for the From header. + + headers_dict = {} + # If no From header is present, let EmailMessage do the default + # thing + from_email = None + for header in headers.split('\n'): + (field, value) = header.split(':') + if (field == 'From'): + from_email = value + elif (field == 'Subject'): + subject = value + else: + # Don't put From and Subject in the dict, else they'll be + # present twice. + headers_dict[field] = value + + msg = EmailMessage( + # Only setting the From address through headers won't set the + # envelope address right. + from_email = from_email, + subject = subject, + body = body, + to = [booking.email], + headers = headers_dict + ) + msg.send() + def book(request): if request.method == "POST": f = BookingForm(request.POST) @@ -17,6 +56,7 @@ def book(request): if f.is_valid(): booking = f.save() + confirm_booking(booking) return render_to_response('tickets/booked.html', {'booking' : booking}) return render_to_response('tickets/bookingform.html', {'form' : f}) -- 2.30.2