如何将保存在 Django FileField 中的文件作为附件发送?

发布于 2024-12-29 03:23:43 字数 746 浏览 1 评论 0原文

如何发送包含保存在 FileField 中的文件的邮件?我不知道如何访问下面代码中的文件。

mail = EmailMessage(subject, message, 'from@from,com', ['[email protected]'])
mail.attach(?, ?, 'application/pdf')
mail.send()

编辑

文件

f = list_pca.pdf_file.open(mode='rb')

我尝试打开其中 list_pca 是实例的

class ListPCA(models.Model):
    pdf_file = models.FileField(upload_to=get_file_path_j, null=True, blank=True)

,但收到错误“没有这样的文件或目录”,因为路径错误。

list_pca.pdf_file.path

返回错误的路径。由于 upload_to 选项,它不是应该知道文件在哪里吗?

谢谢

谢谢

how can I send a mail with a file saved in a FileField? I don't know how to access the file in the code below.

mail = EmailMessage(subject, message, 'from@from,com', ['[email protected]'])
mail.attach(?, ?, 'application/pdf')
mail.send()

EDIT

I tried to open the file with

f = list_pca.pdf_file.open(mode='rb')

where list_pca is an instance of

class ListPCA(models.Model):
    pdf_file = models.FileField(upload_to=get_file_path_j, null=True, blank=True)

but I get an error "No such file or directory", because the path is wrong.

and

list_pca.pdf_file.path

return the wrong path too. Isn't it supposed to know where is the file thanks to the upload_to option?

Thanks

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猫腻 2025-01-05 03:23:43
mail = EmailMessage(subject, message, 'from@from,com', ['[email protected]'])
mail.attach('arbitrary_filename', myfile.read(), 'application/pdf')
mail.send()

由于您使用的是 EmailMessage,因此您也可以将附件 kwarg 传递给它。

email = EmailMessage(subject, message, 'from@from,com', ['[email protected]'], 
    attachments=(('foo.pdf', myfile.read(), 'application/pdf'),))
mail = EmailMessage(subject, message, 'from@from,com', ['[email protected]'])
mail.attach('arbitrary_filename', myfile.read(), 'application/pdf')
mail.send()

Since you're using EmailMessage, you can just pass it the attachments kwarg as well.

email = EmailMessage(subject, message, 'from@from,com', ['[email protected]'], 
    attachments=(('foo.pdf', myfile.read(), 'application/pdf'),))
浅浅淡淡 2025-01-05 03:23:43

通常,就像 aganders3 建议的那样,我会使用:

mail.attach_file(myfile.path) as well

但是,我注意到它对于包含空格的附件会失败。

Ordinarily, like aganders3 suggested, I would use:

mail.attach_file(myfile.path) as well

But, I noted that it fails for attachments containing spaces.

白云不回头 2025-01-05 03:23:43

我认为这可以做到:

mail.attach(os.path.basename(myfile.name), myfile.read(), 'application/pdf')

我可能会忽略 MIME 编码步骤,但 IIRC mail.attach 会做到这一点。

I think this will do it:

mail.attach(os.path.basename(myfile.name), myfile.read(), 'application/pdf')

I might be overlooking a MIME-encoding step, but IIRC mail.attach will do that.

跨年 2025-01-05 03:23:43

尝试:

myfile = mymodel.somefilefield

mail = EmailMessage(subject, message, 'from@from,com', ['[email protected]'])
mail.attach('File Name', myfile.path, 'application/pdf')
mail.send()

Try:

myfile = mymodel.somefilefield

mail = EmailMessage(subject, message, 'from@from,com', ['[email protected]'])
mail.attach('File Name', myfile.path, 'application/pdf')
mail.send()
离鸿 2025-01-05 03:23:43

其他建议应该有效,但您也可以这样做:

mail.attach_file(myfile.path)

您可以选择传递 MIME 类型作为第二个参数,但如果您不提供它,它只是尝试从文件名中找出它。

The other suggestions should work, but you can also do:

mail.attach_file(myfile.path)

You can optionally pass the MIME-type as the second argument, but it just tries to figure it out from the file name if you don't provide it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文