One can use Azure PowerAutomate to process files received via email. We can setup one central email account and create different email alias for different client where PowerAutomate only need to mornitor one email account. Howerver, PowerAutomate doesn’t (at the time of this article) expose the alias email address in the Office365 Outlook email action. One solution to address this challenge is to use Azure Function App to extract the alias email address from the email body using Regex.
Following is the function app script:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string strToSearch = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String((string)data?.Text));
//string strToSearch = (string)data?.Text;
string regularExpression = data?.Pattern;
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline;
// var matches = System.Text.RegularExpressions.Regex.Matches(strToSearch, regularExpression, options);
// var responseString = JsonConvert.SerializeObject(matches, new JsonSerializerSettings()
// {
// ReferenceLoopHandling = ReferenceLoopHandling.Ignore
// });
var match = System.Text.RegularExpressions.Regex.Match(strToSearch, regularExpression, options).ToString();
//match = System.Text.RegularExpressions.Regex.Replace(match, ".*<", "", options);
//match = System.Text.RegularExpressions.Regex.Replace(match, ">.*", "", options);
match = System.Text.RegularExpressions.Regex.Replace(match, "To: ", "", options);
match = System.Text.RegularExpressions.Regex.Replace(match, "\\n", "", options);
match = System.Text.RegularExpressions.Regex.Replace(match, "\\r", "", options);
match = System.Text.RegularExpressions.Regex.Replace(match, "\\s+", "", options);
match = "{'value':'" + match + "'}";
return new ContentResult()
{
ContentType = "application/json",
Content = match
};
}
Then in PowerAutomate, we can add Export Email action to retrive the full email body and then add a HTTP action to call the above function app. The returned results from the Function App is a JSON string. We use Parse JSON action to get the actual value.