首页 > 解决方案 > 使用 Python3 的 AppEngine StdEnv:如何接收传入的电子邮件

问题描述

当前文档: https ://cloud.google.com/appengine/docs/standard/python/mail/receiving-mail-with-mail-api

看起来它是针对 python27 的,否则在 app.yaml 中的条目应该是:

- url: /_ah/mail/.+
  script: auto (instead of handle_incoming_email.app)
  login: admin

我找不到任何关于如何使用 Python3 在 GAE StdEnv 上接收传入电子邮件的文档。我试过:

应用程序.yaml

runtime: python37
entrypoint: gunicorn -b :$PORT incoming_email.app
handlers:
- url: /_ah/mail/.+
  script: auto
  login: admin
inbound_services:
- mail

要求.txt

ez_setup
gunicorn
google-appengine

传入电子邮件.py

from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
import webapp2

class LogSenderHandler(InboundMailHandler):
  """."""
  def receive(self, mail_message):
    """Do things with mail_message"""

app = webapp2.WSGIApplication([
  ('/_ah/mail/', LogSenderHandler)
], debug=True)

但是在部署时,google-appengine 无法构建:

Error ID: 4646FF8A. Error type: InternalError. Error message: `pip_download_wheels` had stderr output: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-wheel-80z20v2n/google-appengine/

error: `pip_download_wheels` returned code: 1.

导入 InboundMailHandler 需要 google-appengine,但看起来 google-appengine pip install 适用于 Python 2?任何人都可以使用 Python3 接收电子邮件?

谢谢!

标签: python-3.xemailgoogle-app-enginegoogle-cloud-platform

解决方案


App Engine 标准环境中的 Python 3 运行时与 Python 2 运行时有很大不同。

正如官方文档中关于 App Engine 标准环境中 Python 2 和 Python 3 之间的差异的说明

邮件服务在 Python 3 中不可用。您需要使用第三方邮件提供商,例如 SendGrid、Mailgun 或 Mailjet 来发送电子邮件。所有这些服务都提供 API 来从应用程序发送电子邮件。

在 App Engine 标准 Python 3 运行时环境中,您可以找到官方文档中介绍的 2 个选项:

这两个邮件 API 都可以接收和解析入站电子邮件。


推荐阅读