Bringing back Incoming Email to SharePoint Online document library using Power Automate

Objective

This article aims to outline the process for setting up incoming email functionality in SharePoint Online. This feature will help you receive incoming emails with attachments to the SharePoint document library.

Scenario

The Incoming email functionality allows the SharePoint sites to accept emails and store them in lists and libraries. This feature used the local SMTP server to provide this function. However, with Office 365, this feature has been deprecated.

This helps you store the mail contents sent to other recipients in SharePoint libraries without uploading the contents manually.

In this article, we help you achieve that functionality using a Shared Mailbox and a Power Automate flow, with a specific set of features:

1. Save emails received from specific domains.

2. Fix to clean special characters from the subject line for folder creation.

Prerequisites

1. A SharePoint document library with an additional Text column for Mail Sender

  • For this blog, we are using a document library with the name MailStore

2. We need a Shared Mailbox, which will be used to receive emails that will be further uploaded to the SharePoint library.

  • For this blog, we are using a Shared Mailbox with the name IncomingMails

3. Security requirements of Accounts – The accounts running the flow need to have access to the Shared Mailbox.

Configuration Steps

1. We will be creating a flow with the action to trigger when a new item with an attachment is received in the shared mailbox.

2. The flow further processes the mails to upload the mail content and attachments to the SharePoint document library.

3. Browse to the Power Automate site using the account that has Power Automate License to create a new Flow

4. Selected New Flow > Automated Cloud flow

new flow

5. Specify the desired name (we have named it Save emails to Document Library) and under the triggers, select When a new email arrives in a shared mailbox (V2) (Office 365 Outlook). This will trigger the flow as soon as an item is created in our Shared Mailbox.

new flow

6. In the trigger, specify the Shared Mailbox alias (IncomingMails) that we will be used to receive Incoming emails for the SharePoint document library.

  • The Folder should be selected as Inbox
  • In the advanced options, Only with Attachments & Include Attachments should be set to Yes
  • incoming email action

    7. Initiate the following variables, which will be used further in the flow for calculations.

            a) From – This will be used to filter mails from specific domains and to update the Mail Sender field in the library

                      i. Type: String

                      ii. Value: toUpper(triggerBody()?['From'])

    initialize variable

            b) Subject – This will be used to create a folder in the document library to store the email and the attachments.

                      i. Type: String

                      ii. Value: @{triggerBody()?['Subject']}

    initialize variable

            c) InvalidCharacters – This will be used to clean the Subject with any special characters to create a folder name

                      i. Type: Array

                      ii. Value: createArray(':','<','>','?','/','|')

    initialize variable array
    d) Temp – This will be used to store the subject value temporarily until the special characters have been removed in the foreach loop

               i. Type: String

               ii. Value: Blank

    initialize variable string

    8. In the next step, we remove the invalid characters from the mail subject, which will further be used to create a folder in the library.

    apply to each invalid char

    The following expressions have been used in the above step.

            a) Loop for each invalid character

                    i. Expression: @{variables('InvalidCharacters')}

    invalid character flow
    b) Set Temp variable

            i. Expression: replace(variables('Subject'),item(),'')

    invalid character flow variable

    c) Set subject variable after removal of invalid characters

            i. Expression: @{variables('Temp')}

    invalid character flow variable

    9. In the next step, we add a condition to allow the processing of emails received from specific domains. This will ensure we filter out any unwanted emails.

    filter sender flow

    10. In the next step, we filter out emails that do not have attachments.

           Expression: @triggerBody()?['HasAttachment']

    filter attachments flow

    11. In the next step, we further process the emails that have attachments.

    process email attachments flow

    12. We first export the contents of the email using the Export email (v2) action. This will be used to upload the email contents to the document library.

    Expression: @{triggerBody()?['Id']}

    export email content flow

    13. The next step is to create a folder in the document library using the subject as the name.

    Expression: @{variables('Subject')}

    create folder subject flow

    14. The next step is to send an email to the admin in case of the previous step of folder creation failure. This will have a run after configuration as below. The content of the email can be customized as per requirements.

    folder creation failure notification flow
    email creation failure notification flow

    Go to the Send an email notification settings – New Folder Failed step and then click on Configure run after. Uncheck all options and select has failed. This will ensure that the email will be triggered only when the create folder action fails.

    run after action failed flow

    15. The next step is to create the eml file in the Subject named folder created earlier.

    Expressions:

  • Folder Path: /MailStore/@{variables('Subject')}
  • File Name: @{concat(triggerBody()?['Subject'],'.eml')}
  • File Content: @{body('Export_email_Content')}
  • create email action flow

    This will also have a run after configuration to run only when the previous step is skipped. This will ensure that if the new folder creation fails and the email notification is triggered, then create eml file will not be triggered since the folder creation had failed earlier.

    run after skip email action flow

    16. The next step is again to send an email to the admin in case of the previous eml file creation failure. The contents of the email can be customized as per requirements.

    create email action failure notification
    This will run after configuration as below to ensure that the email will be triggered only when the create eml file action fails.
    email creation failed notification
    17. The next step is to update the properties of the eml file created in the previous step. 

    Expressions:

            a) Id: @{body('Create_eml_file')?['ItemId']}

            b) MailSender: @{variables('From')}

    update file properties action
    This step will run after configuration as below to ensure it will be triggered only when the previous step is skipped. This will ensure that if the new eml file creation fails and the email notification is triggered, then create update file properties of eml file will not be triggered since the eml file creation had failed earlier.
    run after update file properties action skipped
    18. In the next step, we check-in and publish a major version of the eml file created in the earlier steps. This will make it visible to authorized users.

    Expressions:

            a) Id: @{body('Create_eml_file')?['ItemId']}

    check in file action

    19. The next step is to use the Apply to each action. This will loop through each attachment in the email and save it in the same Subject named folder created earlier.

    Expression: @{triggerBody()?['Attachments']}

    apply to each save attachments

    20. The next step is to create a file from the attachment in the Subject named folder created earlier.

    Expressions:

            a) Folder Path: /MailStore/@{variables('Subject')}

            b) File Name: @{items('Apply_to_each')?['Name']}

            c) File Content: @{items('Apply_to_each')?['ContentBytes']}

    create file from attachment action

    21. The next is to send an email to the admin in case of the previous step of attachment file creation failure. This will have a run after configuration as below. The contents of the email can be customized as per requirements

    attachment creation failed notification
    Go to the settings on the Send an email notification – file attachment Failed step and then click on Configure run after. Uncheck all options and select has failed. This will ensure that the email will be triggered only when the create attachment file action fails.
    run after file creation failure notification

    22. The next step is to update the properties of the attachment file created in the previous step.

    Expressions:

            a) Id: @{body('Create_file_from_attachment')?['ItemId']}

            b MailSender: @{variables('From')}

    update file properties action

    This step will run after configuration as below to ensure it will be triggered only when the previous step is skipped. This will ensure that if the new attachment file creation fails and the email notification is triggered, then create update file properties of the attachment file will not be triggered since the attachment file creation had failed earlier.

    23. In the next step, we check-in and publish a major version of the attachment file created in the earlier steps. This will make it visible to authorized users.

    Expressions:

            a) Id: @{body('Create_file_from_attachment')?['ItemId']}

    Check in attachment file

    Summary

    The above steps helped us achieve the Incoming email functionality that allows the SharePoint sites to accept emails and store them in lists and libraries.

    The Power Automate flow has the following features.

    1. Save emails received from specific domains.
    2. Fix to clean special characters from the subject line for folder creation
    3. Handle failures and notify admins at file creation and upload stages.
    4. Upload multiple attachments
    AneeshKumar

    Written By-  Aneesh Kumar

    (Microsoft 365 Solution Architect)

    Written By-  Aneesh Kumar

    (Microsoft 365 Solution Architect)

    Jasjit

    Peer Reviewed By-  Jasjit Chopra

    (CEO)

    Peer Reviewed By-  Jasjit Chopra

    (CEO)

    Sanika

    Graphics Designed By- Sanika Sanaye

    (Creative Design Director)

    Graphics Designed By- Sanika Sanaye

    (Creative Graphic Designer Trainee)

    {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Connect with us

    >