pythongoogle-app-engineincoming-mail

new App Engine service for incoming emails


I want to get incoming emails via App Engine. I am getting all content and putting it all into Storage. I found from here that I could use the code below to get emails

main.py:

from flask import Flask, request
from google.appengine.api import mail
from google.appengine.api import wrap_wsgi_app
import requests

app = Flask(__name__)

app.wsgi_app = wrap_wsgi_app(app.wsgi_app)

@app.route("/_ah/mail/<path>", methods=["POST"])
def receive_mail(path):
    try:
        message = mail.InboundEmailMessage(request.get_data())
        for content_type, payload in message.bodies():
            if content_type == 'text/html':
                html_content = payload.decode()
                print('HTML content retrieved successfully.')
..................................

app.yaml:

service: my_email_service
runtime: python39
app_engine_apis: true

inbound_services:
- mail

handlers:
- url: /_ah/mail/get_emails
  script: main.app

But it works only if the service only as default (without service: my_email_service line). When I try to make the other service, not default, just add to app.yaml the service name (service: my_email_service) - it doesn't work. I send the message and I am getting nothing, no logs, no message. Is any way to resolve it? I already have the default service in my prod but I need to use another service to get incoming messages.

I tried to use dispatch.yaml link but without result. All works only when the service is default. Any thoughts, please? my dispatch.yaml:

dispatch:
  - url: "*/get_emails/*"
    service: my_email_service

also tried in dispatch use url like: get_emails@PROJECT-ID.appspotmail.com

Maybe these is totally another code/solution


Solution

  • That's probably because you're sending emails to email addresses which aren't prefixed by your service name.

    If you look at the documentation you linked to, it says

    For non-default services, the email address has the following format:

    [STRING]@[servicename]-dot-[Google Cloud project ID].appspotmail.com