教程:使用 Azure 逻辑应用自动处理电子邮件和附件Tutorial: Automate handling emails and attachments with Azure Logic Apps

Azure 逻辑应用有助于跨 Azure 服务、Microsoft 服务、其他软件即服务 (SaaS) 应用以及本地系统自动完成工作流和集成数据。Azure Logic Apps helps you automate workflows and integrate data across Azure services, Microsoft services, other software-as-a-service (SaaS) apps, and on-premises systems. 本教程介绍如何生成可以处理传入电子邮件和任何附件的逻辑应用This tutorial shows how you can build a logic app that handles incoming emails and any attachments. 此逻辑应用分析电子邮件内容,将内容保存到 Azure 存储,然后发送查看该内容的通知。This logic app analyzes the email content, saves the content to Azure storage, and sends notifications for reviewing that content.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 设置 Azure 存储和存储资源管理器,以便查看保存的电子邮件和附件。Set up Azure storage and Storage Explorer for checking saved emails and attachments.
  • 创建用于从电子邮件中删除 HTML 的 Azure 函数Create an Azure function that removes HTML from emails. 本教程包括可以用于此函数的代码。This tutorial includes the code that you can use for this function.
  • 创建空白逻辑应用。Create a blank logic app.
  • 添加一个触发器,用于监视电子邮件中的附件。Add a trigger that monitors emails for attachments.
  • 添加一个条件,用于检查电子邮件是否有附件。Add a condition that checks whether emails have attachments.
  • 添加一个操作,用于在电子邮件有附件时调用 Azure 函数。Add an action that calls the Azure function when an email has attachments.
  • 添加一个操作,用于为电子邮件和附件创建存储 Blob。Add an action that creates storage blobs for emails and attachments.
  • 添加一个操作,用于发送电子邮件通知。Add an action that sends email notifications.

完成后,逻辑应用看起来大致与以下工作流类似:When you're done, your logic app looks like this workflow at a high level:

完成的逻辑应用概览

如果没有 Azure 订阅,可以在开始前注册一个 Azure 试用帐户If you don't have an Azure subscription, sign up for a trial Azure account before you begin.

先决条件Prerequisites

  • 逻辑应用支持的电子邮件提供商(例如 Office 365 Outlook、Outlook.com 或 Gmail)提供的电子邮件帐户。An email account from an email provider supported by Logic Apps, such as Office 365 Outlook, Outlook.com, or Gmail. 至于其他提供商,请查看此处的连接器列表For other providers, review the connectors list here.

    此逻辑应用使用 Office 365 Outlook 帐户。This logic app uses an Office 365 Outlook account. 如果使用其他电子邮件帐户,则常规步骤保持不变,但 UI 显示可能稍有不同。If you use a different email account, the general steps stay the same, but your UI might appear slightly different.

  • 下载并安装免费 Microsoft Azure 存储资源管理器Download and install the free Microsoft Azure Storage Explorer. 此工具可以用于检查存储容器是否已正确设置。This tool helps you check that your storage container is correctly set up.

登录到 Azure 门户Sign in to Azure portal

使用 Azure 帐户凭据登录到 Azure 门户Sign in to the Azure portal with your Azure account credentials.

设置用于保存附件的存储。Set up storage to save attachments

可以在 Azure 存储容器中以 Blob 形式保存传入电子邮件和附件。You can save incoming emails and attachments as blobs in an Azure storage container.

  1. 请先使用以下设置创建存储帐户,然后才能创建存储容器:Before you can create a storage container, Create a storage account with these settings:

    设置Setting Value 说明Description
    名称Name attachmentstorageacctattachmentstorageacct 存储帐户的名称The name for your storage account
    部署模型Deployment model 资源管理器Resource manager 用于管理资源部署的部署模型The deployment model for managing resource deployment
    帐户类型Account kind 常规用途General purpose 存储帐户类型The storage account type
    位置Location 中国东部China East 用于存储存储帐户信息的区域The region where to store information about your storage account
    复制Replication 本地冗余存储 (LRS)Locally redundant storage (LRS) 此设置指定如何复制、存储、管理和同步数据。This setting specifies how your data is copied, stored, managed, and synchronized. 请参阅本地冗余存储 (LRS):适用于 Azure 存储的低成本数据冗余See Locally redundant storage (LRS): Low-cost data redundancy for Azure Storage.
    性能Performance 标准Standard 此设置指定支持的数据类型以及用于存储数据的介质。This setting specifies the data types supported and media for storing data. 请参阅存储帐户的类型See Types of storage accounts.
    需要安全传输Secure transfer required 已禁用Disabled 此设置指定从连接进行请求所需的安全性。This setting specifies the security required for requests from connections. 请参阅需要安全传输See Require secure transfer.
    订阅Subscription <your-Azure-subscription-name><your-Azure-subscription-name> Azure 订阅的名称The name for your Azure subscription
    资源组Resource group LA-Tutorial-RGLA-Tutorial-RG 用于组织和管理相关资源的 Azure 资源组的名称。The name for the Azure resource group used to organize and manage related resources.

    注意: 资源组存在于特定的区域内。Note: A resource group exists inside a specific region. 本教程中的项目可能不在所有区域提供,请尽可能尝试使用同一区域。Although the items in this tutorial might not be available in all regions, try to use the same region when possible.

    配置虚拟网络Configure virtual networks 已禁用Disabled 对于本教程,请保留“禁用”设置。For this tutorial, keep the Disabled setting.
  2. 在 Azure 部署存储帐户以后,请获取存储帐户的访问密钥:After Azure deploys your storage account, get your storage account's access key:

    1. 在存储帐户菜单中的“设置”下,选择“访问密钥”。On your storage account menu, under Settings, select Access keys.

    2. 复制存储帐户名称和“密钥 1”,然后将这些值保存到某个安全位置。Copy your storage account name and key1, and then save those values somewhere safe.

      复制并保存存储帐户名称和密钥

    若要获取存储帐户的访问密钥,也可以使用 Azure PowerShellAzure CLITo get your storage account's access key, you can also use Azure PowerShell or Azure CLI.

  3. 为电子邮件附件创建 Blob 存储容器。Create a blob storage container for your email attachments.

    1. 在存储帐户菜单中选择“概述”。On your storage account menu, select Overview. 在“服务”下选择“Blob”。Under Services, select Blobs.

      添加 Blob 存储容器

    2. “容器”页打开后,在工具栏上选择“容器”。After the Containers page opens, on the toolbar, select Container.

    3. 在“新建容器”下,输入“attachments”作为容器名称。Under New container, enter "attachments" as your container name. 在“公共访问级别”下选择“容器(对容器和 Blob 进行匿名读取访问)”,然后选择“确定”。Under Public access level, select Container (anonymous read access for containers and blobs), and then choose OK.

      完成后,即可在 Azure 门户的存储帐户中找到存储容器,如下所示:When you're done, you can find your storage container in your storage account here in the Azure portal:

    完成的存储容器

    若要创建存储容器,也可以使用 Azure PowerShellAzure CLITo create a storage container, you can also use Azure PowerShell, or Azure CLI.

接下来,将存储资源管理器连接到存储帐户。Next, connect Storage Explorer to your storage account.

设置存储资源管理器Set up Storage Explorer

现在,请将存储资源管理器连接到存储帐户,以确认逻辑应用能否正确地在存储容器中将附件保存为 Blob。Now, connect Storage Explorer to your storage account so you can confirm that your logic app can correctly save attachments as blobs in your storage container.

  1. 打开 Microsoft Azure 存储资源管理器。Open Microsoft Azure Storage Explorer.

    存储资源管理器将提示你提供存储帐户的连接。Storage Explorer prompts you for a connection to your storage account.

  2. 在“连接到 Azure 存储”窗格中,In the Connect to Azure Storage pane,

    选择“使用存储帐户名和密钥”并选择“下一步”。select Use a storage account name and key, and then choose Next.

    存储资源管理器 - 连接到存储帐户

    Tip

    如果未显示提示,请在存储资源管理器工具栏上选择“添加帐户”。If no prompt appears, on the Storage Explorer toolbar, choose Add account.

  3. 在“帐户名称”下,提供存储帐户名称。Under Account name, provide your storage account name. 在“帐户密钥”下,提供前面保存的访问密钥。Under Account key, provide the access key you previously saved. 选择“下一步”。Choose Next.

  4. 确认连接信息,然后选择“连接”。Confirm your connection information, and then choose Connect.

    存储资源管理器会创建连接,并在“资源管理器”窗口中的“(本地和附加)”>“存储帐户”下显示你的存储帐户。Storage Explorer creates the connection, and shows your storage account in the Explorer window under (Local and Attached) > Storage Accounts.

  5. 若要查找自己的 Blob 存储容器,请在“存储帐户”下展开自己的存储帐户(此处为 attachmentstorageacct),然后展开“Blob 容器”,在其中可以找到“附件”容器,例如:To find your blob storage container, under Storage Accounts, expand your storage account, which is attachmentstorageacct here, and then expand Blob Containers where you find the attachments container, for example:

    存储资源管理器 - 查找存储容器

接下来,创建用于从传入电子邮件中删除 HTML 的 Azure 函数Next, create an Azure function that removes HTML from incoming email.

创建用于清理 HTML 的函数Create function to clean HTML

现在,请使用这些步骤提供的代码片段,创建一个可从每封传入电子邮件中删除 HTML 的 Azure 函数。Now, use the code snippet provided by these steps to create an Azure function that removes HTML from each incoming email. 这样就可以让电子邮件内容更干净,更容易处理。That way, the email content is cleaner and easier to process. 然后即可从逻辑应用调用此函数。You can then call this function from your logic app.

  1. 请先使用以下设置创建函数应用,然后即可创建函数:Before you can create a function, create a function app with these settings:

    设置Setting Value 说明Description
    应用名称App name CleanTextFunctionAppCleanTextFunctionApp 函数应用的全局唯一的描述性名称A globally unique and descriptive name for your function app
    订阅Subscription <your-Azure-subscription-name><your-Azure-subscription-name> 以前使用过的同一 Azure 订阅The same Azure subscription that you previously used
    资源组Resource Group LA-Tutorial-RGLA-Tutorial-RG 以前使用过的同一 Azure 资源组The same Azure resource group that you previously used
    托管计划Hosting Plan 使用计划Consumption Plan 此设置决定了如何分配和缩放用于运行函数应用的资源,例如计算能力。This setting determines how to allocate and scale resources, such as computing power, for running your function app. 请参阅托管计划比较See hosting plans comparison.
    位置Location 中国东部China East 以前使用过的同一区域The same region that you previously used
    运行时堆栈Runtime stack 首选语言Preferred language 选择支持你喜欢的函数编程语言的运行时。Choose a runtime that supports your favorite function programming language. 对于 C# 和 F# 函数,选择 .NET。Choose .NET for C# and F# functions.
    存储Storage cleantextfunctionstorageacctcleantextfunctionstorageacct 为函数应用创建存储帐户。Create a storage account for your function app. 只使用小写字母和数字。Use only lowercase letters and numbers.

    注意: 此存储帐户包含函数应用,不同于以前创建的用于电子邮件附件的存储帐户。Note: This storage account contains your function apps, and differs from your previously created storage account for email attachments.

    如果函数应用没有在部署后自动打开,请在 Azure 门户中找到该应用。If your function app doesn't automatically open after deployment, find your app in the Azure portal. 在 Azure 主菜单中选择“应用服务”,然后选择自己的函数应用。On the main Azure menu, select Function Apps, and select your function app.

    选择函数应用

    如果“函数应用”未显示在 Azure 菜单中,请转到“所有服务”。If Function Apps doesn't appear on the Azure menu, go to All services instead. 在搜索框中找到并选择“Function App”。In the search box, find and select Function Apps. 有关详细信息,请参阅创建函数For more information, see Create your function.

    否则,Azure 会自动打开你的函数应用,如下所示:Otherwise, Azure automatically opens your function app as shown here:

    创建的函数应用

    若要创建函数应用,也可以使用 Azure CLIPowerShell 和资源管理器模板To create a function app, you can also use Azure CLI, or PowerShell and Resource Manager templates.

  2. Function App 下展开 CleanTextFunctionApp,然后选择“函数”。Under Function Apps, expand CleanTextFunctionApp, and select Functions. 在函数工具栏上选择“新建函数”。On the functions toolbar, select New function.

    创建新的函数

  3. 在“选择下面的模板或转到快速入门”下,打开“方案”列表并选择“Core”。Under Choose a template below or go to the quickstart, open the Scenario list, and select Core. 在“HTTP 触发器”模板中,选择“C#”。In the HTTP Trigger template, select C#.

    选择函数模板

    Note

    此示例提供 C# 示例代码,使你无需了解 C# 即可学习此示例。This example provides you the C# sample code so you can follow the example without having to know C#.

  4. 在“新建函数”窗格中的“名称”下,输入 RemoveHTMLFunctionIn the New Function pane, under Name, enter RemoveHTMLFunction. 将“授权级别”保留设置为“函数”,然后选择“创建”。Keep Authorization level set to Function, and choose Create.

    为函数命名

  5. 打开编辑器后,将模板代码替换为以下示例代码,以便删除 HTML 并将结果返回给调用方:After the editor opens, replace the template code with this sample code, which removes the HTML and returns results to the caller:

     #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)
     {
         log.LogInformation("HttpWebhook triggered");
    
         // Parse query parameter
         string emailBodyContent = await new StreamReader(req.Body).ReadToEndAsync();
    
          // Replace HTML with other characters
         string updatedBody = Regex.Replace(emailBodyContent, "<.*?>", string.Empty);
         updatedBody = updatedBody.Replace("\\r\\n", " ");
         updatedBody = updatedBody.Replace(@"&nbsp;", " ");
    
         // Return cleaned text
         return (ActionResult) new OkObjectResult(new { updatedBody });
     }
    
  6. 完成后,选择“保存”。When you're done, choose Save. 若要测试函数,请在编辑器右边缘的箭头 (<) 图标下,选择“测试”。To test your function, at the editor's right edge, under the arrow (<) icon, choose Test.

    打开“测试”窗格。

  7. 在“测试”窗格的“请求正文”下,输入下面这行内容,然后选择“运行”。In the Test pane, under Request Body, enter this line, and choose Run.

    {"name": "<p><p>Testing my function</br></p></p>"}
    

    测试函数

    “输出”窗口显示函数的结果:The Output window shows the function's result:

    {"updatedBody":"{\"name\": \"Testing my function\"}"}
    

在检查函数正常以后,创建逻辑应用。After checking that your function works, create your logic app. 虽然本教程介绍的是如何创建从电子邮件中删除 HTML 的函数,但逻辑应用还提供了“HTML 到文本”连接器。Although this tutorial shows how to create a function that removes HTML from emails, Logic Apps also provides an HTML to Text connector.

创建逻辑应用Create your logic app

  1. 在 Azure 主菜单中,选择“创建资源” > “集成” > “逻辑应用”。On the main Azure menu, select Create a resource > Integration > Logic App.

    创建逻辑应用

  2. 在“创建逻辑应用”下,提供有关逻辑应用的信息,如下所示。Under Create logic app, provide this information about your logic app as shown and described. 完成后,请选择“固定到仪表板” > “创建”。When you're done, choose Pin to dashboard > Create.

    提供逻辑应用信息

    设置Setting Value 说明Description
    名称Name LA-ProcessAttachmentLA-ProcessAttachment 逻辑应用的名称The name for your logic app
    订阅Subscription <your-Azure-subscription-name><your-Azure-subscription-name> 以前使用过的同一 Azure 订阅The same Azure subscription that you previously used
    资源组Resource group LA-Tutorial-RGLA-Tutorial-RG 以前使用过的同一 Azure 资源组The same Azure resource group that you previously used
    位置Location 中国东部China East 以前使用过的同一区域The same region that you previously used
    Log AnalyticsLog Analytics 关闭Off 对于本教程,请选择“关闭”设置。For this tutorial, choose the Off setting.
  3. 在 Azure 部署应用以后,逻辑应用设计器会打开并显示一个页面,其中包含简介视频以及针对常用逻辑应用模式的模板。After Azure deploys your app, the Logic Apps Designer opens and shows a page with an introduction video and templates for common logic app patterns. 在“模板”下选择“空白逻辑应用”。Under Templates, choose Blank Logic App.

    选择空白逻辑应用模板

接下来,请添加一个触发器,侦听有附件的传入电子邮件。Next, add a trigger that listens for incoming emails that have attachments. 每个逻辑应用都必须从触发器开始,在发生特定事件或新数据符合特定条件的情况下触发。Every logic app must start with a trigger, which fires when a specific event happens or when new data meets a specific condition. 有关详细信息,请参阅创建第一个逻辑应用For more information, see Create your first logic app.

监视传入电子邮件Monitor incoming email

  1. 在设计器上的搜索框中,输入“收到新电子邮件时”作为筛选器。On the designer in the search box, enter "when new email arrives" as your filter. 针对电子邮件提供程序选择以下触发器:新电子邮件到达时 - <your-email-provider>Select this trigger for your email provider: When a new email arrives - <your-email-provider>

    例如:For example:

    针对电子邮件提供程序选择以下触发器:“新电子邮件到达时”

    • 对于 Azure 工作或学校帐户,请选择“Office 365 Outlook”。For Azure work or school accounts, select Office 365 Outlook.
    • 对于个人 Microsoft 帐户,请选择“Outlook.com”。For personal Microsoft accounts, select Outlook.com.
  2. 如果系统要求提供凭据,请登录到自己的电子邮件帐户,以便逻辑应用可以连接到该电子邮件帐户。If you're asked for credentials, sign in to your email account so Logic Apps can connect to your email account.

  3. 现在,请提供触发器用来筛选新电子邮件的条件。Now provide the criteria the trigger uses to filter new email.

    1. 指定用于检查电子邮件的文件夹、时间间隔和频率。Specify the folder, interval, and frequency for checking emails.

      指定用于检查邮件的文件夹、时间间隔和频率

      设置Setting Value 说明Description
      文件夹Folder InboxInbox 要检查的电子邮件文件夹The email folder to check
      时间间隔Interval 11 在两次检查之间需等待的时间间隔数The number of intervals to wait between checks
      频率Frequency 分钟Minute 两次检查的间隔的时间单位The unit of time for each interval between checks
    2. 选择“显示高级选项”并指定以下设置:Choose Show advanced options and specify these settings:

      设置Setting Value 说明Description
      带有附件Has Attachment Yes 仅获取带有附件的电子邮件。Get only emails with attachments.

      注意: 此触发器不删除帐户中的任何电子邮件,仅检查新邮件,并且仅处理与主题筛选器匹配的电子邮件。Note: The trigger doesn't remove any emails from your account, checking only new messages and processing only emails that match the subject filter.

      包括附件Include Attachments Yes 获取充当工作流输入的附件,而不是仅仅检查是否有附件。Get the attachments as input for your workflow, rather than just check for attachments.
      主题筛选器Subject Filter Business Analyst 2 #423501 要在电子邮件主题中查找的文本The text to find in the email subject
  4. 若要立即隐藏触发器的详细信息,请单击触发器的标题栏。To hide the trigger's details for now, click inside the trigger's title bar.

    折叠形状即可隐藏详细信息

  5. 保存逻辑应用。Save your logic app. 在设计器工具栏上,选择“保存”。On the designer toolbar, choose Save.

    逻辑应用现已生成,但除了检查电子邮件,不能执行任何操作。Your logic app is now live but doesn't do anything other check your emails. 接下来,请添加一个条件,用于指定继续执行工作流所需的条件。Next, add a condition that specifies criteria to continue workflow.

检查是否有附件Check for attachments

现在添加一个条件,以便仅选择包含附件的电子邮件。Now add a condition that selects only emails that have attachments.

  1. 在触发器下,选择“新建步骤” > “添加条件”。Under the trigger, choose New step > Add a condition.

    “新建步骤”、“添加条件”

  2. 重命名此条件,并提供更好的说明。Rename the condition with a better description.

    1. 在条件的标题栏中,选择省略号 (...) 按钮 >“重命名”。On the condition's title bar, choose ellipses (...) button > Rename.

      重命名条件

    2. 重命名条件并提供以下说明:If email has attachments and key subject phraseRename your condition with this description: If email has attachments and key subject phrase

  3. 创建一个条件,用于检查包含附件的电子邮件。Create a condition that checks for emails that have attachments.

    1. 在“和”下面的第一行中,单击左侧框的内部。On the first row under And, click inside the left box. 从显示的动态内容列表中,选择“包含附件”属性。From the dynamic content list that appears, select the Has Attachment property.

      构建条件

    2. 在中间框中,保留运算符“等于”。In the middle box, keep the operator is equal to.

    3. 在右侧框中输入 True 值,该值将与触发器中的“包含附件”属性值进行比较。In the right box, enter True as the value to compare with the Has Attachment property value from the trigger.

      构建条件

      如果两个值相等,则电子邮件至少有一个附件,条件通过,工作流继续。If both values are equal, the email has at least one attachment, the condition passes, and the workflow continues.

    在可在代码编辑器窗口中查看的基础逻辑应用定义中,此条件如以下示例所示:In your underlying logic app definition, which you can view in the code editor window, this condition looks like this example:

    "Condition": {
       "actions": { <actions-to-run-when-condition-passes> },
       "expression": {
          "and": [ {
             "equals": [
                "@triggerBody()?['HasAttachment']",
                  "True"
             ]
          } ]
       },
       "runAfter": {},
       "type": "If"
    }
    
  4. 保存逻辑应用。Save your logic app. 在设计器工具栏上,选择“保存”。On the designer toolbar, choose Save.

测试条件Test your condition

现在,请测试条件是否正常工作:Now, test whether the condition works correctly:

  1. 如果逻辑应用尚未运行,请在设计器工具栏中选择“运行”。If your logic app isn't running already, choose Run on the designer toolbar.

    此步骤手动启动逻辑应用,不需等待指定的时间间隔。This step manually starts your logic app without having to wait until your specified interval passes. 但在测试性电子邮件到达收件箱之前,什么也不会发生。However, nothing happens until the test email arrives in your inbox.

  2. 向自己发送一封满足以下条件的电子邮件:Send yourself an email that meets this criteria:

    • 电子邮件主题的文本是在触发器的主题筛选器中指定的:Business Analyst 2 #423501Your email's subject has the text that you specified in the trigger's Subject filter: Business Analyst 2 #423501

    • 电子邮件有一个附件。Your email has one attachment. 现在,请直接创建一个空的文本文件,然后将该文件附加到电子邮件。For now, just create one empty text file and attach that file to your email.

    当电子邮件到达时,逻辑应用会检查其中是否有附件和指定的主题文本。When the email arrives, your logic app checks for attachments and the specified subject text. 如果满足条件,触发器就会触发,促使逻辑应用引擎创建一个逻辑应用实例并启动工作流。If the condition passes, the trigger fires and causes the Logic Apps engine to create a logic app instance and start the workflow.

  3. 若要检查触发器是否已成功触发以及逻辑应用是否已成功运行,请在逻辑应用菜单中选择“概览”。To check that the trigger fired and the logic app ran successfully, on the logic app menu, choose Overview.

    检查触发器和运行历史记录

    如果触发器成功触发,但逻辑应用没有触发或运行,请参阅排查逻辑应用问题If your logic app didn't trigger or run despite a successful trigger, see Troubleshoot your logic app.

接下来,定义需要针对 If true 分支执行的操作。Next, define the actions to take for the If true branch. 若要保存电子邮件及其附件,请从电子邮件正文中删除 HTML,然后在电子邮件和附件的存储容器中创建 Blob。To save the email along with any attachments, remove any HTML from the email body, then create blobs in the storage container for the email and attachments.

Note

当电子邮件没有附件时,逻辑应用不需针对 If false 分支执行任何操作。Your logic app doesn't have to do anything for the If false branch when an email doesn't have attachments. 完成本教程后,可以针对 If false 分支添加需要执行的适当操作,作为附加练习。As a bonus exercise after you finish this tutorial, you can add any appropriate action that you want to take for the If false branch.

调用 RemoveHTMLFunctionCall RemoveHTMLFunction

此步骤将前面创建的 Azure 函数添加到逻辑应用,并将电子邮件正文内容从电子邮件触发器传递给函数。This step adds your previously created Azure function to your logic app and passes the email body content from email trigger to your function.

  1. 在逻辑应用菜单中选择“逻辑应用设计器”。On the logic app menu, select Logic App Designer. If true 分支中,选择“添加操作”。In the If true branch, choose Add an action.

    在“If true”中添加操作

  2. 在搜索框中,查找“azure functions”,然后选择以下操作:选择 Azure 函数 - Azure FunctionsIn the search box, find "azure functions", and select this action: Choose an Azure function - Azure Functions

    为“选择 Azure 函数”选择操作

  3. 选择你以前创建的函数应用:CleanTextFunctionAppSelect your previously created function app: CleanTextFunctionApp

    选择 Azure 函数应用

  4. 现在,选择你的函数:RemoveHTMLFunctionNow select your function: RemoveHTMLFunction

    选择 Azure 函数

  5. 重命名函数形状并提供以下说明:Call RemoveHTMLFunction to clean email bodyRename your function shape with this description: Call RemoveHTMLFunction to clean email body

  6. 现在,请指定函数要处理的输入。Now specify the input for your function to process.

    1. 在“请求正文”下,输入以下带尾随空格的文本:Under Request Body, enter this text with a trailing space:

      { "emailBody":

      在后续步骤中处理此输入时,会一直显示有关 JSON 无效的错误,直到正确将输入格式设置为 JSON。While you work on this input in the next steps, an error about invalid JSON appears until your input is correctly formatted as JSON. 以前测试此函数时,为此函数指定的输入使用 JavaScript 对象表示法 (JSON)。When you previously tested this function, the input specified for this function used JavaScript Object Notation (JSON). 因此,请求正文也必须使用同一格式。So, the request body must also use the same format.

      此外,将光标置于“请求正文”框中时,会显示动态内容列表,以便可以选择前述操作中可用的属性值。Also, when your cursor is inside the Request body box, the dynamic content list appears so you can select property values available from previous actions.

    2. 在动态内容列表中的“收到新电子邮件时”下,选择“正文”属性。From the dynamic content list, under When a new email arrives, select the Body property. 请记得在此属性的后面添加右大括号:}After this property, remember to add the closing curly brace: }

      指定要传递到函数的请求正文

    完成后,函数的输入如以下例所示:When you're done, the input to your function looks like this example:

    现已完成要传递给函数的请求正文

  7. 保存逻辑应用。Save your logic app.

接下来,请添加一个操作,以便在存储容器中创建用于保存电子邮件正文的 Blob。Next, add an action that creates a blob in your storage container so you can save the email body.

创建用于电子邮件正文的 BlobCreate blob for email body

  1. 在“If true”块中和 Azure 函数的下面,选择“添加操作”。In the If true block and under your Azure function, choose Add an action.

  2. 在搜索框中,输入“创建 Blob”作为筛选器,然后选择以下操作:创建 Blob - Azure Blob 存储In the search box, enter "create blob" as your filter, and select this action: Create blob - Azure Blob Storage

    添加为电子邮件正文创建 Blob 的操作

  3. 如下所示和所述,使用这些设置来与存储帐户建立连接。Create a connection to your storage account with these settings as shown and described here. 完成后,选择“创建”。When you're done, choose Create.

    创建到存储帐户的连接

    设置Setting Value 说明Description
    连接名称Connection Name AttachmentStorageConnectionAttachmentStorageConnection 连接的描述性名称A descriptive name for the connection
    存储帐户Storage Account attachmentstorageacctattachmentstorageacct 以前创建的用于保存附件的存储帐户的名称The name for the storage account that you previously created for saving attachments
  4. 重命名“创建 Blob”操作并提供以下说明:Create blob for email bodyRename the Create blob action with this description: Create blob for email body

  5. 在“创建 Blob”操作中,按如下所示和所述提供信息,并选择创建 Blob 所需的字段:In the Create blob action, provide this information, and select these fields to create the blob as shown and described:

    提供适用于电子邮件正文的 Blob 信息

    设置Setting Value 说明Description
    文件夹路径Folder path /attachments/attachments 以前创建的容器的路径和名称。The path and name for the container that you previously created. 对于此示例,请单击文件夹图标,然后选择“/attachments”容器。For this example, click the folder icon, and then select the "/attachments" container.
    Blob 名称Blob name “发件人”字段From field 对于此示例,请使用发件人的姓名作为 Blob 名称。For this example, use the sender's name as the blob's name. 单击此框内部以显示动态内容列表,然后在“收到新电子邮件时”操作下面选择“发件人”字段。Click inside this box so that the dynamic content list appears, and then select the From field under the When a new email arrives action.
    Blob 内容Blob content “内容”字段Content field 对于此示例,请使用不包含 HTML 的电子邮件正文作为 Blob 内容。For this example, use the HTML-free email body as the blob content. 单击此框内部以显示动态内容列表,然后在“调用 RemoveHTMLFunction 来清理电子邮件正文”操作下面选择“正文”。Click inside this box so that the dynamic content list appears, and then select Body under the Call RemoveHTMLFunction to clean email body action.

    完成后,此操作如以下示例所示:When you're done, the action looks like this example:

    现已完成“创建 Blob”操作

  6. 保存逻辑应用。Save your logic app.

检查附件处理情况Check attachment handling

现在,请测试逻辑应用是否采用指定的方式来处理电子邮件:Now test whether your logic app handles emails the way that you specified:

  1. 如果逻辑应用尚未运行,请在设计器工具栏中选择“运行”。If your logic app isn't running already, choose Run on the designer toolbar.

  2. 向自己发送一封满足以下条件的电子邮件:Send yourself an email that meets this criteria:

    • 电子邮件主题的文本是在触发器的主题筛选器中指定的:Business Analyst 2 #423501Your email's subject has the text that you specified in the trigger's Subject filter: Business Analyst 2 #423501

    • 电子邮件至少有一个附件。Your email has at least one attachment. 现在,请直接创建一个空的文本文件,然后将该文件附加到电子邮件。For now, just create one empty text file and attach that file to your email.

    • 电子邮件的正文中有一些测试内容,例如:Your email has some test content in the body, for example:

      Testing my logic app
      

    如果触发器成功触发,但逻辑应用没有触发或运行,请参阅排查逻辑应用问题If your logic app didn't trigger or run despite a successful trigger, see Troubleshoot your logic app.

  3. 检查逻辑应用是否已将电子邮件保存到正确的存储容器。Check that your logic app saved the email to the correct storage container.

    1. 在存储资源管理器中展开“(本地和附加)”> “存储帐户”>“attachmentstorageacct (外部)”> “Blob 容器”> attachmentsIn Storage Explorer, expand (Local and Attached) > Storage Accounts > attachmentstorageacct (External) > Blob Containers > attachments.

    2. 检查 attachments 容器中是否有电子邮件。Check the attachments container for the email.

      此时只有电子邮件显示在容器中,因为逻辑应用尚未处理附件。At this point, only the email appears in the container because the logic app doesn't process the attachments yet.

      检查存储资源管理器中是否有保存的电子邮件

    3. 完成后,删除存储资源管理器中的电子邮件。When you're done, delete the email in Storage Explorer.

  4. (可选)若要测试 If false 分支(目前不执行任何操作),可以发送一封不符合条件的电子邮件。Optionally, to test the If false branch, which does nothing at this time, you can send an email that doesn't meet the criteria.

接下来,添加一个用于处理所有电子邮件附件的循环。Next, add a loop to process all the email attachments.

处理附件Process attachments

若要处理电子邮件中的每个附件,请将 For each 循环添加到逻辑应用的工作流。To process each attachment in the email, add a For each loop to your logic app's workflow.

  1. 在“创建用于电子邮件正文的 Blob”形状下,选择“更多” > “添加 for each”。Under the Create blob for email body shape, select More > Add a for each.

    添加“for each”循环

  2. 重命名循环并提供以下说明:For each email attachmentRename your loop with this description: For each email attachment

  3. 现在指定供循环处理的数据。Now specify the data for the loop to process. 单击“从先前的步骤中选择一个输出”框的内部以打开动态内容列表,然后选择“附件”。Click inside the Select an output from previous steps box so that the dynamic content list opens, and then select Attachments.

    选择“附件”

    “附件”字段传入一个数组,其中包含电子邮件随附的所有附件。The Attachments field passes in an array that contains all the attachments included with an email. For each 循环针对每个通过数组传入的项目重复操作。The For each loop repeats actions on each item that's passed in with the array.

  4. 保存逻辑应用。Save your logic app.

接下来,添加可在 attachments 存储容器中将每个附件另存为 Blob 的操作。Next, add the action that saves each attachment as a blob in your attachments storage container.

为每个附件创建 BlobCreate blob for each attachment

  1. 在“对每个电子邮件附件”循环中选择“添加操作”,以便指定要对每个已发现附件执行的任务。In the For each email attachment loop, choose Add an action so you can specify the task to perform on each found attachment.

    添加用于循环的操作

  2. 在搜索框中,输入“创建 Blob”作为筛选器,然后选择以下操作:创建 Blob - Azure Blob 存储In the search box, enter "create blob" as your filter, and then select this action: Create blob - Azure Blob Storage

    添加用于创建 Blob 的操作

  3. 重命名“创建 Blob 2”操作并提供以下说明:Create blob for each email attachmentRename the Create blob 2 action with this description: Create blob for each email attachment

  4. 在“为每个电子邮件附件创建 Blob”操作中,按如下所示和所述提供信息,并为要创建的每个 Blob 选择属性:In the Create blob for each email attachment action, provide this information, and select the properties for each blob you want to create as shown and described:

    提供 Blob 信息

    设置Setting Value 说明Description
    文件夹路径Folder path /attachments/attachments 以前创建的容器的路径和名称。The path and name for the container that you previously created. 对于此示例,请单击文件夹图标,然后选择“/attachments”容器。For this example, click the folder icon, and then select the "/attachments" container.
    Blob 名称Blob name “名称”字段Name field 对于此示例,请使用附件的名称作为 Blob 名称。For this example, use the attachment's name as the blob's name. 单击此框内部以显示动态内容列表,然后在“收到新电子邮件时”操作下面选择“名称”字段。Click inside this box so that the dynamic content list appears, and then select the Name field under the When a new email arrives action.
    Blob 内容Blob content “内容”字段Content field 对于此示例,请使用“内容”字段作为 Blob 内容。For this example, use the Content field as the blob content. 单击此框内部以显示动态内容列表,然后在“收到新电子邮件时”操作下面选择“内容”字段。Click inside this box so that the dynamic content list appears, and then select Content under the When a new email arrives action.

    完成后,此操作如以下示例所示:When you're done, the action looks like this example:

    现已完成“创建 Blob”操作

  5. 保存逻辑应用。Save your logic app.

检查附件处理情况Check attachment handling

接下来,请测试逻辑应用是否采用指定的方式来处理附件:Next, test whether your logic app handles the attachments the way that you specified:

  1. 如果逻辑应用尚未运行,请在设计器工具栏中选择“运行”。If your logic app isn't running already, choose Run on the designer toolbar.

  2. 向自己发送一封满足以下条件的电子邮件:Send yourself an email that meets this criteria:

    • 电子邮件主题的文本是在触发器的主题筛选器中指定的:Business Analyst 2 #423501Your email's subject has the text that you specified in the trigger's Subject filter: Business Analyst 2 #423501

    • 电子邮件至少有两个附件。Your email has at least two attachments. 现在,请直接创建两个空的文本文件,然后将这两个文件附加到电子邮件。For now, just create two empty text files and attach those files to your email.

    如果触发器成功触发,但逻辑应用没有触发或运行,请参阅排查逻辑应用问题If your logic app didn't trigger or run despite a successful trigger, see Troubleshoot your logic app.

  3. 检查逻辑应用是否已将电子邮件和附件保存到正确的存储容器。Check that your logic app saved the email and attachments to the correct storage container.

    1. 在存储资源管理器中展开“(本地和附加)”> “存储帐户”>“attachmentstorageacct (外部)”> “Blob 容器”> attachmentsIn Storage Explorer, expand (Local and Attached) > Storage Accounts > attachmentstorageacct (External) > Blob Containers > attachments.

    2. 检查 attachments 容器中是否有电子邮件和附件。Check the attachments container for both the email and the attachments.

      检查是否有已保存的电子邮件和附件

    3. 完成后,删除存储资源管理器中的电子邮件和附件。When you're done, delete the email and attachments in Storage Explorer.

接下来添加一项操作,使逻辑应用发送需查看附件的电子邮件。Next, add an action so that your logic app sends email to review the attachments.

发送电子邮件通知Send email notifications

  1. if true 分支的 For each email attachment 循环下,选择“添加操作”。In the if true branch, under the For each email attachment loop, choose Add an action.

    在“for each”循环下添加操作

  2. 在搜索框中,输入“发送电子邮件”作为筛选器,然后为电子邮件提供程序选择“发送电子邮件”操作。In the search box, enter "send email" as your filter, and then select the "send email" action for your email provider.

    若要通过筛选操作列表来查找特定的服务,可以先选择连接器。To filter the actions list to a specific service, you can select the connector first.

    为电子邮件提供商选择“发送电子邮件”操作

    • 对于 Azure 工作或学校帐户,请选择“Office 365 Outlook”。For Azure work or school accounts, select Office 365 Outlook.
    • 对于个人 Microsoft 帐户,请选择“Outlook.com”。For personal Microsoft accounts, select Outlook.com.
  3. 如果系统要求提供凭据,请登录电子邮件帐户,这样逻辑应用就可以创建到该电子邮件帐户的连接了。If you're asked for credentials, sign in to your email account so that Logic Apps creates a connection to your email account.

  4. 重命名“发送电子邮件”操作并提供以下说明:Send email for reviewRename the Send an email action with this description: Send email for review

  5. 提供此操作的信息,然后选择要包含在电子邮件中的字段,如下所示。Provide the information for this action and select the fields you want to include in the email as shown and described. 若要在编辑框中添加空行,请按 Shift + Enter。To add blank lines in an edit box, press Shift + Enter.

    发送电子邮件通知

    如果在动态内容列表中找不到所需的字段,请选择“收到新电子邮件时”旁边的“查看更多”。If you can't find an expected field in the dynamic content list, choose See more next to When a new email arrives.

    设置Setting Value 注释Notes
    正文Body Please review new applicant:

    Applicant name: 发件人Applicant name: From

    Application file location: 路径Application file location: Path

    Application email content: 正文Application email content: Body

    电子邮件的正文内容。The email's body content. 单击此框的内部,输入示例文本,然后从动态内容列表中选择以下字段:Click inside this box, enter the example text, and from the dynamic content list, select these fields:

    -“收到新电子邮件时”下的“发件人”字段- The From field under When a new email arrives
    -“创建用于电子邮件正文的 Blob”下的“路径”字段- The Path field under Create blob for email body
    -“调用 RemoveHTMLFunction 来清除电子邮件正文”下的“正文”字段- The Body field under Call RemoveHTMLFunction to clean email body

    主题Subject ASAP - Review applicant for position: 主题ASAP - Review applicant for position: Subject 要包括的电子邮件主题。The email subject that you want to include. 单击此框的内部,输入示例文本,然后在动态内容列表中,选择“收到新电子邮件时”下面的“主题”字段。Click inside this box, enter the example text, and from the dynamic content list, select the Subject field under When a new email arrives.
    ToTo <recipient-email-address><recipient-email-address> 为进行测试,可以使用自己的电子邮件地址。For testing purposes, you can use your own email address.

    Note

    如果选择了一个包含数组的字段,例如“内容”字段(包含附件的数组),设计器会围绕引用该字段的操作自动添加“For each”循环。If you select a field that contains an array, such as the Content field, which is an array that contains attachments, the designer automatically adds a "For each" loop around the action that references that field. 这样一来,逻辑应用就可以对每个数组项执行该操作。That way, your logic app can perform that action on each array item. 若要删除该循环,请删除数组的字段,将引用操作移至循环外部,选择循环的标题栏中的省略号 (...),然后选择“删除”。To remove the loop, remove the field for the array, move the referencing action to outside the loop, choose the ellipses (...) on the loop's title bar, and choose Delete.

  6. 保存逻辑应用。Save your logic app.

现在请测试自己的逻辑应用,该应用类似于以下示例:Now, test your logic app, which now looks like this example:

完成的逻辑应用

运行逻辑应用Run your logic app

  1. 向自己发送一封满足以下条件的电子邮件:Send yourself an email that meets this criteria:

    • 电子邮件主题的文本是在触发器的主题筛选器中指定的:Business Analyst 2 #423501Your email's subject has the text that you specified in the trigger's Subject filter: Business Analyst 2 #423501

    • 电子邮件有一个或多个附件。Your email has at one or more attachments. 可以重复使用上一测试留下的任何空的文本文件。You can reuse an empty text file from your previous test. 如需更逼真的方案,请附加一个简历文件。For a more realistic scenario, attach a resume file.

    • 电子邮件正文包含以下文本(可以复制和粘贴):The email body has this text, which you can copy and paste:

      Name: Jamal Hartnett   
      
      Street address: 12345 Anywhere Road   
      
      City: Any Town   
      
      State or Country: Any State   
      
      Postal code: 00000   
      
      Email address: jamhartnett@outlook.com   
      
      Phone number: 000-000-0000   
      
      Position: Business Analyst 2 #423501   
      
      Technical skills: Dynamics CRM, MySQL, Microsoft SQL Server, JavaScript, Perl, Power BI, Tableau, Microsoft Office: Excel, Visio, Word, PowerPoint, SharePoint, and Outlook   
      
      Professional skills: Data, process, workflow, statistics, risk analysis, modeling; technical writing, expert communicator and presenter, logical and analytical thinker, team builder, mediator, negotiator, self-starter, self-managing  
      
      Certifications: Six Sigma Green Belt, Lean Project Management   
      
      Language skills: English, Mandarin, Spanish   
      
      Education: Master of Business Administration   
      
  2. 运行逻辑应用。Run your logic app. 如果成功,逻辑应用会发送一封电子邮件,如以下示例所示:If successful, your logic app sends you an email that looks like this example:

    逻辑应用发送的电子邮件通知

    如果没有收到任何电子邮件,请检查电子邮件的垃圾邮件文件夹。If you don't get any emails, check your email's junk folder. 垃圾电子邮件筛选器可能会将这些类型的邮件重定向。Your email junk filter might redirect these kinds of mails. 否则,如果不确定逻辑应用是否正常运行,请参阅逻辑应用故障排除Otherwise, if you're unsure that your logic app ran correctly, see Troubleshoot your logic app.

祝贺!你现在已创建并运行逻辑应用。该应用可以跨不同的 Azure 服务自动完成任务,并且可以调用某些自定义代码。Congratulations, you've now created and run a logic app that automates tasks across different Azure services and calls some custom code.

清理资源Clean up resources

不再需要包含逻辑应用和相关资源的资源组时,可将其删除。When no longer needed, delete the resource group that contains your logic app and related resources. 在 Azure 主菜单中转到“资源组”,然后选择逻辑应用的资源组。On the main Azure menu, go to Resource groups, and then select the resource group for your logic app. 选择“删除资源组”。Choose Delete resource group. 输入资源组名称作为确认,然后选择“删除”。Enter the resource group name as confirmation, and choose Delete.

删除逻辑应用资源组

获取支持Get support

后续步骤Next steps

本教程介绍了如何集成 Azure 存储和 Azure Functions 之类的 Azure 服务,以便创建处理和存储电子邮件附件的逻辑应用。In this tutorial, you created a logic app that processes and stores email attachments by integrating Azure services, such as Azure Storage and Azure Functions. 现在,请详细了解可以用来生成逻辑应用的其他连接器。Now, learn more about other connectors that you can use to build logic apps.