Djangourald:无法发送带有附件的邮件

2024-05-13 03:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

有人使用Django herald发送通知吗

我已经努力了好几天让它工作起来,但是缺少文档和无声的失败使得调试问题变得不可能。如果我在邮件中添加附件,邮件似乎就不会被发送

from herald.base import EmailNotification


def sendMail():
    SendThisMail(user, my_modal).send(user=my_user) # creates an error on this line as the file object is closed and inaccessible.

@registry.register_decorator()
class SendThisMail(SomeBaseClass, EmailNotification):
    def __init__(self, user, my_modal: models.MyModal):
        super().__init__(user, my_modal)

        self.subject = "abc"

        file = open('.staticfiles/assets/some.pdf', 'rb')

        self.attachments = [('attachment_1', File(file))]

        self.context = {
            **self.context,
            'subject': self.subject,
            'attachment': self.attachments,
        }

        self.to_emails = [user.email]

怎么了


Tags: selfattachmentinitmydefcontext邮件attachments
1条回答
网友
1楼 · 发布于 2024-05-13 03:34:58

从项目文档中:

Each attachment in the list can be one of the following:

  1. A tuple which consists of the filename, the raw attachment data, and the mimetype. It is up to you to get the attachment data

因此,代码的相关部分应该是:

  data = open('.staticfiles/assets/some.pdf', 'rb').read()
  self.attachments = [('attachment_1', data, 'application/pdf')]

相关问题 更多 >