我有一个小的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 = "[email protected]"
_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)