繁体   English   中英

Google App Engine Python中的EmailMessage类不起作用?

[英]EmailMessage class in google app engine python not working?

Google App Engine文档是否未更新?

当我这样做时,它工作正常(发送带有附件的电子邮件):

message = mail.EmailMessage( sender=EMAIL_SENDER, 
subject=subject,body=theBody,to=['test@gmail.com'],attachments=[(attachname, 
new_blob.archivoBlob)])
message.send()

但是当我使用message.attach时,它说EmailMessage对象没有属性attach

message.attach("certificate.pdf", new_file, "application/pdf")
or
message.Attachment("certificate.pdf", new_file, "application/pdf")

两者都说: EmailMessage对象没有属性attach/attachment

在文档中有“ 附件 ”的示例。

请帮忙!

据我在文档中看到的,有google.appengine.api.mail.Attachment类,但是google.appengine.api.mail.EmailMessage类没有任何方法attach()

google.appengine.api.mail.EmailMessage类确实具有attachment属性,这就是为什么当您使用attachments=[(foo,bar),(foo,bar)]初始化电子邮件时它可以工作的原因。 您实际上是在创建google.appengine.api.mail.Attachment新实例(使用docs中说明的元组),将其添加到数组中,并在初始化电子邮件时将其用作attachments属性。

请注意,在文档中,当他们写attachment = mail.Attachment('foo.jpg', 'data') ,该mail是对导入的google.appengine.api.mail的引用,而不是实例化的邮件对象。

回到您的示例(请注意,我不是python开发人员,并且我还没有尝试过,我只是在浏览文档并进行假设),而不是

message.attach("certificate.pdf", new_file, "application/pdf")

你可能应该在

attachment1 = mail.Attachment("certificate.pdf", new_file, "application/pdf")
attachment2 = mail.Attachment("another_certificate.pdf", new_file, "application/pdf")
message.attachments = [attachment1, attachment2]

自从使用python以来已经有好几年了,但是如果发现任何错误(或发布您自己的答案),随时可以探索这个想法并编辑我的答案。

EmailMessage类的属性是动态分配的,例如*

class EmailMessage(_EmailMessageBase):

    def __init__(self, **kwargs):
        for name, value in kwargs.items():
            setattr(self, name, value)

因此,如果attachments没有作为关键字参数传递给构造函数,则该实例没有attachments属性,如果尝试引用它,则会出现AttributeError

正如Jofre在回答中所观察到的那样,您仍然可以直接将其分配给attachments

message.attachments = [attachment1]

在创建附件属性之后,您还可以附加到它:

message.attachments.append(attachment2)

*这是简化版本; 在实际的类中,分配是通过单独的方法完成的,但本质上是相同的。

我修复了send_mail函数的问题,其中一个错误是未将文件名指定为“ .pdf”,而是将其命名为“ certificate”

另外,如果您在app.yaml中指定了任何处理程序以进行直接URL访问,请确保将“ application_ready”设置为true,以便可以在应用程序内访问该文件:

- url: /certificate.((pdf)|(PDF))
  static_files: pages/pdf/certificate.PDF
  upload: pages/pdf/certificate.PDF
  application_readable: true

 additional_pdf = open(os.path.dirname(__file__) + '/../pages/pdf/certificate.pdf').read()

 mail.send_mail(sender=EMAIL_SENDER,
                  to=['mmyemail@gmail.com'],
                  subject=subject,
                  body=theBody,
                  attachments=[(attachname, blob.archivoBlob),("certificate.pdf",additional_pdf)])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM