Send HttpResponse as Attachment in emails in django
I have a class in which I am creating a downloadable excel file like the following:
response = HttpResponse(content_type='application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=Scanned Data_{}.xlsx '.format(time.strftime("%d-%m-%Y %H:%M"))
writer = ExcelWriter(response)
.
.
.
writer.save()
Now I want to send this response as an attachment to the mail for which I am using Mimemultipart() like the following:
recipient_list = ['xxx@yyy.com',]
message = MIMEMultipart()
message['Subject'] = 'Results'
message['From'] = 'xxx@yyyy.com'
body_content = "foo"
message.attach(MIMEApplication(response))
message.attach(MIMEText(body_content, "html"))
msg_body = message.as_string()
server = SMTP('email-smtp.ap-south-1.amazonaws.com', 587)
server.starttls()
server.login('xxxx', 'xxx+x')
server.sendmail(message['From'], recipient_list, msg_body)
for which I am getting:
TypeError: expected bytes-like object, not HttpResponse
How do I attach this to file ? Should I convert it to the file and save it locally and then read it and send email and then delete it ? (This doesn't seem the right way to do this)