Reputation: 6740
I have a small Python script that daily checks for some failed processes. This runs daily with a time trigger.
I want the script to email me a summary of all the failed processes. For this I already had a SendGrid account, but I created a new one from within Azure.
However, I am not sure how to implement SendGrid in my Python script. The Azure docs has a guide for a .NET application, which doesn't help me alot.
Has anyone done something like this before?
Upvotes: 2
Views: 1743
Reputation: 222657
I have found this code in github, you might need to tweak a bit to support the latest function version,
Azure Functions Queue Trigger Python Sample that send email by using SendGrid bindings.
SendGrid binding reference:
https://learn.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)
Upvotes: 1