通过云功能通过云存储桶更改发送邮件
Send Mail thru cloud function over cloud bucket changes
我试图将云功能配置为在不使用第三方工具的情况下将文件推送到我们在 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 等第三方电子邮件服务集成,并将您的 Cloud Function 构建为 Cloud Function,例如:
# 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。
我试图将云功能配置为在不使用第三方工具的情况下将文件推送到我们在 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 等第三方电子邮件服务集成,并将您的 Cloud Function 构建为 Cloud Function,例如:
# 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。