简体   繁体   English

由于缺少 api 代理,从 Google Appengine 发送电子邮件失败

[英]Sending emails from Google Appengine fails because of missing api proxy

I am trying to send e-mails from a GAE application using this code:我正在尝试使用以下代码从 GAE 应用程序发送电子邮件:

from google.appengine.api.mail import send_mail

send_mail(
    "sender@nowhere.com",
    ["user@example.com"],
            "Subject",
            "Body",
)

I have configured usage of the apis in app.yaml with:我在app.yaml中配置了 API 的使用:

app_engine_apis: true

And deploy to App Engine is done with gcloud beta app deploy .部署到 App Engine 是使用gcloud beta app deploy完成的。

However, I get this error:但是,我收到此错误:

Traceback (most recent call last):   
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app      response = self.full_dispatch_request()    
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request      rv = self.handle_user_exception(e)    
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request      rv = self.dispatch_request()    
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request      return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)    
File "/srv/infrastructure/view_modifiers.py", line 12, in view_method      response_val = f(*args, **kwargs)    
File "/srv/views/orders.py", line 25, in create_order      vm.create_order()    
File "/srv/viewmodels/orders/order_viewmodel.py", line 74, in create_order      self._send_order_email()    
File "/srv/viewmodels/orders/order_viewmodel.py", line 54, in _send_order_email      send_mail(    
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/mail.py", line 401, in send_mail      message.send(make_sync_call=make_sync_call)    
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/mail.py", line 1209, in send      make_sync_call('mail', self._API_CALL, message, response)    
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/apiproxy_stub_map.py", line 96, in MakeSyncCall      return stubmap.MakeSyncCall(service, call, request, response)    
File "/layers/google.python.pip/pip/lib/python3.9/site-packages/google/appengine/api/apiproxy_stub_map.py", line 348, in MakeSyncCall      assert stub, 'No api proxy found for service "%s"' % service  AssertionError: No api proxy found for service "mail"

This seems to suggest that even for the default behavior of the mail service, some kind of proxy needs to be configured.这似乎表明即使对于邮件服务的默认行为,也需要配置某种代理。 However, I cannot find any information about the setup of this proxy.但是,我找不到有关此代理设置的任何信息。 And, my initial understanding was that setting up a proxy is only needed for unit-testing or local development.而且,我最初的理解是设置代理只需要进行单元测试或本地开发。

I just tested this and it worked for me (ie I received the email I supplied for recipient_email_address in the code below).我刚刚对此进行了测试,它对我有用(即我收到了我在下面的代码中为recipient_email_address电子邮件地址提供的 email)。 Note that sender must be a value specified under Email Senders in App Engine settings page请注意,sender 必须是 App Engine 设置页面中Email Senders下指定的值

requirements.txt file requirements.txt文件

Flask
appengine-python-standard>=1.0.0

app.yaml file app.yaml文件

runtime: python39
app_engine_apis: true

handlers:

- url: /static
  static_dir: static/

- url: /.*
  script: auto

main.py

    from flask import Flask
    from google.appengine.api import wrap_wsgi_app
    from google.appengine.api.mail import send_mail

    app = Flask(__name__)
    app.wsgi_app = wrap_wsgi_app(app.wsgi_app)

    @app.route("/")
    def sendMail():
        
        
        send_mail(sender= <authorized_email_sender>,
                  to= <recipient_email_address>,
                  subject="Testing Python3 sending mails",
                    body="""Dear Albert:
    
                        Your example.com account has been approved.  You can now visit
                        http://www.example.com/ and sign in using your Google Account to
                        access new features.
    
                        Please let us know if you have any questions.
    
                        The example.com Team
                        """)

            return "Mail was sent"

Here is an example that works with fastapi.这是一个适用于 fastapi 的示例。

from google.appengine.api import mail
from fastapi.middleware.wsgi import WSGIMiddleware
from google.appengine.api import wrap_wsgi_app
from flask import Flask

app = create_app() // this is just app = FastAPI() and somes  middleware but but no relevance here
app_flask = Flask(__name__)
app_flask.wsgi_app = wrap_wsgi_app(app_flask.wsgi_app, use_deferred=True)


def send_approved_mail(sender_address):
    # [START send_message]
    message = mail.EmailMessage(
        sender=sender_address,
        subject="Your account has been approved")

    message.to = "test@gmail.com"
    message.body = """Dear Albert:
Your example.com account has been approved.  You can now visit
http://www.example.com/ and sign in using your Google Account to
access new features.
Please let us know if you have any questions.
The example.com Team
"""
    message.send()

@app_flask.route("/send_email", methods=['GET'])
def send_email():
    send_approved_mail("registred_email@domain.com")
    return "message sent"


app.mount("/v1", WSGIMiddleware(app_flask))

here app.yaml这里 app.yaml

runtime: python39
entrypoint: gunicorn -k uvicorn.workers.UvicornWorker app.main:app
instance_class: F1
app_engine_apis: true
inbound_services:
- mail
- mail_bounce

You will not see flask route on swagger.您不会在 swagger 上看到 flask 路线。 and for some reason, i could not sent email with app engine's default service account.由于某种原因,我无法使用应用引擎的默认服务帐户发送 email。 I had to register my email ( with same domain) here: https://console.cloud.google.com/appengine/settings/emailsenders?project=your_project我必须在这里注册我的 email(具有相同的域): https://console.cloud.google.com/appengine/settings/emailsenders?project=your_project

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GCP:由于缺少标签“最新”,Cloud Run 预览构建失败 - GCP: Cloud Run preview build fails because of a missing tag "latest" 向 reCAPTCHA Enterprise API 发送请求失败 - Sending request to reCAPTCHA Enterprise API fails 使用 Identity Aware Proxy 在 nodejs AppEngine 环境中获取用户 API 访问权限 - Getting Users API access in nodejs AppEngine environment with Identity Aware Proxy 使用 SendGrid 从逻辑应用向多个收件人发送电子邮件 - Sending emails with SendGrid from a logic app to multiple recipients 不断从 Google 请求 Appengine 上的 Acme Challenge 文件 - Constant request of Acme Challenge file on Appengine from Google 具有域范围委托的 Google 日历 API:events.watch 方法失败并显示“请求缺少有效的 API 密钥” - Google Calendar API with domain wide delegation: events.watch method fails with "The request is missing a valid API key" 通过 SendGrid 和 Google Workspaces 发送具有相同地址的电子邮件 - Sending emails with the same address via both SendGrid and Google Workspaces Google 日历 API 将事件数据发送到 Google PubSub - Google Calendar API sending event data to Google PubSub 使用 SendGrid 发送在本地工作但不在域上工作的电子邮件 API - Sending emails working locally but not on domain using SendGrid API 将 Java Google AppEngine 本地标准服务器连接到云数据库 | appengine-api-1.0-sdk-1.9.84.jar | IntelliJ 和云代码 - Connect Java Google AppEngine Local Standard Server to Cloud DB | appengine-api-1.0-sdk-1.9.84.jar | IntelliJ & Cloud Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM