【发布时间】:2013-06-22 01:59:54
【问题描述】:
我正在开发一个应用程序来处理打到我邮箱的电子邮件。我已修改我的邮件设置,将收到的邮件转发到 myapp。到达 myapp 的邮件将被路由到处理程序脚本(“handle_incoming_email.py”),在那里进行处理。我的 app.yaml 文件如下所示
app.yaml
application: myapp
version: 1-1
runtime: python27
api_version: 1
threadsafe: false
default_expiration: "360d"
handlers:
- url: /_ah/mail/.+
script: myapp/utils/handle_incoming_email.py
邮件处理脚本如下
handle_incoming_email.py
import logging
import urllib
import base64
import traceback
from google.appengine.ext import webapp
from google.appengine.ext import blobstore
from google.appengine.api import urlfetch
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
class ReceiveEmail(InboundMailHandler):
def receive(self, message):
try:
for filename, filecontents in message.attachments:
if filecontents.encoding:
# data = filecontents
data = filecontents.decode()
# data = filecontents.payload
# data = filecontents.payload.decode()
# data = base64.b64decode(filecontents.decode())
# data = base64.b64decode(filecontents.payload)
# data = base64.b64decode(filecontents.payload.decode())
upload_url = blobstore.create_upload_url('http://myapp.appspot.com/settings/saveItem/')
form_fields = {'field_name': data}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url=upload_url,
payload=form_data,
method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'})
logging.info(result)
except Exception, e:
traceback.print_exc()
application = webapp.WSGIApplication([ReceiveEmail.mapping()], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
我的要求是为每个带有附件的邮件创建一个实体。为此,我需要解析邮件中的附件并将其上传到 blobstore。但是,当我尝试将附件上传到 blobstore 时,出现以下错误:
此 URL 不接受请求的内容类型。
正如您在“handle_incoming_email.py”中的注释代码中看到的那样,我尝试了不同的方法(试错法)来获取正确的数据,但无济于事。
谁能指导我解决这个问题!
谢谢!!!
【问题讨论】:
-
上传?为什么不将其写入 blobstore。
-
@voscausa :根据documentation,不推荐将文件写入 blobstore。此外,我可能需要将文件附件作为 multipart/form-data 从我的代码发布到第 3 方 API。
标签: google-app-engine python-2.7 blobstore urlfetch