tickets: Send a confirmation email for bookings.
authorMatthijs Kooijman <matthijs@stdin.nl>
Mon, 18 Oct 2010 11:36:50 +0000 (13:36 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Mon, 18 Oct 2010 11:36:50 +0000 (13:36 +0200)
tickets/templates/tickets/booked.eml [new file with mode: 0644]
tickets/views.py

diff --git a/tickets/templates/tickets/booked.eml b/tickets/templates/tickets/booked.eml
new file mode 100644 (file)
index 0000000..baa084f
--- /dev/null
@@ -0,0 +1,13 @@
+{% autoescape off %}
+From: Dorestad 1493 <dorestad@evolution-events.nl>
+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 %}
index 63cf28610311b7afff27af37b7cd6e259baa53a6..3e9547f689f8bb9d524895e61d7cac5037df0338 100644 (file)
@@ -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})