我正在尝试在 Django 中发送一封带有附件的电子邮件。文件是request.FILE['file']
对象(InMemoryUploadedFile 类型)。我通过创建消息EmailMessage(...)
然后附加文件message.attach(f.name, f.read(), f.content_type)
.
发送电子邮件失败并出现以下错误:'InMemoryUploadedFile' object has no attribute 'encode'
这在表单定义中对我有用forms.py
from django.core.mail import EmailMultiAlternatives
email = EmailMultiAlternatives(
subject='some subject',
from_email='from_address@some_domain.com',
to=['recipient1@another_domain.com'],
body='some html content')
email.content_subtype = "html"
if hasattr(self.files, 'getlist'):
files = self.files.getlist('document[]')
for _file in files:
_file.open()
email.attach(_file.name, _file.read(), _file.content_type)
_file.close()
email.send()
Where documents[]
是输入 html 标签的名称:
<input name="document[]" id="file" type="file">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)