Auto-save Email Attachments in SharePoint

How to Save Email Attachments in SharePoint with Power Automate?

If you are part of a company and working on a specific project, dealing with multiple shared mailboxes is common to meet various organizational needs. High volume of emails and numerous attachments can lead to the risk of overlooking important files in these mailboxes. Indeed, digging through the Inbox to find and save email attachments is not an easy task.

Rather, we can utilize the power of Microsoft Power Automate’s flow to extract and save outlook email attachments from shared mailbox to SharePoint. In addition, we can slightly customize this flow to automatically save email attachments from personal mailbox to OneDrive. By doing so, we save time and reduce the risk of missing important attachments. Furthermore, we’ll ensure your email storage stays clutter-free by automatically removing attachments after saving them.  

To get a visual demonstration of how this flow can be constructed and implemented, check out this video: 

Process Summary: A Quick Overview to Save Email Attachments

The way that the flow would progress is as follows: 

  1. When an email arrives in a shared mailbox, the flow checks if it holds any attachment(s). 
  2. If attachments are present, the flow will automatically save email attachments to the SharePoint document library specified in the flow. 
  3. However, prior to saving, the flow checks if a file with the same name already exists in the specified path. 
  4. If a file with a similar name is found, the attachment is saved with its delivery time appended to the name. This ensures that the file is preserved without overwriting existing files.   
  5. Additionally, we’ll remove the attachments from the email once they are stored in SharePoint. This action will free up mail storage space. 

How to Incorporate the Flow in Your Environment? 

  1. Import the flow to automatically save email attachments. 
  2. Procedural walkthrough to build your own flow to save email attachments.

I. Import Pre-constructed flow

If you prefer not to build the flow from scratch, you can use the pre-built exported package of this flow provided below. Import and use this workflow in your environment by following the steps provided.

The exported zip file packages for both flows featured in this blog on- Automate and Organize: How to Save Email Attachments in SharePoint with Power Automate? are available below. 

AutomateSavingEmailAttachmentsFlowPackages.zip

Importing the flow zip file
  • Click the ‘Upload’ option and select the downloaded zip file package. Now you can see all the details regarding the package. 
  • Under ‘Related resources’, you can see all the resources that are used in this flow. Connect your M365 account with those resources, and then click ‘Import’.

Note: The connectors that we have incorporated in this flow are – SharePoint and Office 365 Outlook.

Connecting the account to use resources
  • Now the flow will be imported, and you can find it under the ‘My flows’ tab. 

You’re all set! Now, all you need to do is:

  • Turn on your flow.
  • Fill in the mailbox address, SharePoint site address, and document library folder fields, and start using it right away.

II. Save Email Attachments by Building Your Own Flow 

Follow the steps explained and you can easily construct your own flow to automatically save outlook email attachments to SharePoint. Moreover, for better understanding, the whole implementation is consolidated into a few steps as outlined below:

  1. Kickstarting with Power Automate flow to extract email attachments.
  2. Automate email attachment removal with Power Automate.
  3. Check if a file with the attachment’s name already exists in SharePoint folder
  4. Create file without overwriting existing files.
  5. Saving unique email attachments.
  6. Automated removal of outlook attachments.
  7. Terminate the flow with Success status.

Step 1: Kickstarting with Power Automate to Extract Email Attachments

  1. Login to your Microsoft Power Automate portal
  2. To create a new flow, click ‘Create’ from the left pane and select ‘Automated cloud flow’. 
  3. Set up your flow name. Select the trigger, ‘When a new email arrives in a shared mailbox(V2) from the ‘Office 365 Outlook’ connector. 


    Trigger- When anew email arrives in a Shared mailbox
    Selecting the trigger for the flow

  4. Enter the shared mailbox address in the ‘Original Mailbox Address’ field of the trigger. Then select the ‘Inbox’ folder where you receive the incoming emails containing attachments.

    Note: For this operation to succeed, the flow creator’s account or the account associated with the ‘Office 365 Outlook’ connection should have permission to access the shared mailbox specified in this field.

  5. Set ‘Yes’ for the fields ‘Only with Attachments’ and ‘Include Attachments’. This ensures that our flow will trigger only when an email with attachments is received. 

You shall fill out the rest of the fields as per your specific needs, while I am just keeping it blank. 

Flow triggers when an email arrives with attachments.
Data Input for the trigger

Step 2: Automate Email Attachment Removal with Power Automate 

When receiving an email with attachments, you may opt not to keep them in the email while saving them in SharePoint. Obviously, this reduces the mailbox storage needed to accommodate these attachments. In order to build a workflow for email attachment cleanup, we need to perform the below three actions: 

  1. Retrieve the Inbox folder properties by sending an HTTP request. By doing so, we can specifically retrieve the Inbox folder ID, where emails with attachments are received. 
  2. Use the Initialize variable action to store the Inbox folder ID value for the upcoming Send an HTTP request action within our flow. 
  3. Delete the attachments with an HTTP request from the emails received in the inbox.

1. Add the ‘Send an HTTP Request’ action from the ‘Office 365 Outlook’ connector 

  • URI: Use the below query in the URI field:

https://graph.microsoft.com/v1.0/users/<SharedMailboxAddress>/mailFolders?filter=displayName eq ‘Inbox’

First, we recommend trying out this Graph Api query in Graph Explorer | Try Microsoft Graph APIs – Microsoft Graph. This will help you to understand and obtain the necessary consent for permissions before using it.
Replace <SharedMailboxAddress> with the address of the shared mailbox passed in the trigger. 

Returning to our flow, fill in the fields like below.

  • Method: Set the Method field to ‘GET’ to retrieve the required information. 
  • Content-Type: Use the value – ‘application/json’, for the ‘Content-Type’ field. 
  • Leave the Body field unfilled, as it is not required for this action.
HTTP request to get Inbox folder ID

2. We’ll now use the ‘Initialize variable’ action with the following configurations: 

  • Name: In the ‘Name’ field, give the variable a name. Let’s use ‘InboxID’ for this example. 
  • Type: Set the ‘Type’ field as ‘String’
  • Value: In the ‘Expression’ tab of value, construct the following expression and click ‘OK’
    body('Send_an_HTTP_request').value[0].id
  • Here, the following expression is used, since we’ve renamed the action ‘Send an HTTP request’.
    body('Send_an_HTTP_request_-_To_get_the_Inbox_folder_ID').value[0].id

This expression will store the ‘Inbox folder’s Id’ value from the array of values obtained from the previous Send an HTTP request action.  

Initializing variable to store Inbox folder ID

3. Next, we need to use the ‘Send an HTTP request’ action again from the ‘Office 365 Outlook’ connector.  

But this time, we’ll use it to automate the deletion of email attachments from the received email. Note that this action should be added only after using the Create file action to save email attachments in SharePoint. For now, Let’s focus on saving the attachments and we’ll postpone adding the ‘Send an HTTP request’ action for later. 

Note: You can totally skip Step 2 if you prefer to retain the email attachments within the emails. However, checking the mailbox size would be beneficial in deciding whether to include this step or not.

Step 3: Check If a File with Attachment’s Name Already Exists 

Before proceeding further, it is significant to consider the complications involved while saving the Outlook email attachments. 

In one case, the receiving email attachments might replace the existing file in the SharePoint document library if the file names are identical. Hence it prevents file duplication. 

However, in another case, files may have different content but share the same names. Therefore, it is equally important to manage the retention of those files as well. For this reason, to avoid file replacement, we’ll save email attachments with their received time appended to their name in such cases. Therefore, to check if a file with the same name as attachments exists, follow the steps below. 

  1. Select the action Get file content using path and choose the desired SharePoint site for the ‘Site Address’ field where you want to save your attachments. This action will specifically retrieve a single file from the path we mention.   
  2.  To check for a file with a similar name, type the path till the folder (/Shared Documents/Mail attachments/), where you want to save your attachments. Then choose ‘Attachments name’ from the dynamic content.
Check the existence of file with same name

Since there could be multiple attachments in an email, the action Apply to each will get included itself to iterate through them. Hence, we shall place the further actions within ‘Apply to each’.

Step 4: Create File without Overwriting Existing Files 

If the action ‘Get file content using path’ could retrieve a file, it indicates the presence of an existing file with the same name as the attachment. This may lead to overwriting the existing file. Thus, before proceeding with ‘Create File’ action, let’s proceed to handle the file overwriting.

  1. To achieve this, let’s use the Compose action first.
  2. If we try to save those email attachments with their received time as discussed earlier, the format of the date and time will be in unrecognizable terms.    
  3. To modify that, we shall construct an expression with ‘formatDateTime’ function in the expression tab of Compose action. 
  4. Select the ‘Received time’ from the dynamic content, copy it, and then remove it. 
  5. Now paste the copied content within the ‘formatDateTime’ function.  
  6. Then you shall format it like the expression below and click ‘Ok’

formatDateTime(triggerOutputs()?['body/receivedDateTime'],'dd/MM/yyyy  hh:mm tt')

Formatting the received date time of email

Note: SharePoint library’s naming restrictions in saving a file don’t allow symbols like ‘/’ and ‘:’. Therefore, the symbols involved in date and time format (‘/’,’:’) will get replaced with underscores (_). For instance,10/07/2023 09:20 AM will become 10_07_2023 09_20 AM in the file name.

 4.1: Save Email Attachments with Received Date-Time 

Once the received time of the email is formatted, we shall move towards saving the attachments in the SharePoint site.

  1. Let’s select the Create file action.
  2. Select the appropriate SharePoint site for the field ‘Site Address’. For the ‘Folder path’, you shall select the path using the ‘Show picker’ as shown below.
  3. For the fields ‘File Name’ and ‘File Content’, select ‘Attachments Name’ and ‘Attachments Content’ from the dynamic content.
  4. Make sure to also include the output of the ‘Compose’ action in the ‘File Name’ field. 

Thus, to prevent the overwriting of files, we will be saving the attachments along with their received time. 

Save attachments with their received date-time

Step 5: Save Unique Email Attachments: Non-identical Names

In contrast, to save the email attachments having unique names that do not conflict with existing files in the SharePoint library, we will perform the following steps. 

  1. Add another Create file action from the ‘SharePoint’ connector parallel to the Compose action. 
  2. Then, configure this action for the scenario where its previous action ‘Get file content using path’ fails. This failure may occur if a file with the same name as the attachment is not present in the specified folder.
  3. Hence, set the ‘Configure run after’ option of this ‘Create file’ action as ‘has failed’. So, it will run only if the previous action fails. Otherwise, it will be skipped.


    Edit Configure run after of Create file action to handle previous action failure.
    Create file action’s Configure run after option


    Set configure run after as 'has failed'
    Setting the Configure run after as ‘has failed’

  4. Fill in the fields accordingly as done in the previous Create file action.  


    save email attachments with name
    Save email attachments with their name

Step 6: Automated Removal of Outlook Attachments

Once the attachments are saved, proceed to delete them from the email, which was left pending in Step 2. And so, we’ll add the Send an HTTP request action within Apply to each loop, right after each Create file action.

Here are the details to configure the action: 

  • URI: Use the provided URI pattern. 

https://graph.microsoft.com/v1.0/users/<SharedMailboxAddress>/mailFolders/InboxID/ messages/<MessageID>/attachments/<AttachmentID>

Replace <SharedMailboxAddress> with the shared mailbox address used in previous actions within the flow. For <InboxID>, select the output of ‘Variables’ from the dynamic content. And for <MessageID> and <AttachmentID>, use the respective values available in the dynamic content.

  • Method: Set the method as ‘DELETE’ 
  • Content-Type: Use ‘application/json’ 
  • Leave the Body field unfilled, as it is not required for this case.
delete email attachments after saving
HTTP request to delete the email attachments

Step 7: Terminate the Flow with Success Status

Before concluding, let’s include one more step to ensure a successful flow run status.
In case the action Get file content using path fails within the Apply to each action, the entire ‘Apply to each’ action will be considered failed. Consequently, the flow run status will be marked as ‘Failed’. This applies even if the Create file action is successfully executed using ‘Configure run after’, since it comes after the failure of the Get file content using path action.

Therefore, use the Terminate action outside the ‘Apply to each’ action. Set its ‘Configure run after’ as ‘has failed’ which ensures the flow run status successful despite the failure of Apply to each action.

Terminate the flow with Success status

Final Visual: Save Email Attachments in SharePoint 

That’s it! Our simple flow with significant impact on managing email attachments is finally ready and this is the overall visual. 

Save email attachments in SharePoint
Save email attachments in SharePoint

How to Automatically Save Email Attachments in OneDrive? 

Similarly, we can automate the process to save email attachments from our personal Outlook mailbox to OneDrive. This could be achieved by making a few customizations in our created workflow. 

All the actions that we’ve used from the SharePoint connector will be replaced by similar actions within OneDrive for Business connector. 

  1. Firstly, use When a new email arrives(V3) as the trigger from the Office 365 Outlook connector. 
  2. Secondly, replace the action Get file content using path with Get file content using path action from OneDrive for Business connector. 
  3. Finally, replace Create file action with Create file action from OneDrive for Business connector.
  4. Meanwhile, keep the rest of the actions and configurations the same as we did for the Shared mailbox. 

Below is the final visual representation of the flow to automate saving outlook attachments in OneDrive! 

You can share a copy of this flow with your colleagues. Thus, let them have their personalized automation process to transfer the Outlook email attachments to OneDrive. 

Final Visual: Save Email Attachments in OneDrive

save email attachments in OneDrive
Save email attachments in OneDrive

So, why worry about finding email attachments or losing track of them when there is a way to automate the process? Even though Microsoft has pre-built templates for this practical case, the flow we’ve created includes extra checks. This makes it more detailed and specific to our needs. So, create your own workflow using these steps to automate saving email attachments in SharePoint or in OneDrive, in a more granular way.

We highly value your thoughts and comments. Feel free to share your insights on this automated saving of outlook attachment flow and engage with us by leaving a comment in the comment section below. We look forward to hearing from you!

For more automation goodness, explore our other blog post that outlines a step-by-step flow to automate user onboarding with manager approval and password generation.

Microsoft 365 User Onboarding Workflow For Easy User Provisioning | AdminDroid Blog

Leave a Reply

Your email address will not be published. Required fields are marked *

How to Save Email Attachments in SharePoint with Power Automate?

time to read: 11 min
Follow us!