Djangp-herald:无法发送带有附件的邮件

时间:2019-06-15 16:57:52

标签: python django django-herald

有人使用Django先驱发送通知吗?

为了使它正常工作,我已经努力了好几天,但是缺少文档和无声的故障使得无法调试问题。如果我在其中包含附件,则似乎没有发送邮件。

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]

怎么了?

1 个答案:

答案 0 :(得分:1)

从项目文档中:

  

列表中的每个附件可以是以下之一:

     
      
  1. 由文件名,原始附件数据和mimetype组成的元组。由您来获取附件数据
  2.   

因此,代码的相关部分应类似于:

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