【发布时间】:2020-01-13 21:20:50
【问题描述】:
我试图配置云功能,以便在文件被推送到 GCP 中的一个云存储桶时发送邮件,而不使用第三方工具。我已经查看了此处的概念,并确保我的发件人电子邮件已注册为授权发件人。
https://cloud.google.com/appengine/docs/standard/python/mail/sending-mail-with-mail-api
requirements.txt 添加了以下值:
google-cloud-storage==1.23.0
googleapis-common-protos==1.3.5
google-api-python-client
在需求文件中。但仍然得到同样的错误。
但是,当我尝试使用以下脚本时,结果出现了这个错误:
ModuleNotFoundError: No module named 'google.appengine' .
提前感谢您的建议和帮助。
from google.appengine.api import app_identity
from google.appengine.api import mail
import webapp2
def send_approved_mail(sender_address):
# [START send_mail]
mail.send_mail(sender=sender_address,
to="Albert Johnson <Albert.Johnson@example.com>",
subject="Your account has been approved",
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
""")
# [END send_mail]
class SendMailHandler(webapp2.RequestHandler):
def get(self):
send_approved_mail('{}@appspot.gserviceaccount.com'.format(
app_identity.get_application_id()))
self.response.content_type = 'text/plain'
self.response.write('Sent an email to Albert.')
app = webapp2.WSGIApplication([
('/send_mail', SendMailHandler),
], debug=True)
【问题讨论】:
-
您说您正在尝试使用 Cloud Functions,但这看起来像是 App Engine 应用。
google.appengine模块仅在 App Engine 运行时可用。我建议查看cloud.google.com/functions/docs/tutorials/sendgrid 的示例 -
谢谢达斯汀。是的,我正在以不同的方式进行尝试,看看是否可以在不使用 sendgrid 等第三方服务的情况下完成。
-
但是我似乎没有找到任何可以仅使用 gcp 资源通过云功能发送邮件的参考。如果没有其他方法,那么我将开始使用第三方服务来满足我们的要求。谢谢
-
Sendgrid 是这里推荐的解决方案。
标签: python google-cloud-platform google-cloud-functions google-cloud-storage sendmail