def send_invoice(invoice): """ Sends Invoice to the invoice partner """ template = EmailTemplate.objects.get(name='invoice-email') subject = replace_invoice_vars(template.subject, invoice, 'en') message_plain = replace_invoice_vars(template.plain_text, invoice, 'en') message_html = replace_invoice_vars(template.html_text, invoice, 'en') partner_email = invoice.partner.partner_email or invoice.partner.finance_email # make email msg = EmailMultiAlternatives( subject=subject, body=message_plain, from_email='*****@*****.**', to=[partner_email, ], bcc=['*****@*****.**'], ) msg.attach_alternative(message_html, 'text/html') # make Invoice PDF cmd_options = { 'orientation': 'landscape', 'page-size': 'A4', 'title': invoice.invoice_nr } template = 'adventures/invoice/invoice_pdf.html' context = { 'invoice': invoice, 'partner': invoice.partner, } pdf_data = PDFTemplateResponse(request=None, template=template, context=context, filename=invoice.invoice_nr, show_content_in_browser=True, cmd_options=cmd_options) pdf_data.render() pdf_data.close() filename = "{} invoice Adventure Tickets {}.pdf".format(invoice.created.strftime("%Y-%m-%d"), invoice.invoice_nr) msg.attach(filename, pdf_data.rendered_content, 'application/pdf') msg.send() logger.info("Invoice sent to {}: {}".format(partner_email, str(invoice)))