0

I have a device that sends data in text form to a blob on AZURE, once the blob receives the data it triggers a function in azure functions which is basically and executable file made from c++ code, when it finishes it generates another text file which is stored in other blob

it is a very simple operation. But now I would like to receive an email each time the function goes trough successfully, I have searched on the web but the tutorial are very confusing or does not address this simple task.

I did developed the executable file with c++ but I inherited the azure function from someone else and I have zero experience with azure (i am electrical engineer not computer science). The azure function is written in C#, I just need a guide.

Thank you in advance!!

Diego Fernando Pava
  • 899
  • 3
  • 11
  • 24

3 Answers3

5

Use can add SendGrid output binding to you C# Azure Function. The binding in function.json would look something like this:

{
    "name": "mail",
    "type": "sendGrid",
    "direction": "out",
    "apiKey" : "MySendGridKey"
}

and function body like this:

#r "SendGrid"
using SendGrid.Helpers.Mail;

public static void Run(string input, out string yourExistingOutput, out Mail message)
{
    // Do the work you already do

    message = new Mail
    {        
        Subject = "Your Subject"          
    };

    var personalization = new Personalization();
    personalization.AddTo(new Email("recipient@contoso.com"));   

    Content content = new Content
    {
        Type = "text/plain",
        Value = "Email Body"
    };
    message.AddContent(content);
    message.AddPersonalization(personalization);
}

Read about SendGrid and SendGrid bindings.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
3

I had a similar problem which Mikhail's solution helped me solve. In my case I wanted the static Run method to be asynchronously, which meant I couldn't use the out parameter modifier. My solution's slightly different as it is a timer trigger and was implemented using Visual Studio and the NuGet package Microsoft.Azure.Webjobs.Extensions.SendGrid v2.1.0.

    [FunctionName("MyFunction")]
    public static async Task Run(
        [TimerTrigger("%TimerInterval%")]TimerInfo myTimer,
        [SendGrid] IAsyncCollector<Mail> messages,
        TraceWriter log)
    {
        log.Info($"C# Timer trigger function started execution at: {DateTime.Now}");

        // Do the work you already do...

        log.Info($"C# Timer trigger function finished execution at: {DateTime.Now}");

        var message = new Mail();
        message.From = new Email("from@email.com");
        var personalization = new Personalization();
        personalization.AddTo(new Email("to@email.com"));
        personalization.Subject = "Azure Function Executed Succesfully";
        message.AddPersonalization(personalization);

        var content = new Content
        {
            Type = "text/plain",
            Value = $"Function ran at {DateTime.Now}",
        };
        message.AddContent(content);
        await messages.AddAsync(message);
    }

This solution used Zain Rivzi's answer to How can I bind output values to my async Azure Function? and the SendGrid Web API v3 quick start guide.

dmb_acunim
  • 377
  • 1
  • 2
  • 10
0

The answer can be slightly simplified:

using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using SendGrid.Helpers.Mail;

public static class ExtractArchiveBlob
{
    [FunctionName("MyFunction")]
    public static async Task RunAsync(string input, 
                                      [SendGrid(ApiKey = "SendGridApiKey")] 
                                      IAsyncCollector<SendGridMessage> messageCollector)
    {
            var message = new SendGridMessage();

            message.AddContent("text/plain", "Example content");
            message.SetSubject("Example subject");
            message.SetFrom("from@email.com");
            message.AddTo("to@email.com");

            await messageCollector.AddAsync(message);
   }
}

Where SendGridApiKey is the app setting holding your Send Grid api key.

veuncent
  • 1,599
  • 1
  • 20
  • 17