X

Azure functions – Read file and use SendGrid to send an email

Azure functions

Was playing around with Azure functions and thought I would write a quick blog post on how to use Azure functions to read a text file and send an email out to me daily. Basically I went with a joke a day idea. So lets get started with writing some of the awesome code πŸ™‚

Warning Prerequisites Assumed
If you don’t have an Azure subscription, you can always create a free account before you begin.
Azure function
Login into your Azure account and click the New button found on the upper left-hand corner of the Azure portal, then select Compute > Function App. You should see a create button for function app.

I have named my app to zeytinmail since it was available, also used a new Resource Group, you can use an existing one.
Hosting plan is set to consumption plan and since I am in East Coast I have set up location in East US.
For storage I am just using a auto generated one, I could use an existing one but for simplicity sake just using a default one that was auto generated.


Now that we have it created we should see the overview of our function app.

We can now click on function and create a Timer function with CSharp.

We should see the run.csx file open up with the Run method pre-populated.

We will then upload the jokes.txt file onto the server. I got my jokes.txt from https://github.com/rdegges/yomomma-api/blob/master/jokes.txt. I just clicked on Add Button and created the file and copy and pasted the text into the file.

From there in order to read the file in Azure Function all I have to do is put the path of the file in my code.

File Path
You can find out the path by using Kudu in Azure function, usually they are stored in D drive with D:\home\site\wwwroot\{functionName}\file

Your solution would look something like below:

SendGrid
Next up we need to create yourself a SendGrid account.
SendGrid
Sendgrid has free account to send email out.

We need to set up our Application Setting to have our SendGrid API Key. We can go into the Application Setting and Add a new key, lets call it SendGridKey

We need to go into the Integrate section of the function where we want a new Output for our function. We will be selecting the SendGrid output to send an email out.

We can then put in our Api Key App Settings like below and from address if we want.

We now need to modify our function.json file to add SendGrid information, add this inside the binding array as another section.

{
      "type": "sendGrid",
      "name": "message",
      "apiKey": "SendGridKey",
      "from": "taswar@zeytinsoft.com",
      "direction": "out"
}

Finally we need to write the code to send the email out.

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

public static void Run(TimerInfo myTimer, TraceWriter log, out Mail message)
{
    var joke = GetQuote("jokes.txt");

    message = new Mail
    {        
        Subject = "Azure Mama joke"          
    };

    var personalization = new Personalization();
    // change to email of recipient
    personalization.AddTo(new Email("somebody@gmail.com"));

    var txtMessage = joke;

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

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

public static string GetQuote(string filename)
{
    var lines = File.ReadAllLines(@"D:\home\site\wwwroot\TimerTriggerCSharp1\" + filename);
    var r = new Random();
    var randomLineNumber = r.Next(0, lines.Length - 1);
    return lines[randomLineNumber];    
}

With this we will be able to send the email to a user with a joke of the day.

Last but not least we need to also set a schedule by clicking on Integrate and set the time for the schedule to send an email out. If you want to learn more about cron timer you can visit this site to learn more about it. https://codehollow.com/2017/02/azure-functions-time-trigger-cron-cheat-sheet/
Azure Timer Schedule

Summary

Here it is how to send an email out with a joke with SendGrid and Azure Functions. Have fun πŸ˜›

Categories: .NET Azure
Taswar Bhatti:
Related Post