python - 通过云功能通过云存储桶更改发送邮件
问题描述
我试图配置云功能,以便在文件被推送到 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)
解决方案
您需要与第三方电子邮件服务(如 Sendgrid)集成,并将您的云函数构建为云函数,例如:
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
def send_mail(data, context):
message = Mail(
from_email='from_email@example.com',
to_emails='to@example.com',
subject='This is the subject',
html_content='This is the content'
)
response = sg.send(message)
有关更多详细信息,请参阅https://cloud.google.com/tasks/docs/tutorial-gcf。
推荐阅读
- android - 在中心显示回收站中的最后一项
- python - 如何使用子进程获取终端错误信息?
- android - 位图到 base64 编码不起作用。损坏的图像。此外,从服务器抛出错误“Base-64 字符数组或字符串的长度无效”
- c# - C#插入与主键具有相同值的外键
- javascript - 猫鼬中的用户、帖子和评论模型
- cadence-workflow - 是否有一种机制可以立即向工作流发出有关异步活动失败/超时的信号
- javascript - 我怎样才能从数组中获得最高的最低和中等值..?
- php - 如何在 PHP 中添加具有数字值的字符串
- c++ - 有没有更好的算法来找到连接的集群?
- css - 为什么 CSS 灰度滤镜会隐藏其他页面元素?