Search code examples
python-3.xemailgoogle-app-enginegoogle-cloud-platform

AppEngine StdEnv with Python3: How to receive incoming email


The current documentation: https://cloud.google.com/appengine/docs/standard/python/mail/receiving-mail-with-mail-api

looks like it is for python27, otherwise in app.yaml entry should be:

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

I can't find any documentation on how to receive incoming email on GAE StdEnv with Python3. I tried with:

app.yaml

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

requirements.txt

ez_setup
gunicorn
google-appengine

incoming_email.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)

But when deploying, google-appengine can't build:

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.

google-appengine is needed to import the InboundMailHandler, but looks like google-appengine pip install is for Python 2? Anyone able to receive emails with Python3?

Thanks!


Solution

  • The Python 3 runtime on the App Engine standard environment is significantly different from the Python 2 runtime.

    As stated in the official documentation for the differences between Python 2 and Python 3 on the App Engine standard environment:

    The Mail service is not available in Python 3. You need to use a third-party mail provider such as SendGrid, Mailgun, or Mailjet to send email. All of these services offer APIs to send email from applications.

    In the App Engine Standard Python 3 runtime environment you can find 2 options covered in the official documentation:

    Both of these mail APIs can receive and parse inbound emails.