# Incoming Email via API

## Create the Email Server Source

1. Create your new Incoming email server source, see [Adding a New Server](https://docs.alemba.com/asm-hermes/setup-and-configure-asm/setting-up-your-system/setup-email/email-server-configuration#adding-an-email-server)
2. Select the **“Alemba API”** protocol is available to select when creating a new incoming email server:

{% hint style="info" %}
If you cannot see the ***Alemba API*** email protocol, please contact [Alemba Support](mailto:support@alemba.com).
{% endhint %}

![](https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/FhgDYY4eoO1PR54U7lPv/0)

{% hint style="info" %}
**“Email Server Id”** will contain a value. This value is needed when creating requests to add emails via the API.
{% endhint %}

3. The remaining fields are the standard incoming email fields.

## Logic App Email Configuration

### Part 1: Create the Function and the Function App&#x20;

During the Logic App Configuration, you will need to link to the **ReplaceInlineImages** Azure function. &#x20;

* This action will pass the attachment name, ref and the HTML version of the description to the **ReplaceInlineImages** functions and will return the updated HTML version of the description, to include the inline image.&#x20;

You will need to create a function app and the function itself.  Please contact [Alemba Support](mailto:Support@alemba.com) if you require assistance.

* [ ] When creating the function app, ensure that the Runtime stack is set to ‘Node.js’ and the Version is set to ’12 LTS’
* [ ] The function needs to be deployed locally using VS Code. You can find details about doing this if you go to functions and click on **Add**
* [ ] Select VS Code as the Development environment. It will give instructions on how to configure VS Code to deploy to Azure Functions
* [ ] Extract the **Attachment-Inline-Function.zip** file and open it up in VS Code.  &#x20;
* [ ] In the explorer, right click on **ReplaceInlineImages** and select ‘**Deploy to Function App**…’.&#x20;
* [ ] Select the relevant subscription .     &#x20;

  <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/Kp0elDCIlOP854lKaAST/8" alt=""><figcaption></figcaption></figure>
* [ ] Select the relevant function app. &#x20;

  <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/vYFkQXmhpxfssed10q5u/9" alt=""><figcaption></figcaption></figure>
* [ ] Then click deploy. &#x20;

  <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/UJXPetwHPFH4tdJNUe4E/10" alt=""><figcaption></figcaption></figure>
* [ ] Once completed you can see if it was successful by accessing the function app and see if the **ReplaceInlineImages** function exists.&#x20;

{% hint style="info" %}
You will not be able to see or modify the code within the Azure Portal, so if any modifications are needed, they will need to be done in VS Code and redeployed.&#x20;
{% endhint %}

### Part 2: Build Logic App

1. Create a Logic App with the “**When a new email arrives (V3)**” Office 365 Outlook trigger.&#x20;
2. The **“Include Attachments”** parameters needs to be enabled.

   <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/WYWylcOnG9sM6dsgBHMp/1" alt=""><figcaption></figcaption></figure>
3. Add an **“Initialize variable”** action where the variable name is Attachments and the type is array:&#x20;

   <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/iFUbNhJqw6Im6ZLsS2xu/2" alt=""><figcaption></figcaption></figure>
4. Add an **“Initialize variable”** action where the variable name is BodyHtml and the type is string:&#x20;

   <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/DPaA4OzyyWYbfRwXxw1a/3" alt=""><figcaption></figcaption></figure>
5. Add a **“For each”** action which will loop through the Attachments from the trigger and will append the ***Attachments Content***, ***Attachments Name*** and ***Attachments Is Inline*** values to the attachments array:&#x20;

   <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/EY4zCuXdMWdWUlGNXCwh/4" alt=""><figcaption></figcaption></figure>
6. Add a **“Condition”** action within the **“for each”** action that will check whether the ***Attachments Is Inline*** value is equal to true:&#x20;

   <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/AfdXNNHPPjMwduBhcvkI/5" alt=""><figcaption></figcaption></figure>
7. Go into the settings of the **“for each”** action and set the concurrency control limit to **1**:&#x20;

   <figure><img src="https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/SApMESj0o9SrDwnY4d4s/6" alt=""><figcaption></figcaption></figure>
8. Under the **“True”** side of the condition, add the following actions:
   * Link to the **ReplaceInlineImages** Azure function
     * This action will pass the attachment name, ref and the HTML version of the description to the **ReplaceInlineImages** functions and will return the updated HTML version of the description, to include the inline image. [Please refer to Part 1 for more information](#part-1-create-the-function-and-the-function-app)
9. Set the **BodyHtml** variable with the **DescriptionHtml**

![](https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/skZfQqGJ0hVUIeGqqFJv/11)

## Post to the Alemba API

1. Outside of the “**for each**” loop, add a new “**Http**” action that will post the data to the Alemba API.&#x20;

{% hint style="info" %}
You will need to add any necessary authentications. The endpoint url will follow this format: https\://{Server Endpoint}/{System Name}/alemba.api/api/v2/email-incoming/&#x20;
{% endhint %}

2. The “**EmailServerId**" parameter is the “**Email Server Id**” value from the email server settings in ASM.

![](https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/796JZSDapOedoDRGenPR/12)

![](https://content.gitbook.com/content/hlW9jKl7dcDggHAPhNU9/blobs/RXghzl1Le3rS6wMjlBx4/13)
