简体   繁体   中英

python3 use google-app-engine receive email, how to configure to specific app, not default app

I am able to receive eamil at the default service of google-app-engine, the address is anystring@my-appid.appspotmail.com, but I need to send to a specific service, not the default service, how can I do that, below is my code, thanks in advance.

app.yaml

runtime: python39 
app_engine_apis: true 
service: target-service
inbound_services: 
- mail 
- mail_bounce

handle code:main.py

from flask import Flask, request
from google.appengine.api import wrap_wsgi_app
from google.appengine.api import mail
app = Flask(__name__)
app.wsgi_app = wrap_wsgi_app(app.wsgi_app, use_deferred=True)
@app.route('/_ah/mail/<path>', methods=['POST'])
def receive_mail(path):
    mail_message = mail.InboundEmailMessage(request.get_data())
    print("called")
    return 'Success'

To be able to use a specific service in App Engine, you need to specify it in the app.yaml :

service: service-name

Additionally if you are planning to use multiple services in your app, here's a documentation you can use to structure your app.

Just tested this in Python2.7 but I believe it should work in Python3 (the principle should be the same when it comes to the mail part but might require a few tweaks for the other places)

  1. I have a default service with an app.yaml file and another service called second-service with second-service.yaml .

  2. Added the following to both app.yaml & second-service.yaml files

    inbound_services:
        - mail
  1. Added this to second-service.yaml Note that my python file here is called second.py . It handles traffic routed to second-service
- url: /_ah/mail/.+
  script: second.app
  1. Added this to app.yaml Note that my python file here is called ``main.py```.
- url: /_ah/mail/.+
  script: main.app
  1. Added the following to both main.py and second.py
@app.route('/_ah/mail/<path>', methods=['POST'])
def receive_mail(path):
    mail_message = mail.InboundEmailMessage(request.get_data())
    logging.info(mail_message.subject)
    return 'Success'
  1. For testing, I sent emails to the addresses below and they were only received by their respective services.

Note: I used -dot- and not . for the multi-service bit.

a) support@second-service-dot-<project_id>.appspotmail.com

b) support@<project_id>.appspotmail.com

Final bit: I only have service: second-service within second-service.yaml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM