How to automatically archive labeled e‑mails in Power Automate
This tutorial shows how to automatically archive e‑mails that have a specific label in Power Automate, saving the content and attachments to a SharePoint library and recording useful metadata. It is useful for organizing correspondence, meeting retention policies, and automating audit tasks.
Prerequisites
- Microsoft 365 account with access to Outlook/Exchange and SharePoint.
- Permissions to create flows in Power Automate and write to a SharePoint library.
- A SharePoint library with columns for From, Subject, Received.
Step 1: Create a new automated flow
Open Power Automate and create an "Automated cloud flow" with the trigger When a new email arrives (V3) from the Outlook connector. This trigger lets you react to e‑mails as soon as they arrive in the inbox.
Trigger: When a new email arrives (V3)
Folder: Inbox
Include Attachments: Yes
Only with Attachments: No
Step 2: Filter by label (category) on the e‑mail
The trigger does not always provide category filtering, so we use a condition to check whether the e‑mail contains the desired label (for example, "Arquivar"). The property that contains categories is Categories.
Condition:
@contains(triggerOutputs()?['body/Categories'], 'Arquivar')
Step 3: Prepare file metadata
When the e‑mail matches the condition, create variables with the metadata to save in the SharePoint library. This makes classification and search easier later.
Initialize variable 'EmailFrom' (String) = triggerOutputs()?['body/from']
Initialize variable 'EmailSubject' (String) = triggerOutputs()?['body/subject']
Initialize variable 'EmailReceived' (String) = triggerOutputs()?['body/receivedDateTime']
Step 4: Save the e‑mail body as a .eml or .html file
Choose the format you prefer. For compatibility with mail readers, you can save as .eml; for web viewing, use .html. Use the action Create file from the SharePoint connector to create the main file in the library.
Action: Create file (SharePoint)
Site Address:
Folder Path: /Archive/Emails
File Name: @{formatDateTime(utcNow(),'yyyyMMdd_HHmmss')}_@{replace(triggerOutputs()?['body/subject'], '/', '-')}.eml
File Content: triggerOutputs()?['body'] // or body in HTML: triggerOutputs()?['body/bodyPreview']
Step 5: Save attachments, if any
If the e‑mail has attachments, iterate over the Attachments collection and create separate files in the same folder. Use the Apply to each action with Attachments.
Apply to each attachment in triggerOutputs()?['body/Attachments']
Create file (SharePoint)
File Name: @{items('Current')?['Name']}
File Content: @{items('Current')?['ContentBytes']}
Step 6: Update metadata of the main file
After creating the e‑mail file, update the library columns with the metadata (From, Subject, Received). You can use the Update file properties action pointing to the ID of the created file.
Get file metadata using path (SharePoint)
Path: /Archive/Emails/filename.eml
Update file properties (SharePoint)
ID: @{body('Get_file_metadata_using_path')?['UniqueId']}
From: @{variables('EmailFrom')}
Subject: @{variables('EmailSubject')}
Received: @{variables('EmailReceived')}
Step 7: Handle exceptions and avoid duplicates
Implement a simple control: search the library for files with the same Message‑ID (if available) or Subject+Received. If one already exists, you can choose to update or skip. Use the Get files (properties only) action with an OData filter.
Get files (properties only) Filter Query:
Subject eq '@{replace(triggerOutputs()?['body/subject'], "'", "''")}' and Received eq '@{variables('EmailReceived')}'
Condition: length(body('Get_files')?['value']) is greater than 0 -> skip or update
Verify the result
To confirm the flow works, send a test e‑mail with the category "Arquivar" and, if applicable, attachments. Check the SharePoint library: there should be a .eml or .html file with the subject, populated metadata and the attachments saved as separate files. If they do not appear, consult the flow run history in Power Automate to identify errors (permissions, invalid file names or size limits).
Conclusion
With this flow in Power Automate you can automatically archive labeled e‑mails, keeping metadata and attachments organized in SharePoint — useful for compliance and efficiency. Next steps: add automatic indexing, connect to a retention process, or send notifications when items are archived. Tip: always test with various attachment formats and subjects to avoid errors with invalid characters.