2

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?

rebellion
  • 6,628
  • 11
  • 48
  • 79

1 Answers1

1

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 = "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)
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Thanks! Do you know what how to solve this message? `The 'TimerTrigger' function is in error: The binding type(s) 'sendGrid' are not registered. Please ensure the type is correct and the binding extension is installed.` – rebellion Dec 20 '19 at 15:36
  • you need to add that extension https://github.com/Azure/azure-functions-eventgrid-extension/issues/54 – Sajeetharan Dec 20 '19 at 15:49