我一直在尝试将xls缓冲区或pdf缓冲区附加到Django邮件队列,但我做不到
我尝试过使用FileResponse或HttpResponse并将其转换为Django文件对象,但也失败了。你知道吗
这就是我所尝试的:
new_message = MailerMessage()
new_message.subject = "Test"
new_message.to_address = "test@gmail.com"
new_message.from_address = "noreply@gmail.com"
file_attachment = FileResponse('file_content_buffer', content_type='application/vnd.ms-excel')
new_message.add_attachment(file_attachment)
new_message.save()
我有个错误:
'FileResponse' object has no attribute 'file'
。
我知道附件方法等待一个文件对象,如文档所述:
https://django-mail-queue.readthedocs.io/en/latest/usage.html#attaching-files。你知道吗
你知道吗?谢谢。你知道吗
解决方案:
关注mailqueue代码的功能有点不正常。你怎么认为?你知道吗
from mailqueue.models import MailerMessage, Attachment
new_message = MailerMessage()
new_message.subject = "Subject Mail"
new_message.to_address = 'test@gmail.com'
new_message.from_address = 'noreply@gmail.com'
new_message.save()
attachment = Attachment()
attachment.file_attachment.save('file.xlsx',ContentFile(excel_buffer),save=False)
attachment.email = new_message
attachment.original_filename = 'file.xlsx'
try:
new_attachment.save()
except Exception as e:
new_attachment.file_attachment.delete()
试试这个Django 2.1版:
file_attachment = FileResponse(open('myfile.xls', 'rb'),as_attachment=True,'myfile.xls')
Source是关于如何使用FileResponse的官方django文档。你知道吗
使用
HTTPResponse
为Django 1.9添加答案:Source
相关问题 更多 >
编程相关推荐