Creating An Azure Logic App That Processes Email Attachments (Part 1)

Creating An Azure Logic App That Processes Email Attachments (Part 1)

Azure   /   Dec 29th, 2018   /   0 COMMENTS   /  A+ | a-
I wanted to create an app that would periodically check a mailbox for emails with attachments, strip the email down, extract the attachments and upload them to a website (Azure Web App).  Sounds pretty simple, right?  Well, here is how I did it:

Create a New Logic App:

Give it a Name, Subscription, Resource Group & Location:

Enable Log Analytics if you wish.

Now create an Azure Function that is an HTTP trigger that we will use to clean up the emails that are received.

Configure the Azure Function with a Name, Subscription, Resource Group, OS, Hosting Plan, Location & Storage Account.

Feel free to enable Application Insights if you wish.

Open the newly created Function App and select the Functions blade.

From here, you will have the option to create a new function:

Select New Function and in the template search, enter the text "HTTP Trigger" as shown here:

Select the HTTP Trigger template.

Give the HTTP Trigger a Name and set the Authoriztion Level to "Function":

Delete all of the text in the run.csx file and paste the following code in the window:
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Net.Http.Headers;

public static async Task Run(HttpRequest req, ILogger log)
    log.LogInformation($"Function started processing a requst at: {DateTime.Now}.");
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    string updatedBody = Regex.Replace(requestBody, "<.*?>", string.Empty);
    updatedBody = updatedBody.Replace("\\r\\n", " ");
    updatedBody = updatedBody.Replace(@" ", " ");
    log.LogInformation($"Function finished processing a requst at: {DateTime.Now}.");

    return updatedBody != null
        ? (ActionResult)new OkObjectResult($"{updatedBody}")
        : new BadRequestObjectResult("Please pass data to the request body");
Save the run.csx file:

Now let's test the function.  On the far right-hand side, you should see a test blade:

The blade should expand and you will see something similar to the following:

Delete what is listed in the Request Body and enter the following information text:
{"name": "

This is just a test."}


Select Run.

You should receive the following output:

Ok, for now we have the Logic App started and a Function to eventually tie into it.  Next, we will deep dive into the Logic App.

To be continued...
No comments posted...

Leave a Comment

Very catpcha image