gpt4 book ai didi

google-app-engine - 在使用 GAE InboundMailHandler 处理附件方面需要帮助

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:46 25 4
gpt4 key购买 nike

我已经正确实现了 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 来显示导入的 pdf 文件时: '/serve/%s' % blob_info.key()我得到一个包含编码数据的页面,而不是实际的 pdf 文件。

看起来像这样:


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

有什么想法吗?谢谢

最佳答案

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

尝试:

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

关于google-app-engine - 在使用 GAE InboundMailHandler 处理附件方面需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6949361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com