需要有关使用 GAE InboundMailHandler 处理附件的帮助

发布于 2024-11-28 09:19:44 字数 1435 浏览 0 评论 0原文

我已经正确实现了 InboundMailHandler,并且能够处理除 mail_message.attachments 之外的所有其他 mail_message 字段。附件文件名已正确读取,但内容未保存在正确的 mime_type 中

        if not hasattr(mail_message, 'attachments'):
            raise ProcessingFailedError('Email had no attached documents')

        else:
            logging.info("Email has %i attachment(s) " % len(mail_message.attachments))

        for attach in mail_message.attachments:
            filename = attach[0]
            contents = attach[1]


        # Create the file
        file_name = files.blobstore.create(mime_type = "application/pdf")

        # Open the file and write to it
        with files.open(file_name, 'a') as f:
            f.write(contents)

        # Finalize the file. Do this before attempting to read it.
        files.finalize(file_name)

        # Get the file's blob key
        blob_key = files.blobstore.get_blob_key(file_name)
        return blob_key

        blob_info = blobstore.BlobInfo.get(blob_key)

`

当我尝试通过转到 url: '/serve/%s' % blob_info.key() 显示导入的 pdf 文件时 我得到的页面看起来像是编码数据,而不是实际的 pdf 文件。

看起来像这样:

<代码> 来自无人 Thu Aug 4 23:45:06 2011 内容传输编码:base64 JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G bGF0ZURLY29kZSA+PgpzdHJlYW0KeAGtXVuXHLdxfu9fgSef2RxxOX2by6NMbSLalOyQK+ucyHpQ eDE3IkWKF0vJj81vyVf3Qu9Mdy+Z40TswqKAalThqwJQjfm1/Hv5tWzxv13blf2xK++el+/LL+X+ g/dtefq 有什么

想法吗?谢谢

I have properly implemented InboundMailHandler and I'm able to process all other mail_message fields except mail_message.attachments. The attachment filename is read properly but the contents are not being saved in the proper mime_type

        if not hasattr(mail_message, 'attachments'):
            raise ProcessingFailedError('Email had no attached documents')

        else:
            logging.info("Email has %i attachment(s) " % len(mail_message.attachments))

        for attach in mail_message.attachments:
            filename = attach[0]
            contents = attach[1]


        # Create the file
        file_name = files.blobstore.create(mime_type = "application/pdf")

        # Open the file and write to it
        with files.open(file_name, 'a') as f:
            f.write(contents)

        # Finalize the file. Do this before attempting to read it.
        files.finalize(file_name)

        # Get the file's blob key
        blob_key = files.blobstore.get_blob_key(file_name)
        return blob_key

        blob_info = blobstore.BlobInfo.get(blob_key)

`

When I try to display the imported pdf file by going to the url: '/serve/%s' % blob_info.key()
I get a page with what seems like encoded data, instead of the actual pdf file.

Looks like this:


From nobody Thu Aug 4 23:45:06 2011 content-transfer-encoding: base64 JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvYmoKPDwgL0xlbmd0aCA1IDAgUiAvRmlsdGVyIC9G bGF0ZURlY29kZSA+PgpzdHJlYW0KeAGtXVuXHLdxfu9fgSef2RxxOX2by6NMbSLalOyQK+ucyHpQ eDE3IkWKF0vJj81vyVf3Qu9Mdy+Z40TswqKAalThqwJQjfm1/Hv5tWzxv13blf2xK++el+/LL+X+ g/dtefq

Any ideas? Thanks

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

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

发布评论

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

评论(3

爱,才寂寞 2024-12-05 09:19:44

电子邮件的附件是 EncodedPayload 对象;要获取数据,您应该调用 decode() 方法。

尝试使用:

# Open the file and write to it
with files.open(file_name, 'a') as f:
    f.write(contents.decode())

Email's attachments are EncodedPayload objects; to get the data you should call the decode() method.

Try with:

# Open the file and write to it
with files.open(file_name, 'a') as f:
    f.write(contents.decode())
陌上芳菲 2024-12-05 09:19:44

如果您希望成功处理大于 1MB 的附件,请解码并转换为 str:

#decode and convert to string
datastr = str(contents.decode())
with files.open(file_name, 'a') as f:
  f.write(datastr[0:65536])
  datastr=datastr[65536:]
  while len(datastr) > 0:
    f.write(datastr[0:65536])
    datastr=datastr[65536:]

If you want attachments larger 1MB to be processed successfully, decode and convert to str:

#decode and convert to string
datastr = str(contents.decode())
with files.open(file_name, 'a') as f:
  f.write(datastr[0:65536])
  datastr=datastr[65536:]
  while len(datastr) > 0:
    f.write(datastr[0:65536])
    datastr=datastr[65536:]
梦回旧景 2024-12-05 09:19:44

在这篇优秀的博客文章中找到了答案:
http: //john-smith.appspot.com/app-engine--what-the-docs-dont-tell-you-about-processing-inbound-mail

这是如何解码 GAE 入站邮件的电子邮件附件:

        for attach in mail_message.attachments:
            filename, encoded_data = attach
            data = encoded_data.payload
            if encoded_data.encoding:
                data = data.decode(encoded_data.encoding)

Found the answer in this excellent blob post:
http://john-smith.appspot.com/app-engine--what-the-docs-dont-tell-you-about-processing-inbound-mail

This is how to decode an email attachment for GAE inbound mail:

        for attach in mail_message.attachments:
            filename, encoded_data = attach
            data = encoded_data.payload
            if encoded_data.encoding:
                data = data.decode(encoded_data.encoding)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文