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
文档中有“ Attachment”的示例。
请帮助!
答案 0 :(得分:4)
据我在文档中看到的,有类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
的新实例(将元组用作explained in the 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已经有好几年了,但是如果发现任何错误(或发布您自己的答案),请随时探索这个想法并编辑我的答案。
答案 1 :(得分:1)
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)
* 这是一个简化版本;在实际的类中,分配是通过单独的方法完成的,但本质上是相同的。
答案 2 :(得分:0)
我已修复send_mail函数的问题,其中一个错误是未将文件名指定为“ .pdf”,而是仅将其命名为“ certificate”
此外,如果您在app.yaml中指定了任何处理程序以进行直接的url访问,请确保将“ application_visible”设置为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)])