Power Automate is a Microsoft service that enables you to create automated workflows for various tasks and processes. You can use Power Automate to connect different apps and services, send notifications, manage approvals, and more.
In this post I am going to show you how you can use Power Automate to save attachments from your emails to a specific SharePoint folder. This way, you don’t have to manually download and organize your files. You can also set up filters to save only the attachments from certain senders or with certain keywords.
There are many reasons that you may want to do this, it may be a stand alone use case or part of a wider use case and solution. Here are some potential business use cases for using Power Automate to automatically save email attachments to a SharePoint folder:
- Contract Management - Automatically save all attachments from legal/contract emails to a Contracts document library for record keeping.
- Invoice Processing - Save vendor invoice attachments from Accounts Payable emails to a folder to streamline approvals.
- Recruiting - Save resume attachments from recruiting emails to candidate profiles in SharePoint.
- Support Ticket Management - Save attachment logs/screenshots from customer support emails to tickets in SharePoint for reference.
- Policy Distribution - Save policy document attachments circulated via email to a central company policies library.
- Real Estate - Save listing attachments from broker emails to organized property folders.
- Event Planning - Save event supporting docs like proposals, speaker agreements, from planner emails.
- Training Management - Save training materials/manual attachments from internal trainer emails.
- Onboarding - Save offer letter attachments, forms completed by new hires.
Here is a high level view of the Flow and a step by step summary of how you might achieve this or a similar Flow in Power Automate:
Step 1: Create the flow with the "When a new email arrives" Trigger
Create a New "Automated Cloud Flow" and choose the "When a new email arrives(V3)" trigger. If you haven't already youy may need to enter login details to connect the outlook account for the mailbox you want this flow to work against. The connection can easily be changed after creation by visiting the Flow details page and editing connections in the top right hand corner.
NB: The versions of the triggers and actions throughout Power Automate may well change as time moves on, it is usually best to try whichever is the latest one when building your Flow unless it is a very recent one and still in preview then can make sense to use the older one.
In this case it is the Attachements we are most intereted in, so make sure to set the Include Attachments option to Yes so that you can access the attachments later.
Depending on your process needs and by using the settings in the trigger step, it could be set to fire only under certain circumstances, e.g Only when an email is moved to a certain folder in your mailbox, only when it is from a certain email address or has a certain subject.
For the purpose of this demonstration we are going to trigger only for Subjects that contain the string "AttachementSave", and set the Only with Attachments to Yes.
Step 2: Create 2 Variables to store Name of File and Folder
Initialize two variables to store the name of the folder and the name of the email file, set them to what you want your names to be.
To get unique names for your folder and file names you could use expressions to format the date and time and add this to the name, for example to get the date in the format of 230908 you can use. - formatDateTime(utcNow(),'yyMMdd')
You could also use other details from the email like subject, senders name, attachment name as part of your folder and file name, and of course you could hard code some elements of it. For unique values it is best to use the ID's values from you dynamic content, e.g Email ID or Attachement ID.
What you choose to do will be very much dependent on your use case and how you want it to work to meet your needs. You may want add the time onto the end of folder or filename or use the information from the from email or subject to give unique and logical names.
The screen shot below shows an example of the variable being set using the Concat function as the Email ID + Formated Date and Time. - Concat(triggerOutputs()?['body/id'], formatDateTime(utcNow(),'yyMMdd'))
A similar logic can then be used to create the second folder variable. One options would be to use the first 10 characters of the Subject as part of the folder name e.g. -Concat(substring(triggerOutputs()?['body/subject'],0,10), formatDateTime(utcNow(),'yyMMdd'))
Step 3: Create the Folder and save a Copy of the email to it.
Use the Create New Folder action from the SharePoint connector to create a new folder in your SharePoint document library.
- The Site address field will give you a list of sites in your current environment.
- Once chosen you will then be able to select from the list in the List or Library field to choose where the folder is going to be created.
- Use the folder name variable as the input for the Folder name. In this example we have also used some hard coded values in the folder path, the first part is the name of the Folder that has been created in advance as the location for these items to be saved to, the second hard coded part is a prefix to the folder name we are creating of "Email". This could also have been added in at the point the VFolderName variable was created and set.
Use the Export Email (V2) action from the Outlook connector to get the email content as an EML file.
- Use the Message ID as dynamic content from the trigger action as the input for the Message ID.
- Similar use the To value to specify the Original Mailbox Address.
This step will now have all the details from the email ready for you to create a copy of the meail in your folder.
Use the Create File action from the SharePoint connector to save the email content as an EML file in your SharePoint folder. Use an expression to get the folder path from the Create Folder action, and use the email file name variable as the input for the file name. Use the body from the Export Email (V2) action as the input for the file content.
- Select your Site Address from the list.
- Set the Folder Path to be the Full Path output from the Create new Folder step using the Dynamic Content selector.
- Set the File Name to be the Vfilename varaible, and in this case we have added a prefix "email-" and also hard code the file extension to be ".eml" so we get the correct type of file.
- Finally, set the File Content to be the Body output of the Export email step.
Step 4: Save the Email Attachements to the same location
Add a condition step to check if there are any attachments in the email.
You can do this be using the formula - length(triggerOutputs()?['body/attachments']) - and checking in the condition if the length is greater than 0.
Now to save a copy of each attachement into the SharePoint folder we will need to use an an Apply to each loop to iterate over each attachment.
However to do this without Power Automate trying to create too many Apply To each loops for you, we start by adding the Create File action into the Yes leg of the condition
As each of the values are set in turn from the Dynamic content, when you select the File Name value, Power Automate will automaticall sense there could be more than one of these files and place this action inside an Apply to each so that when it is ran, it will go through each file attached to the email.
- Select your Site Address from the list.
- Set Folder Path to be the Full Path from the Create Folder step.
- Set File Name to be the Attachment Name from the Trigger step - When a new email arrives (V3) - The Apply to each will now appear automatically.
- Set the File Contnet to be the Attachement Content also from the Trigger step.
The Flow should now be ready to test, for this flow if we send an email to the connected mailbox with a subject of "AttachmentSave" and with 3 attachements called Att1, Att2, and Att3, shortly after the email arrives this will create the following:
We get a folder created inside the FraserEmails folder with a copy of the email along with the 3 attachements. You will notice there is an extra image file in there, this is from the image that is used in the signature of the email sent, as images in the email will be treated as additional atachements.
There you have it the basics of how to create flow that automatically saves email attachments to a SharePoint folder for you to expand on and tweak in order to meet your own business process needs.