gpt4 book ai didi

python - 'InMemoryUploadedFile' 对象没有属性 'encode'

转载 作者:行者123 更新时间:2023-11-28 19:12:16 24 4
gpt4 key购买 nike

我正在尝试在 Django 中发送带有附件的电子邮件。文件是 request.FILE['file']对象(InMemoryUploadedFile 类型)。我通过 EmailMessage(...) 创建消息,然后通过 message.attach(f.name, f.read(), f.content_type) 附加文件。

发送电子邮件失败并出现给定错误:“InMemoryUploadedFile”对象没有属性“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()

在哪里documents[]是输入的html标签的名称:

<input name="document[]" id="file" type="file">

关于python - 'InMemoryUploadedFile' 对象没有属性 'encode',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38510951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com