Из TemplateResponse получить файл на отправку на почту
def render_payroll(self, form, data):
response = TemplateResponse(
self.request,
'payroll/payroll.html',
context={'data': data},
)
# Нажата кнопка скачать
if 'payroll_action_download' in form.data:
response['Content-Type'] = "application/vnd.ms-excel; charset=utf-8"
response['Content-Disposition'] = 'inline; filename="payroll.xls"'
return response
# Отображаем
elif 'payroll_action_show' in form.data:
return response
# Отправляем по почте письмо с вложением
else:
text_message = render_to_string('payroll/send.html',{'data': data})
subject_message = f'Расчетный листок за {data["month_rus"]} {data["t_year"]}'
email = EmailMessage(subject_message, text_message,"[email protected]",["[email protected]"])
email.content_subtype = 'html'
**email.attach(filename='payroll.xlsx', content=bytes(str(response.rendered_content), encoding = 'utf-8'), mimetype='application/vnd.ms-excel')**
email.send(fail_silently=False)
messages.success(self.request, "Расчётный лист отправлен на почтовый адрес")
return redirect(reverse('payslip'))
Подскажите, как отправить файл на почту? Пока приходит только xlsx файл, но внутри html
Ответы (1 шт):
Автор решения: Ivan Dorofeev
→ Ссылка
Нашёл решение сам) До отправки нужно вызвать:
response.render()
