gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:05:34 27 4
gpt4 key购买 nike

我正在尝试使用以下代码从 GAE 应用程序发送电子邮件:

from google.appengine.api.mail import send_mail

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

我在 app.yaml 中配置了 api 的用法:

app_engine_apis: true

并使用 gcloud beta app deploy 部署到 App Engine。

但是,我得到这个错误:

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"

这似乎表明即使对于邮件服务的默认行为,也需要配置某种代理。但是,我找不到有关此代理设置的任何信息。而且,我最初的理解是,只有在单元测试或本地开发时才需要设置代理。

最佳答案

这是一个使用 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))

这里是 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

你不会在 swagger 上看到 flask 路由。出于某种原因,我无法使用 App Engine 的默认服务帐户发送电子邮件。我必须在这里注册我的电子邮件(具有相同的域):https://console.cloud.google.com/appengine/settings/emailsenders?project=your_project

关于python - 由于缺少 api 代理,从 Google Appengine 发送电子邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73357741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com