Sending PDF files by email

When a payment is successful, we will send an automatic email to our customers including the generated PDF invoice. Edit the views.py file of the payment application and add the following imports to it:

from django.template.loader import render_to_stringfrom django.core.mail import EmailMessagefrom django.conf import settingsimport weasyprintfrom io import BytesIO

Then, in the payment_process view, add the following code after the order.save() line with the same indentation level as follows:

def payment_process(request):    # ...    if request.method == 'POST':        # ...        if result.is_success:            # ...            order.save()            # create invoice e-mail            subject = 'My Shop - Invoice no. {}'.format(order.id) message = 'Please, find attached the ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.