作者:手机用户2602891751 | 来源:互联网 | 2023-09-18 00:53
我有一个小的Python脚本,它每天检查一些失败的进程。该操作每天都会触发时间。
我希望脚本通过电子邮件向我发送所有失败过程的摘要。为此,我已经拥有一个SendGrid帐户,但是我在Azure中创建了一个新帐户。
但是,我不确定如何在我的Python脚本中实现SendGrid。 Azure文档有一个.NET应用程序指南,对我没有太大帮助。
以前有人做过这样的事吗?
我在github中找到了这段代码,您可能需要进行一些调整以支持最新的功能版本,
Azure Functions Queue Trigger Python Sample that send email by using SendGrid bindings.
SendGrid binding reference:
https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-sendgrid
"""
import os,json
_AZURE_FUNCTION_QUEUE_INPUT_ENV_NAME = "inputMessage"
_AZURE_FUNCTION_SENDGRID_OUTPUT_ENV_NAME = "outputMessage"
_SENDGRID_EMAIL_TO = "receiver@contoso.com"
_SENDGRID_EMAIL_SUBJECT = "Mail Subject"
# read the queue message
messageText = open(os.environ[_AZURE_FUNCTION_QUEUE_INPUT_ENV_NAME]).read()
print("Function script processed queue message '{0}'".format(messageText))
outmsg={
"personalizations": [
{
"to": [{ "email": _SENDGRID_EMAIL_TO }]
}
],"subject": _SENDGRID_EMAIL_SUBJECT,"content": [
{
"type": 'text/plain',"value": messageText
}
]
}
# Send email using SendGrid (output name: outputMessage)
print('Sending email using SendGrid:',outmsg)
with open(os.environ[_AZURE_FUNCTION_SENDGRID_OUTPUT_ENV_NAME],'wb') as f:
json.dump(outmsg,f)