gpt4 book ai didi

django - 如何将 Django FileField 作为附件通过电子邮件发送?

转载 作者:行者123 更新时间:2023-12-03 14:34:26 25 4
gpt4 key购买 nike

我需要发送 models.FileField作为使用 Django 的电子邮件附件。我看过一些片段,展示了如何使用原始 request.FILES 来做到这一点。数据(仍然包含内容类型),但是一旦您已经将文件保存在 models.FileField 中,就无法找到任何显示如何操作的内容。 .内容类型似乎无法从 models.FileField 访问.

有人可以给我一个例子来说明这是如何工作的吗?我开始认为在保存文件时可能必须将 Content-Type 存储在模型中。

谢谢!

最佳答案

我只是不提供内容类型并让收件人的电子邮件客户端解决。除非它是不寻常的东西,否则它应该不是问题。

RFC2616指出:

If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource.



但是 ...
如果你想指定它,那么在上传时存储内容类型是一个很好的主意。应该注意的是,django 自己的文档对 verify the data from users

如果您使用的是 *unix 操作系统,您可以尝试猜测/检查它:
import subprocess
subprocess.check_output(['file', '-b', '--mime', filename])

(来自 How to find the mime type of a file in python?)

关于django - 如何将 Django FileField 作为附件通过电子邮件发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500652/

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